在OS/X上用Java打开文件

1 java eclipse file-io

下面的代码段会导致此错误:

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

该路径有效,因为我可以看到使用此代码列出的文件:

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);
    }
}   
Run Code Online (Sandbox Code Playgroud)

我在Java中回顾了许多关于OS/X路径的文章,但没有解决我的问题.我将在Windows PC上尝试它,看看问题是否特定于OS/X和/或我的Eclipse安装.

smk*_*smk 6

FileNotFoundException是一个经过检查的异常,需要进行处理.它与文件或路径无关.

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