2017-06-16 49 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

你有沒有檢查過它? –