Introduction to Java PrintWriter
The java PrintWriter class is used to print objects in the formatted representation to the text output stream. The PrintWriter class is a built in class in java that defines the java.io.PrintWriter package. The Writer class is a super class of the PrintWriter. The PrintWriter class enables to write the formatted object to the underlying Writer class, for example to write int, double, long and other primitive object or data as in text, not in the bytes values. The PrintWriter class implements all the print method as the PrintStream except the methods which writes the raw bytes. As the PrintWriter class intended to write text so it is useful to generate reports where we need to mix the numbers and text.
Syntax:
The following is the declaration for java.io. PrintWriter class:
public class PrintWriter extends Writer
{
// Constructors and methods of the PrintWriter class
}
The above is the syntax of the PrintWriter where it is extends to the Writer class.
Constructors of Java PrintWriter Class
Given below are the constructors mentioned:
- PrintWriter(File file): This constructs create the new instance of PrintWriter, with the specified file and which is not automat line flushing.
- PrintWriter(File file, String ch): This constructs create the new instance of PrintWriter, with the specified file, charset and which is not automat line flush.
- PrintWriter(OutputStream out): This constructs create the new instance of PrintWriter from an existing OutputStream and which is not automat line flush.
- PrintWriter(OutputStream out, booleanautoFlush): This constructs create the new instance of PrintWriter from an existing OutputStream.
- PrintWriter(String fName): This constructs create the new instance of PrintWriter with the specified file name and which is not automat line flush.
- PrintWriter(String fileName, String csn): This constructs create the new instance of PrintWriter with the specified file name and charset and it is not automat line flush.
- PrintWriter(Writer out): This constructs create the new instance of PrintWriter which is not automat line flush.
- PrintWriter(Writer out, booleanautoFlush): This constructs create the new instance of PrintWriter.
Methods of Java PrintWriter Class
Given below are the methods:
- public void print( Object obj): This method prints an object.
- public void println(boolean x): This method prints the boolean value.
- public void println(char[] x): This method prints an array of characters.
- public void println(int x): This method prints an integer. Similar methods to prints for all different primitive objects.
- public PrintWriterappend(char ch): This method appends the pass character to the writer.
- public PrintWriterappend(CharSequencechseq): This method appends the specified character sequence to the writer.
- public PrintWriterappend(CharSequencech, int start, int end): This method appends a subsequence of pass character to the writer.
- public booleancheckError(): This method checks its error state and flushes the stream.
- public protected void clearError(): This method clears an internal error state of the stream.
- public protected void setError(): This method indicates that an error occurs.
- public PrintWriterformat(String format, Object..args): This method writes the formatted string to this writer with the passed format string and arguments.
- public void flush(): This method flushes the stream.
- public void close(): This method closes the stream.
Examples of Java PrintWriter
Given below are the examples mentioned:
Example #1
Here we create an PrintWriter object by using the PrintWriter class constructor and pass the file name to write in it.
Code:
package p1;
importjava.io.File;
importjava.io.PrintWriter;
publicclass Demo
{
publicstaticvoidmain( String[] arg) {
try {
//create the new instance of PrintWriter with the specified file
PrintWriter pw =null;
pw = newPrintWriter(new File("D:\\data.txt"));
pw.write("This ia an example text for PrintWriter."+"\n");
pw.write("This ia an example number for PrintWriter :" +(int)563+ ".");
pw.flush();
pw.close();
System.out.println("Print writer done. you can open the file.");
}catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
When we open the file, we can see the content of the file as below:
4.8 (7,964 ratings)
View Course
As in the above code the file “data.txt” is opened to write some data with the help of PrintWriter class and its constructor.
Example #2
Here we create an PrintWriter object of a console that is System.out by using the PrintWriter class constructor and write on the console.
Code:
package p1;
importjava.io.File;
importjava.io.PrintWriter;
importjava.util.Locale;
publicclass Demo
{
publicstaticvoidmain( String[] arg) {
String str="Hello";
String str2=" World";
charch='H';
try {
//create the new instance of PrintWriter to System.out
PrintWriter pw =null;
pw = newPrintWriter(System.out);
pw.print("print(inti) : "+ 123);
pw.println();
pw.print("print(float f) : " +34.65f);
pw.println();
pw.print("print(boolean b) : " +true);
pw.println();
pw.print("print(char ch) : "+ch);
pw.println();
pw.print("print(String s) : "+str);
pw.println();
pw.print("print(Object Obj) : " +pw);
pw.println();
pw.print("append(CharSequencecsq) : ");
pw.append(str);
pw.append(str2);
pw.println();
pw.println("checkError() : " +pw.checkError());
pw.print("format() : ");
pw.format(Locale.CHINA, " This is an china formatted example for %s text.",str);
pw.flush();
pw.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
Output:
As in the above code, writing some of the data which we can see in the output to the out console (System.out) with the help of PrintWriter class constructor and methods.
Conclusion
The PrintWriter class is a built in class in java that is define in the java.io.PrintWriter package and it is used to print an object in the formatted representation to the text output stream. The PrintWriter class is the subclass of Writer class.
Recommended Articles
This is a guide to Java PrintWriter. Here we discuss the introduction to java PrintWriter, constructors, methods and examples for better understanding. You may also have a look at the following articles to learn more –