2014-04-18 69 views
-3
private void getInput() throws IOException { 

    InputStream resourceAsStream = this.getClass().getResourceAsStream(
      "aaa.txt"); 
    BufferedReader br = new BufferedReader(new FileReader(
      resourceAsStream.toString())); 
    try { 
     StringBuilder sb = new StringBuilder(); 
     String line = br.readLine(); 

     while (line != null) { 
      sb.append(line); 
      sb.append(System.lineSeparator()); 
      line = br.readLine(); 
     } 
     String everything = sb.toString(); 
     System.out.println(everything); 
    } finally { 
     br.close(); 
    } 
} 

文件aaa.txt在同一個項目中,它不能被讀取,當我執行它時,我有java.lang.NullPointerException。如何讀取文件?

+0

可能重複的[讀/將InputStream轉換爲字符串](http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string) –

+1

在哪一行你會得到例外? – mok

+0

BufferedReader br = new BufferedReader(new FileReader( \t \t \t \t resourceAsStream.toString()));這裏 – user3503255

回答

0

只有做到這一點

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

但是把文件中的FileReader來代替。

希望它能幫上忙。