读取文件

public void test1() throws IOException {
        File file = new File("path");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String str = null;
        while ((str = br.readLine()) != null) {
            System.err.println(str);
        }

        br.close();
        fr.close();
    }

    public void test2() throws IOException {
        Path path = Paths.get("text.txt");
        List<String> fs = Files.readAllLines(path, StandardCharsets.UTF_8);
        for (String str : fs) {
            System.err.println(str);
        }
    }

    public void print(Object o) {
        String name = Thread.currentThread().getName();
        System.err.println(name + ":" + o);
    }
原文地址:https://www.cnblogs.com/jpit/p/7357603.html