如何读取文件?

时间:2014-04-18 18:59:06

标签: java io

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。

1 个答案:

答案 0 :(得分:0)

只做这个

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

但是将文件放在文件阅读器中。

希望它有所帮助。