why is this file empty? this code should populate it with stuff

后端 未结 2 955
小蘑菇
小蘑菇 2021-01-25 21:47
package stuff;

import java.io.IOException;

public class DataWriter {
    public static void main(String[] args) throws  IOException {
        java.io.File file = new j         


        
相关标签:
2条回答
  • 2021-01-25 22:12

    your not closing the output variable

    java.io.File file = new java.io.File("mydata.txt");
            if(file.exists()) {
                System.out.println("file already exists");
                System.exit(1);
    
            }
             java.io.PrintWriter output = new java.io.PrintWriter(file);
            output.println("data1");
            output.println("data2");
            output.println("data3");
            output.println("data4");
            output.println("data5");
            output.println("data6");
            output.println(" data7");
            output.close();          //close<--------------------------------
            System.out.println("data8");
    
    0 讨论(0)
  • 2021-01-25 22:27

    You need to close the file with:

    output.close();
    
    0 讨论(0)
提交回复
热议问题