找不到.txt文件?

时间:2014-08-26 23:03:08

标签: java file bufferedreader filereader

我无法找到我想要阅读的.txt文件。

以下是代码:

public void read(){

    try {
        File file = new File("las");
        String path = file.getAbsolutePath();
        System.out.println(path);
        BufferedReader reader = new BufferedReader(new FileReader("/Users/Asus/workspace/testhitodit/las.txt"));
        String line = reader.readLine();
        while(line != null) {
            System.out.println(line);
            line = reader.readLine();
            }
        reader.close();
    }catch(FileNotFoundException e) {
    System.out.println("File not found");
    }catch(IOException e) {
    System.out.println("SOmething went wrong");
    }
}

这是打印出来的:

C:\Users\Asus\workspace\testhitodit\las

找不到文件

我在该文件夹中有一个名为las.txt的文件。

为什么它不起作用?

1 个答案:

答案 0 :(得分:1)

   //include the file extension
   File file = new File("las.txt");
   String path = file.getAbsolutePath();
   System.out.println(path);

   //pass the file you created to your file reader
   BufferedReader reader = new BufferedReader(new FileReader(file));