Java write to file. Using a loop

后端 未结 6 1729
不知归路
不知归路 2021-01-25 00:53

I have a simple application that yet would trash a text file (it\'s just practice) I\'m only 3 days with Java yet. Problem is there are no errors until you run the program then

6条回答
  •  没有蜡笔的小新
    2021-01-25 01:45

    Actual writing while being in a loop will not take place unless you call file.flush() method.

    FileWriter fileWriter = new FileWriter("hello.txt");
    try {
        for (int i = 0; i < 10; i++) {
             fileWriter.write(line);
             fileWriter.flush();
        }
    } finally {
         fileWriter.close();
    }
    

提交回复
热议问题