文本文件写入不起作用

时间:2018-04-25 16:58:57

标签: java output

我一直在尝试为输出而不是System.out.println()写入文本文件。但是,当我尝试这个时,似乎没有任何东西可写。我的代码有什么问题?

    try{
        List<String> lines = Arrays.asList("Data Goes Here");
        Path file = Paths.get("output.txt");
        Files.write(file, lines, Charset.forName("UTF-8"));
        }

    catch (IOException ex) {
        System.out.println("Frick, something broke. Sorry folks, go home.");
        }

1 个答案:

答案 0 :(得分:-1)

我只是通过将路径作为位于项目根目录中的资源目录进行了一些小的更改。我能够成功写入文件。

以下是更新后的代码:

    try {
        List<String> lines = Arrays.asList("Data Goes Here");
        Path file = Paths.get("./resources/test.txt");
        Files.write(file, lines, Charset.forName("UTF-8"));
    }

    catch (IOException ex) {
        ex.printStackTrace();
        System.out.println("Frick, something broke. Sorry folks, go home.");
    }
相关问题