This should do it i think? It will append it to the End of the File.
public class yourClass
{
private BufferedWriter bufferedWriter;
public static void main(String[] args)
{
try
{
bufferedWriter = new BufferedWriter( new OutputStreamWriter(new FileOutputStream("file.txt"), StandardCharsets.UTF_8));
}
catch (FileNotFoundException e)
{
System.out.println("Couldn't initialize the BufferedWriter");
e.printStackTrace();
System.exit(0);
}
writeStuff("Hey");
writeStuff("Hey2");
writeStuff("Hey3");
writeStuff("Hey4");
writeStuff("Hey5");
flushAndClose(false);
writeStuff("Hey");
writeStuff("Hey2");
writeStuff("Hey3");
flushAndClose(true);
}
public void writeStuff(String toWrite)
{
bufferedWriter.write(toWrite + System.lineSeparator());
}
public void flushAndClose(boolean closeToo)
{
bufferedWriter.flush();
if(closeToo)
{
bufferedWriter.close();
}
}
}
Everytime you wan't to write something jsut call the writeStuff Method.
If this isn't what you wanted, I am sorry i missunderstood you.