Pages

Wednesday, December 1, 2021

Java Printwriter New Line

1. Overview

In this tutorial, we'll learn how to add the new file to the printwriter in java.

When you use the write() method of print writer then you do not see the new lines for each time you call write() method.

To fix this problem and print the new line \n for each time write the data into the printwriter, you need to use the println() method rather than using any other methods such as write() or print() methods.

If you are new to java, please read the java printwrite methods and exampls.

By end of this post, you will understand write() vs println() of printwriter.

Java Printwriter New Line



2. Java Printwriter New Line With write() method


First, we'll learn how to add \n new line to the output file or console using write() method.


Example 1
package com.javaprogramto.files.printwriter.newline;

import java.io.PrintWriter;

public class PrintWriterNewLine {

	public static void main(String[] args) {

		PrintWriter printWriter = new PrintWriter(System.out);

		printWriter.write("this is the first line");
		printWriter.write("\n");
		printWriter.write("this is the second line");
		printWriter.write("\n");
		printWriter.write("third line");

		printWriter.flush();

		printWriter.close();

	}
}

Output
this is the first line
this is the second line
third line

This example writes the contents into the console and we added \n newline into the write() method.

If we would not have given the \n then the output is as follows in a single line. By default, write() ignores the new line if we do not provide the explicitly in the text as "\n".

this is the first linethis is the second linethird line




3. Java Printwriter New Line With println() method


In the above section, we used write("\n") method with special character after every write() method with actual contents. This is difficult to manange the code and looks not good.

The recommended way is to add the new line is using println() method of PrintWriter class.

println() method internally adds the new line character to the contents.


Example 2

package com.javaprogramto.files.printwriter.newline;

import java.io.PrintWriter;

public class PrintWriterNewLine2 {

	public static void main(String[] args) {

		PrintWriter printWriter = new PrintWriter(System.out);

		printWriter.println("this is the first line");
		printWriter.println("this is the second line");
		printWriter.println("third line");

		printWriter.flush();

		printWriter.close();

	}
}

Output
this is the first line
this is the second line
third line


4. Java Printwriter New Line With print() method


If you are trying to add the html contents to the print writer then you need to add the explict break statements with tag <br \>.

Example 3

writer.print("hello"+"<br/>");
writer.print("hope you are doing good"+"<br/>");


5. Conclusion


in this article, we've seen how to add the new line to the printwriter when writing the contents to the console or file.




No comments:

Post a Comment

Please do not add any spam links in the comments section.