在OS/X上用Java打开文件

在OS/X上用Java打开文件,java,eclipse,file-io,Java,Eclipse,File Io,以下代码段导致此错误: 路径是有效的,因为我可以看到与以下代码一起列出的文件: 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 &l

以下代码段导致此错误:

路径是有效的,因为我可以看到与以下代码一起列出的文件:

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);
    }
}   

我查阅了许多关于Java中OS/X路径的文章,没有一篇解决了我的问题。我将在Windows PC上试用它,看看这个问题是否是OS/X和/或我的Eclipse安装所特有的。

FileNotFoundException是一个选中的异常,需要处理。它与文件或路径无关


FileNotFoundException是一个已检查的异常,需要处理。它与文件或路径无关

Error:未解决的编译问题:意味着在javac输出过程中列出了真正的错误,但它会在这里为您重复。未处理的异常类型FileNotFoundException-必须显式捕获或重新抛出Java中的异常。

Java.lang.Error:未解决的编译问题:表示在javac输出期间列出了真正的错误,但它会在此处为您重复。未处理的异常类型FileNotFoundException-必须显式捕获或重新抛出Java中的异常。

这不是抱怨文件,而是要求您在找不到文件时进行处理

如果您不想对此场景进行任何处理,请使用throws FileNotFoundException更新您的方法签名,例如,对于main方法,您可以编写如下内容:

 public static void main(String[] args) throws FileNotFoundExcetion {
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");
 }          
如果要处理异常,或将上述代码包装在try{}catchFileNotFoundException fnfe{}块中,如下所示:

 public static void main(String[] args) throws FileNotFoundExcetion {
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");
 }          
它不是抱怨你的文件,而是要求你在没有找到文件的情况下进行处理

如果您不想对此场景进行任何处理,请使用throws FileNotFoundException更新您的方法签名,例如,对于main方法,您可以编写如下内容:

 public static void main(String[] args) throws FileNotFoundExcetion {
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");
 }          
如果要处理异常,或将上述代码包装在try{}catchFileNotFoundException fnfe{}块中,如下所示:

 public static void main(String[] args) throws FileNotFoundExcetion {
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使用路径执行文件分隔符,并使用带有两个参数的文件构造函数

 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));

如前所述,您需要捕获或让方法抛出IOException。

让Java使用路径来执行文件分隔符,并使用带有两个参数的文件构造函数

 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));
如前所述,您需要捕获或让方法抛出IOException