1. Overview
In this tutorial, We'll learn how to use PrintWriter class in java with examples.
PrintWriter class is present in a package of java.io and PrintWriter class is an implementation of Writer class.
public class PrintWriter extends Writer
PrintWriter class is used to store or print the formatted representation of the object to the next output stream.
This class implements all methods from PirntStream class.
None of the methods from PrintWriter class does not throw IOException. But some of the contractors may throw IO Exceptions.
To know the errors are present or not, we need to use the checkError() method.
It is not possible to use PrintWriter with Java 8 Streams.
2. Java PrintWriter Constructors
The below are the constructors of PrintWriter class.
2.1 PrintWriter(File file) throws FileNotFoundException
Creates a new PrintWriter, without automatic line flushing, with the specified file.
2.2 PrintWriter(File file, String csn) throws FileNotFoundException,UnsupportedEncodingException
Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
2.3 PrintWriter(OutputStream out)
Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
2.4 PrintWriter(OutputStream out, boolean autoFlush)
Creates a new PrintWriter from an existing OutputStream.
2.5 PrintWriter(String fileName)
Creates a new PrintWriter, without automatic line flushing, with the specified file name.
2.6 PrintWriter(String fileName, String csn)
Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
2.7 PrintWriter(Writer out)
Creates a new PrintWriter, without automatic line flushing.
2.8 PrintWriter(Writer out, boolean autoFlush)
Creates a new PrintWriter.
3. Java PrintWriter Methods
Below are 36 useful methods of PrintWriter class.
3.1 PrintWriter append(char c)
Appends the specified character to this writer.
3.2 PrintWriter append(CharSequence csq)
Appends the specified character sequence to this writer.
3.3 PrintWriter append(CharSequence csq, int start, int end)
Appends a subsequence of the specified character sequence to this writer.
3.4 boolean checkError()
Flushes the stream if it's not closed and checks its error state.
3.5 protected void clearError()
Clears the error state of this stream.
3.6 void close()
Closes the stream and releases any system resources associated with it.
3.7 void flush()
Flushes the stream.
3.8 PrintWriter format(Locale l, String format, Object... args)
Writes a formatted string to this writer using the specified format string and arguments.
3.9 PrintWriter format(String format, Object... args)
Writes a formatted string to this writer using the specified format string and arguments.
3.10 void print(boolean b)
Prints a boolean value.
3.11 void print(char c)
Prints a character.
3.12 void print(char[] s)
Prints an array of characters.
3.13 void print(double d)
Prints a double-precision floating-point number.
3.14 void print(float f)
Prints a floating-point number.
3.15 void print(int i)
Prints an integer.
3.16 void print(long l)
Prints a long integer.
3.17 void print(Object obj)
Prints an object.
3.18 void print(String s)
Prints a string.
3.19 PrintWriter printf(Locale l, String format, Object... args)
A convenient method to write a formatted string to this writer using the specified format string and arguments.
3.20 PrintWriter printf(String format, Object... args)
A convenient method to write a formatted string to this writer using the specified format string and arguments.
3.21 void println()
Terminates the current line by writing the line separator string.
3.22 void println(boolean x)
Prints a boolean value and then terminates the line.
3.23 void println(char x)
Prints a character and then terminates the line.
3.24 void println(char[] x)
Prints an array of characters and then terminates the line.
3.25 void println(double x)
Prints a double-precision floating-point number and then terminates the line.
3.26 void println(float x)
Prints a floating-point number and then terminates the line.
3.27 void println(int x)
Prints an integer and then terminates the line.
3.28 void println(long x)
Prints a long integer and then terminates the line.
3.29 void println(Object x)
Prints an Object and then terminates the line.
3.30 void println(String x)
Prints a String and then terminates the line.
3.31 protected void setError()
Indicates that an error has occurred.
3.32 void write(char[] buf)
Writes an array of characters.
3.33 void write(char[] buf, int off, int len)
Writes A Portion of an array of characters.
3.34 void write(int c)
Writes a single character.
3.35 void write(String s)
Writes a string.
3.36 void write(String s, int off, int len)
Writes a portion of a string.
4. Java PrintWriter Example 1 - Writing To Console
The below is a simple program that writes the contents into the console with the help of PrintWriter.write() method and System.out.
Example 1
package com.javaprogramto.files.printwriter; import java.io.PrintWriter; public class PrintWriterExample1 { public static void main(String[] args) { PrintWriter writer = new PrintWriter(System.out); writer.write("First line of console "); writer.write("Second line of console"); writer.flush(); boolean hasError = writer.checkError(); System.out.println(" any error in processing - "+hasError); writer.close(); } }
Output
First line of console Second line of console any error in processing - false
5. Java PrintWriter Example 2 - Writing To File
Next, we will write a program to write the contents into the File rather than showing it on the console. This is very useful to store the files and for backup purposes of old contents.
Example 2
package com.javaprogramto.files.printwriter; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; public class PrintWriterExample2 { public static void main(String[] args) { PrintWriter writer = null; try { writer = new PrintWriter(new File("src/main/resources/print_writer/test.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } writer.write("First line in file "); writer.write("Second line in file"); writer.flush(); boolean hasError = writer.checkError(); System.out.println("any error in storing in file - " + hasError); writer.close(); } }
Output
any error in storing in file - false
File output
6. Conclusion
In this article, we have seen how to use PrintWriter to write the objects into the console or into the file.
No comments:
Post a Comment
Please do not add any spam links in the comments section.