Java IO PrintWriter
In this tutorial we will learn about the the PrintWriter class in Java.
java.io.PrintWriter class is used for format printing of objects. To print these objects to the text output stream all the print method of PrintStream class is implemented by this class. This class extends Writer class and writes the character data. However, to write on the console in Java System.out is provided, except that the PrintWriter is also provided to write on console. PrintWriter is a character based class which makes the Java program easy for Internationalization. Objects of this class can be created by using various of its Constructors.
Constructor Details
PrintWriter(File file) | This constructor is used to create a new PrintWriter object with the given file
name. Using this constructor output can be written to the file. This method
doesn't flushes line automatically. Syntax : public PrintWriter(File file) throws FileNotFoundException |
PrintWriter(File file, String csn) | This constructor is used to create a new PrintWriter object with the given file
name and charset. Using this constructor output can be written to the file with
the specified charset. This method doesn't flushes line automatically. Syntax : public PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException |
PrintWriter(OutputStream out) | This constructor is used to create a new PrintWriter object with the OutputStream
already existed. This constructor doesn't flushes line automatically. Syntax : public PrintWriter(OutputStream out) |
PrintWriter(OutputStream out, boolean autoFlush) | This method is used to create a new PrintWriter object with the OutputStream
already existed and a boolean value for auto flushing. Automatic flushing of line
based on the boolean value. Syntax : public PrintWriter(OutputStream out, boolean autoFlush) |
PrintWriter(String fileName) | This constructor creates a new PrintWriter object with the given file name.
Using this constructor output can be written to the file. This method doesn't
flushes line automatically. Syntax : public PrintWriter(String fileName) throws FileNotFoundException |
PrintWriter(String fileName, String csn) | This constructor creates a new PrintWriter object with the given file name
and specified charset. This method doesn't flushes the line automatically. Syntax : public PrintWriter(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException |
PrintWriter(Writer out) | This constructor is used to create a new PrintWriter object with an instance
of Writer (character-output stream). This method doesn't flushes the line automatically. Syntax : public PrintWriter(Writer out) |
PrintWriter(Writer out, boolean autoFlush) | This constructor creates a new PrintWriter object with an instance of Writer
(character-output stream)
and a boolean value for auto flushing. Automatic flushing of line based on the
boolean value. Syntax : public PrintWriter(Writer out, boolean autoFlush) |
Methods Detail
- append(char c) : This method is used to write the
characters with the existing characters into the writer.
Syntax : public PrintWriter append(char c)
- append(CharSequence csq) : This method is used to write the
sequence of characters with the existing characters into the
writer.
Syntax : append(CharSequence csq)
- append(CharSequence csq, int start, int end) : This method
is used to write the specified portion of a sequence of characters with the existing characters into the writer.
Syntax : public PrintWriter append(CharSequence csq, int start, int end)
- checkError() : This method returns a boolean value which
specifies the state of an error (whether the print stream has encountered error
or not) and if the stream is not closed then this method flushes the stream.
Syntax : public boolean checkError()
- clearError() : This method is used to clear the state of
error.
Syntax : protected void clearError()
- close() : This method is used to close the stream as well
as to release the resources associated with the stream.
Syntax : public void close()
- flush() : This method is used to flush the stream.
Syntax : public void flush()
- format(Locale l, String format, Object... args) : This
method is used to write a string, formatted by the given Locale, format string and
arguments, to the writer.
Syntax : public PrintWriter format(Locale l,String format, Object... args)
- format(String format, Object... args) : This method is used
to write a string, formatted by the given format string and arguments, to the
writer.
Syntax : public PrintWriter format(String format, Object... args)
- print(boolean b) : This method is used to print a boolean
value.
Syntax : public void print(boolean b)
- print(char c) : This method is used to print a character
value.
Syntax : public void print(char c)
- print(char[] s) : This method is used to print an array of
characters.
Syntax : public void print(char[] s)
- print(double d) : This method is used to print a
double-precision floating point number
value.
Syntax : public void print(double d)
- print(float f) : This method is used to print a float
value.
Syntax : public void print(float f)
- print(int i) : This method is used to print an integer
value.
Syntax : public void print(int i)
- print(long l) : This method is used to print long integer
value.
Syntax : public void print(long l)
- print(Object obj) : This method is used to print an Object.
Syntax : public void print(Object obj)
- print(String s) : This method is used to print a String.
Syntax : public void print(String s)
- printf(Locale l, String format, Object... args) : This
method is used to write a string, formatted by the given Locale, format string
and arguments, to the writer.
Syntax : public PrintWriter printf(Locale l, String format, Object... args)
- printf(String format, Object... args) : This method is used
to write a string, formatted by the given format string and arguments, to the
writer.
Syntax : public PrintWriter printf(String format, Object... args)
- println() : This method is used to write a new line into
the writer.
Syntax : public void println()
- println(boolean x) : Using this method a boolean value is
printed first and then the current line is terminated.
Syntax : public void println(boolean x)
- println(char x) : Using this method a character is printed
first and then the current line is terminated.
Syntax : public void println(char x)
- println(char[] x) : Using this method an array of
characters is printed first and then the current line is terminated.
Syntax : public void println(char[] x)
- println(double x) : Using this method double-precision
floating point number value is printed first and then the current line is
terminated.
Syntax : public void println(double x)
- println(float x) : Using this method floating point number
value is printed first and then the current line is terminated.
Syntax : public void println(float x)
- println(int x) : Using this method an integer is printed
first and then the current line is terminated.
Syntax : public void println(int x)
- println(long x) : Using this method long integer value is
printed first and then the current line is terminated.
Syntax : public void println(long x)
- println(Object x) : Using this method an Object is printed
first and then the current line is terminated.
Syntax : public void println(Object x)
- println(String x) : Using this method string is printed
first and then the current line is terminated.
Syntax : public void println(String x)
- setError() : This method shows the occurrence of an error.
Syntax : protected void setError()
- write(char[] buf) : This method is used to write
caharacter's array.
Syntax : public void write(char[] buf)
- write(char[] buf, int off, int len) : This method is used
to write specified part of an array of characters started from the offset off.
Syntax : public void write(char[] buf, int off, int len)
- write(int c) : This method is used to write a character.
Syntax : public void write(int c)
- write(String s) : This method is used to write a string.
Syntax : public void write(String s)
- write(String s, int off, int len) : This method is used to
write a specified length of string started from the specified offset.
Syntax : public void write(String s, int off, int len)
Example
Here I am giving a simple example which will demonstrate you about how to use the PrintWriter in the Java applications. In this example I have created a PrintWriter object using the constructor PrintWriter(File file) and various of methods to print/write the characters to the file.
Source Code
JavaPrintWriterExample.java
import java.io.PrintWriter; import java.io.File; import java.util.Locale; public class JavaPrintWriterExample { public static void main(String[] args) { String s = "PrintWriter"; int i = 50; try { File file = new File("file.txt"); // create a new PrintWriter with the specified file PrintWriter pw = new PrintWriter(file); // format text with specified locale. // %s indicates a string will be placed there, which is s pw.format(Locale.US, "This is a %s example", s); // usage line.separator pw.println(); // write integer pw.println(i); // write character pw.write(i); // usage line.separator pw.println(); // format text with specified locale // %d indicates a integer will be placed there, which is 100 pw.format("This is a %s example with %d", s, i); System.out.println(); System.out.println("Data written to the file successfully"); // flush the writer pw.flush(); PrintWriter pw1 = new PrintWriter(System.out); pw1.println(); pw1.println("Data on console : "); pw1.format(Locale.US, "This is a %s example", s); pw1.println(); pw1.println(i); pw1.write(i); pw1.println(); pw1.format("This is a %s example with %d", s, i); pw1.println(); pw1.flush(); } catch (Exception ex) { System.out.println(ex); } } }
Output
When you will execute the above example you will get the output as follows :
Source code of this example can also be downloaded from the link given below.