Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
The implementation of the writer class in Java is the PrintWriter Class. The formatted representation of objects is printed to a text output stream. Let us dig a bit deeper and understand the concept in detail. Here is the agenda for this article:
Starting off with the definition of PrintWriter class in Java!
The Java.io.PrintWriter class prints formatted representations of objects to a text-output stream. This class implements all the print methods that are found in printstream.
With this simple definition, let me show you the class declaration.
public class PrintWriter extends Writer
This class inherits methods from the following class −
● Java.io.Object
Now, the next segment will tell you the constructors used in the PrintWriter class.
Below is the list of constructors of PrintWriter class:
Constructor | Description |
PrintWriter(File file, String csn) | This constructor helps in creating a new PrintWriter with no automatic line flushing. It creates it with the specified file and charset. |
PrintWriter(OutputStream out, boolean autoFlush) | This constructor helps in creating a new PrintWriter from an already existing output stream. |
PrintWriter(OutputStream out) | it helps in creating a new PrintWriter from an existing OutputStream |
PrintWriter(String fileName, String csn) | It helps in creating a new PrintWriter, which specified file name and charset. |
PrintWriter(String fileName) | It creates a new PrintWriter with the specified file name without automatic line flushing. |
PrintWriter(Writer out) | It creates a new PrintWriter, without automatic line flushing. |
PrintWriter(Writer out, boolean autoFlush) | This creates a new PrintWriter. |
PrintWriter(File file) | It creates a new PrintWriter, without automatic line flushing, with the specified file. |
After understanding the constructors of this class, let us study the methods provided by the PrintWriter class.
Method | Description |
PrintWriter append(CharSequence csq) | It helps in appending the specified character sequence to this writer. |
PrintWriter append(CharSequence csq, int start, int end) | It helps in appending a subsequence of the specified character sequence to this writer. |
void close() | It closes the stream |
boolean checkError() | It closes the stream if it’s not closed and checks its error state. |
protected void clearError() | It Clears the error state of this stream. |
void flush() | It flushes the stream. |
PrintWriter format(String format, Object… args) | It writes a formatted string to this writer using the specified format string and arguments. |
PrintWriter format(Locale l, String format, Object… args) | This method writes a formatted string to this writer using the specified format string and arguments. |
void print(char c) | It prints a character. |
void print(float f) | It prints a floating-point number. |
void print(double d) | It prints a double-precision floating-point number. |
void print(boolean b) | It prints a boolean value. |
void print(int i) | It prints an integer. |
void print(long l) | It prints a long integer. |
void print(Object obj) | It prints an object. |
void print(String s) | This method prints a string. |
void println() | It terminates the current line by writing the line separator string. |
PrintWriter printf(String format, Object… args) | This is a convenience method to write a formatted string to this writer using the specified format string and arguments. |
PrintWriter printf(Locale l, String format, Object… args) | It writes a formatted string to this writer using the specified format string and arguments. |
void println(boolean x) | It prints a boolean value and then terminates the line. |
void println(char x) | It prints a character and then terminates the line. |
void println(char[] x) | It prints an array of characters and then terminates the line. |
void println(double x) | It prints a double-precision floating-point number and hence terminates the line. |
void println(long x) | It prints a long integer and then terminates the line. |
void println(int x) | It prints an integer and then terminates the line. |
void println(float x) | It prints a floating-point number and then terminates the line. |
void println(Object x) | It prints an Object and then terminates the line. |
void println(String x) | It prints a String and then terminates the line. |
void write(char[] buf) | It writes an array of characters. |
void write(char[] buf, int off, int len) | It writes a portion of an array of characters. |
protected void setError() | It indicates that an error has occurred. |
void write(int c) | It writes a single character. |
void write(String s) | It writes a string |
Now, let’s hop onto the implementation process
Example
Code:
import java.io.File; import java.io.PrintWriter; public class Example { public static void main(String[] args) throws Exception { //Data to write on Console using PrintWriter PrintWriter writer = new PrintWriter(System.out); writer.write("Welcome to Edureka!"); writer.flush(); writer.close(); //Data to write in File using PrintWriter PrintWriter writer1 =null; writer1 = new PrintWriter(new File("D:testout.txt")); writer1.write("Learn different technologies."); writer1.flush(); writer1.close(); } }
Output:
Learn different technologies.
With this, we have reached towards the end of this tutorial. I hope the concept is clear to you now. Keep reading, keep exploring!
If you found this article on “PrintWriter class in Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
If you come across any questions, feel free to ask all your questions in the comments section of “PrintWriter class in Java” and our team will be glad to answer.
edureka.co