简体   繁体   English

在OS / X上用Java打开文件

[英]Opening a file in Java on OS/X

The code snippet below causes this error: 下面的代码片段导致此错误:

 Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type FileNotFoundException 
String path = "/Users/jfaig/Documents/workspace/my_array/";
BufferedReader in = new BufferedReader(new FileReader(path + "Matrix.txt"));

The path is valid because I can see the file listed with this code: 该路径有效,因为我可以看到此代码列出的文件:

File dir = new File(path);
String[] chld = dir.list();
if(chld == null){
    System.out.println("Specified directory does not exist or is not a directory.");
    System.exit(0);
} else {
    for(int i = 0; i < chld.length; i++){
        String fileName = chld[i];
        System.out.println(fileName);
    }
}   

I reviewed many articles about OS/X paths in Java and none solved my problem. 我查看了许多有关Java中OS / X路径的文章,但都没有解决我的问题。 I'm going to try it on a Windows PC to see if the problem is particular to OS/X and/or my installation of Eclipse. 我将在Windows PC上尝试该问题,以查看问题是否特定于OS / X和/或我的Eclipse安装。

FileNotFoundException is a checked exception and needs to be handled. FileNotFoundException是已检查的异常,需要进行处理。 It has nothing to do with the file or path. 它与文件或路径无关。

http://www.hostitwise.com/java/java_io.html http://www.hostitwise.com/java/java_io.html

Its not complaining about your file but asking you to put the handling in place if the file is not found. 它不是在抱怨您的文件,而是要求您在找不到文件的情况下进行处理。

If you don't want any handling done for this scenario, update your method signature with throws FileNotFoundException eg for main method, you can write as below: 如果您不希望在这种情况下进行任何处理,请使用throws FileNotFoundException更新您的方法签名,例如对于main方法,您可以编写如下:

 public static void main(String[] args) throws FileNotFoundExcetion {

If you want to handle the exception, or wrap the above code in a try{}catch(FileNotFoundException fnfe){} block as below: 如果要处理该异常,或将以上代码包装在try{}catch(FileNotFoundException fnfe){}块中,如下所示:

try{
   File dir = new File(path);
   String[] chld = dir.list();
   if(chld == null){
   System.out.println("Specified directory does not exist or is not a directory.");
    System.exit(0);
   } else {
    for(int i = 0; i < chld.length; i++){
      String fileName = chld[i];
      System.out.println(fileName);
    }
   }   
  }catch(FileNotFoundException fnfe){  
    //log error
    /System.out.println("File not found");
 }          

java.lang.Error: Unresolved compilation problem: means that the real error is listed during the output of javac , but it repeats it here for you. java.lang.Error: Unresolved compilation problem:意味着真正的错误在javac的输出过程中列出,但在此为您重复。 Unhandled exception type FileNotFoundException — exceptions in Java must be explicitly caught, or re-thrown. Unhandled exception type FileNotFoundException必须明确捕获或重新抛出Java中的异常。

Let Java do the file separators with the path and use File constructor with two arguments. 让Java使用路径进行文件分隔符,并使用带有两个参数的File构造函数。

 File path = new File("/Users/jfaig/Documents/workspace/my_array");
 File file = new File(path, "Matrix.txt");
 System.out.println("file exists=" + file.exists()); // debug
 BufferedReader in = new BufferedReader(new FileReader(file));

And as mentioned previously you need to catch or have the method throw IOException. 如前所述,您需要捕获或让方法抛出IOException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM