为什么这台扫描仪找不到它的文件? [英] Why doesn't this scanner find its file?

查看:100
本文介绍了为什么这台扫描仪找不到它的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello社区,



我一直在尝试用Java读取.txt文件。这是我的代码中引发错误的行:



我尝试过:



Hello community,

I've been trying to read a .txt file in Java. This is the line in my code that throws an error:

What I have tried:

Scanner in = new Scanner(Paths.get("C:\\Users\\longyuxi\\Documents\\ICPResult.txt"));



我已将目录复制并粘贴到计算机上的文件浏览器中,并打开了我希望它打开的txt文件。不过这行抛出了一个 java.io.IOException



我也尝试过读取文件使用FileReader。它会抛出一个 Java.io.FileNotFoundException


I've copied and pasted the directory into the file explorer on my computer and it opened the txt file that I wanted it to open. Nevertheless this line throws out a java.io.IOException.

I've also tried to read the file using FileReader. It throws out a Java.io.FileNotFoundException

FileReader fileReader = new FileReader("C:\\Users\\longyuxi\\Documents\\ICPResult.txt");





看到使用绝对路径不起作用,我已经试图把我的txt文件放在我程序的工作目录下。但是我仍然没有找到运气。这是我的代码:





Seeing that using absolute path doesn't work, I've tried to put my txt file under the working directory of my program. However I still found no luck. This is my code:

Scanner in2 = new Scanner(Paths.get("ICPResult.txt"));





有人可以请指出我的错误吗?



祝你好运



更新:我将文件复制到不同的驱动器中,但代码仍无效。

我还添加了一个try-catch块应该打印出错误消息的代码如下。但是,没有打印任何内容。





Can someone please kindly point out my error?

Best regards

Update: I copied the file into different drives, but the code still doesn't work.
I've also added a try-catch block to the code that should print out the error message as follow. However, nothing is printed.

try {
            File input = new File("ICPResult.txt");
            FileReader fileReader = new FileReader("D:\\ICPResult.txt");
            FileReader fileReader2 = new FileReader("F:\\ICPResult.txt");
            Scanner in = new Scanner(Paths.get("C:\\Users\\longyuxi\\Documents\\ICPResult.txt"));
            Scanner in2 = new Scanner(Paths.get("ICPResult.txt"));
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
            System.out.println(ioe.toString());
            System.out.println(ioe.getMessage());
        }

推荐答案

代码看起来很好。



很高兴看到完整的异常消息,因为它们包含更多信息,如拒绝访问。



可能的错误源正在以另一个用户身份执行应用程序(用户不是允许访问文件)或文件被锁定(文件由另一个具有独占访问权限的应用程序打开)。
The code looks fine.

Would be good to see the full exception messages because they contain more information like "access denied".

Possible error sources are executing the application as another user (user is not allowed to access the file) or that the file is locked (file is opened by another application with exclusive access).


两种处理异常的方法:



two ways to handle exception:

public static void main(String[] args) throws IOException {
    Scanner in = new Scanner(Paths.get("C:\\Users\\longyuxi\\Documents\\ICPResult.txt"));
    System.out.println(in.nextLine());
    in.close();
  }





另一个例子:



Another example:

public static void main(String[] args) {
    Scanner in = null;
    String firstLine=null;
    try {
    in = new Scanner(Paths.get("C:\\Users\\longyuxi\\Documents\\ICPResult.txt"));
    firstLine=in.nextLine();
    in.close();
    } catch(IOException e) {
     System.err.println("File not found");
    }
   if(firstLine!=null) {
    System.out.println(firstLine);
   }
  }


这篇关于为什么这台扫描仪找不到它的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆