[java] Java FileWriter how to write to next Line

I used the code below to write record into a File. The record can be written in the file, but is append in one Line, each time I call this method example:

Hello WorldHello WorldHello WorldHello World

How can I modify the code so the output look like below, so that when read the text I can use line.hasNextLine() to check?

Hello World
Hello World
Hello World
Hello World

        // Create file
        FileWriter fstream = new FileWriter(fileName, true);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(c.toString());
        //Close the output stream
        out.close();

        // Code I used to read record I am using | as a seperator name and id
        String fileName = folderPath + "listCatalogue.txt";
        String line = "";
        Scanner scanner;
        String name, id;
        scanner = new Scanner(fileName);
        System.out.println(scanner.hasNextLine());
        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            System.out.println(line);
            StringTokenizer st = new StringTokenizer(line, "|");
            name = st.nextToken();
            id = st.nextToken();
            catalogues.add(new Catalogue(name, id));
        }

This question is related to java file newline

The answer is


I'm not sure if I understood correctly, but is this what you mean?

out.write("this is line 1");
out.newLine();
out.write("this is line 2");
out.newLine();
...

You can call the method newLine() provided by java, to insert the new line in to a file.

For more refernce -http://download.oracle.com/javase/1.4.2/docs/api/java/io/BufferedWriter.html#newLine()


.newLine() is the best if your system property line.separator is proper . and sometime you don't want to change the property runtime . So alternative solution is appending \n


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to file

Gradle - Move a folder from ABC to XYZ Difference between opening a file in binary vs text Angular: How to download a file from HttpClient? Python error message io.UnsupportedOperation: not readable java.io.FileNotFoundException: class path resource cannot be opened because it does not exist Writing JSON object to a JSON file with fs.writeFileSync How to read/write files in .Net Core? How to write to a CSV line by line? Writing a dictionary to a text file? What are the pros and cons of parquet format compared to other formats?

Examples related to newline

How can I insert a line break into a <Text> component in React Native? Print "\n" or newline characters as part of the output on terminal Using tr to replace newline with space How to write one new line in Bitbucket markdown? Line break in SSRS expression How to insert a new line in Linux shell script? Replace CRLF using powershell How to write new line character to a file in Java What is the newline character in the C language: \r or \n? How to print values separated by spaces instead of new lines in Python 2.7