Java阅读文本项目

时间:2017-06-16 12:29:58

标签: java

大家好我在java中创建一个项目来阅读文本文件,但它不起作用我希望你能帮助我

package read_file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Demo {


public static void main(String[] args) {
BufferedReader br= null;



try{
br= new BufferedReader (new FileReader("C:\\Users\\hp\\workspace\\MyFFFFProject"));


String line ; 

while ((line=br.readLine())!= null ) { 
    System.out.println(line);
}
}catch (IOException e){ 
e.printStackTrace();
}finally {
try{
    br.close();
}catch(IOException e){
    e.printStackTrace();
    }
}
}

}

在控制台中显示此消息

java.io.FileNotFoundException: C:\Users\hp\workspace\MyFFFFProject (Access is denied)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at read_file.Demo.main(Demo.java:11)

Exception in thread "main" java.lang.NullPointerException
at read_file.Demo.main(Demo.java:21)

我想要阅读的文件是NotePad ++包含的: -

this is my file content line 1
this is my file content line 2
this is my file content line 3
this is my file content line 4
this is my file content line 5
this is my file content line 6
this is my file content line 7

1 个答案:

答案 0 :(得分:0)

检查您的文件权限。您(或运行该程序的用户)对此特定文件没有读取权限。 我有一个linux盒子并尝试相同的代码,它的工作原理。我不得不给我任何读取权限并得到同样的错误。