Hi I'm trying to write to a text file with the class FileImageOutput Stream but I can't manage to overwrite the file I'm writing to. So I tried to flush the Object with the flush() method but it still doesn't work. Here is my code:
and here is the output to the test file
test.txt
what I want the textfile to look like after running the programm is the following
Code:
import java.io.File;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileWriter{
public static void main(String[] args){
try{
File file = new File("test.txt");
FileImageOutputStream fileWriter = new FileImageOutputStream(file);
for(int i=0;i<20;i++){
fileWriter.writeBytes("test this\n");
}
fileWriter.close();
FileImageOutputStream fileWriter2 = new FileImageOutputStream(file);
fileWriter2.flush();
fileWriter2.writeBytes("test\n");
}
catch(Exception e){
System.out.println("the file could not be opened");
}
}
}
test.txt
Code:
test this test this test this test this test this test this test this test this test this test this test this test this test this test this test this test this test this test this test this test this
Code:
test