2017-06-16 53 views
-2

嗨,大家好我做一个项目,在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) 
控制台

我想读的文件是记事本++包含有: -

 
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 
+2

例外说“(访问被拒绝)”...该文件是否在其他应用程序中打开,如Notepad ++? –

+0

你可以使用'Files.readLines'并传递'Path'对象。 – Xry

+1

你确定'MyFFFFProject'是一个文件而不是文件夹吗? –

回答

0

检查文件permissions.Seems你(或者是谁运行的用户该程序)没有读取这个特定文件的权限。 我有一个Linux的盒子,并尝试相同的代码,它的工作。我不得不对我没有读取权限,并得到同样的错误。

+0

你有没有检查过它? –