【问题标题】:writing text in a file, in java在文件中写入文本,在java中
【发布时间】:2022-12-12 19:52:45
【问题描述】:

您好我想在文件中写入文本,但是每次运行代码时,程序都会删除文件中私下写入的任何文本,并将文本文件留空,但我不明白为什么。

public static void output() throws IOException
    {
        PrintWriter write_file = new PrintWriter (new FileWriter("input_text_file.txt"));


        write_file.println("this text is written in the file");
        System.out.println("done");
    }

任何人都可以帮我找到代码错误的地方吗?

【问题讨论】:

    标签: java io writefile


    【解决方案1】:

    您好,您需要在最后关闭文件:

    write_file.close();

    【讨论】:

      【解决方案2】:

      以附加模式而不是截断模式打开文件

      PrintWriter writeFile = 
        new PrintWriter(new FileOutputStream(new File("input_text_file.txt"), true));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多