Java: how to write formatted output to a text file

I am developing a small java application. At some point, I write some data in a text file. Using the following code:

Writer Candidateoutput = null; File Candidatefile = new File("Candidates.txt"), Candidateoutput = new BufferedWriter(new FileWriter(Candidatefile)); Candidateoutput.write("\n Write this text on next line"); Candidateoutput.write("\t This is indented text"); Candidateoutput.close(); 

Now everything is going well, the file is created with the expected text. The only problem is that the text was not formatted, all the text was on one line. But if I copy and paste text into MS Word, the text is automatically formatted.

Is there a way to save text formatting in a Plain text file?

Note. By text formatting, I mean only \n and \t

+4
source share
6 answers

Use System.getProperty("line.separator") for System.getProperty("line.separator") β€” this is a platform-independent way to get a newline separator. (on windows it \r\n , on linux it \n )

Also, if it will run on windowless machines, do not use \t - use X (four) spaces instead.

+4
source

You can use the system property line.separator to solve your problem.

eg.

 String separator = System.getProperty("line.separator"); Writer Candidateoutput = null; File Candidatefile = new File("Candidates.txt"), Candidateoutput = new BufferedWriter(new FileWriter(Candidatefile)); Candidateoutput.write(separator + " Write this text on next line"); Candidateoutput.write("\t This is indented text"); Candidateoutput.close(); 

line.separator system property is a platform-independent way of getting a new line from your environment.

+3
source

A PrintWriter makes this platform independent - use the println() methods.

+2
source

You will need the Java Formatter utility, which can be found here: java.util.Formatter

Then you just need to create an object of type Formatter , for example:

 private Formatter output; 

In this case, output will be the output file you are writing.

Then you must pass the file name to the output object as follows:

 output = new Formatter("name.of.your.file.txt") 

After that, you can either copy the contents of the file to the output file using the output.format , which is similar to the System.out.println or printf commands.

Or use the Scanner utility to enter data into memory, then use output.format to output this data to an output object or file.

This is an example of how to write a record for output:

 output.format( "%d %s %s %2f\n" , field1.decimal, field2.string, field3.string, field4.double) 

There is little more to it than that, but it certainly surpasses data parsing or the use of many complex third-party plugins.

To read this file, you redirect the Scanner utility to read the file instead of the console:

 input = new Scanner(new File( "name.of.your.file.txt") 
+2
source

Well, Windows expects the newline and carriage return char to indicate the newline. So you want to do this in order to make it work.

+1
source

The Notepad window needs \r\n to correctly display a new line. Only t21 is ignored by Notepad.

+1
source

All Articles