GreenAddiction
GreenAddiction

Reputation: 17

add new line to file when appending

    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

Upvotes: 0

Views: 863

Answers (1)

greenPadawan
greenPadawan

Reputation: 1581

Try the BufferedWriter.newLine() method.

Upvotes: 2

Related Questions