0
    public void writeToFile(){
    try{
        String data = "\n" +" This content will append to the end of the file";

        File file =new File("questions.txt");

        //if file doesnt exists, then create it
        if(!file.exists()){
            file.createNewFile();
        }

        //true = append file
        FileWriter fileWritter = new FileWriter(file.getName(),true);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);

            bufferWritter.write(data);

            bufferWritter.close();

        System.out.println("Done");

    }catch(IOException e){
        e.printStackTrace();
    }
}

I have the following code above, this was taken off the internet, I am able to correctly add data to the file specified however, I need to make it so that it adds data on a new line. Instead of appending to the current last line

  • Instead of `"\n"` use `System.getProperty("line.separator")` or Java 7 - `System.lineSeparator()`. Follow this [post](http://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character) – Braj Jan 15 '16 at 05:51
  • You don't need the `exists()/createNewFile()` part. This is just wasteful. `new FileOutputStream(...)` already does everything necessary in that line. Don't repeat work the system has to do anyway. – user207421 Jan 15 '16 at 06:26

1 Answers1

2

Try the BufferedWriter.newLine() method.

user207421
  • 305,947
  • 44
  • 307
  • 483
greenPadawan
  • 1,511
  • 3
  • 17
  • 30