I am trying to write strings to a file but all the strings are being written to the file once the script is terminated.
To make my problem clear, in the below code I want to write a single string to my files per second and my file should be updated each second. Please help I am new to java.
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException {
String[] list = "Should write each word in each iteration".split("\\s");
for(String word:list){
Writer obj = new Writer();
obj.func(word);
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}
}
class Writer{
public void func(String word) throws IOException{
FileWriter writer = new FileWriter("file" , true);
writer.write(word);
writer.close();
}
}