2013-12-21 47 views
-2

我的阅读程序吐出一个“FileNotFound”异常,有什么想法?java FileReader BufferedReader“FileNotFound”

代码段:

import java.io.FileReader; 

import java.io.BufferedReader; 

public class Main{ 

    public static void main (String [] args){ 


try{ 

BufferedReader br = new BufferedReader(new FileReader("file.txt")); 

    String[] input = new String[0]; 
String[] temp = new String[1]; 

for (int i = 0; (br.readLine()) != null; i++){ 

    for(int j = 0; j < (i); j++){ 

      temp[j] = input[j]; 

     } 

    temp[i] = br.readLine(); 
    input = new String [i + 1]; 

    for(int j = 0; j <= (i); j++){ 

      input[j] = temp[j]; 

    } 

    temp = new String[i + 2]; 
} 

br.close(); 

    } catch (IOException error1){ 
    System.out.println("Error 404: File Not Found"); 
} 

} 

现在代码可编译,但是吐出来的是“错误404:未找到文件”,虽然“file.txt”,但在同一个文件夹中的“类”文件。这是为什么?

+5

很简单,因为它无法找到该文件。文件路径是否有效? –

回答

2

创建的BufferedReader

System.out.println(new File(args[0]).getAbsolutePath()); 

之前添加以下语句,并检查outputed路径实际指向的文件系统和现有文件。

0

很难说,因为您没有提供文件路径或任何其他文件,而是尝试查找文件的绝对路径。

0

比方说,你有这样的

C:/ 
    My_IDE_Projex 
       ProjectRoot 
         bin 
          Main.class 
         src 
          Main.java 

的文件结构。如果您使用的是realtive路径“MYFILE.TXT”在命令行,如果你在命令行,无论编译你的java文件你指定了要编译的类文件,这就是你需要拥有这个文件的地方。例如:

C:\My_IDE_Projex\ProjectRoot\bin> java Main myFile.txt

该文件应在bin文件夹