繁体   English   中英

PrintWriter未写入文本文件

[英]PrintWriter not writing to text file

我正在尝试创建一个保存功能,该功能将存储的数据输出到文本文件。 我尝试使用Printwriter写入文件,尽管我没有收到任何错误并且输出似乎正确,但文本文件仍然为空。 这是我的代码:

public void saveConfiguration() throws IOException{
    PrintWriter pw = new PrintWriter("locos.txt");

    for (int i = 0; i < currentTrains.size(); i++) {
        //confirm data is correct
        System.out.println(currentTrains.get(i).getAddress() + " " + 
                currentTrains.get(i).getName() + " " + "\n");

        //write to file
        pw.write(currentTrains.get(i).getAddress() + " " + 
                currentTrains.get(i).getName() + " " + "\n");
    }

    pw.close();

    //for testing
    System.out.println("File Saved");
}

这是控制台上的内容:

8 class 08 

55 Jinty 

44 BR44 

File Saved

上面打印出的数据是正确的,但没有写入文件中。 谁能解释如何正确执行此操作?

编辑:我不知道这是否相关,但是我正在Tomcat服务器上运行它。

您应该尝试处理PrintWriterFilerwriter

例:

PrintWriter pw = new PrintWriter(new FileWriter("locos.txt"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM