-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。如何讀取文件?
可能重複的[讀/將InputStream轉換爲字符串](http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string) –
在哪一行你會得到例外? – mok
BufferedReader br = new BufferedReader(new FileReader( \t \t \t \t resourceAsStream.toString()));這裏 – user3503255