时间:2019-07-08 14:08:18

标签: java

我希望能够在输出终端中显示.txt文件的内容(使用System.out.print()命令)。

我已经尝试过fileInputStream / outputStream,但是我没有正确使用它,或者它没有按照我的预期工作。

目标是使用相同的换行符等在我的文件中显示内容...

1 个答案:

答案 0 :(得分:0)

只需使用扫描仪(或阅读器)读取文本文件的每一行,然后将其打印出来即可。

    String path = "testFilePath.txt";
    try (Scanner scanner = new Scanner(new File(path))) {
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    } catch(Exception e) {
        e.printStackTrace();
    }