Java.io.PrintWriter class in Java | Set 2 Last Updated : 12 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Java.io.PrintWriter class in Java | Set 1 More methods: PrintWriter printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this writer using the specified format string and arguments. Syntax :public PrintWriter printf(Locale l, String format, Object... args) Parameters: l - The locale to apply during formatting. If l is null then no localization is applied. format - A format string as described in Format string syntax args - Arguments referenced by the format specifiers in the format string. Returns: This writer Throws: IllegalFormatException NullPointerException Java import java.io.*; import java.util.Locale; //Java program to demonstrate printf method public class PrintWriterDemo { public static void main(String[] args) { String s = "for"; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // printf this string pr.printf(Locale.CANADA, "Geeks%sGeeks", s); // flush the stream pr.flush(); } } Output: GeeksforGeeks PrintWriter printf(String format, Object... args) : A convenient method to write a formatted string to this writer using the specified format string and arguments. Syntax :public PrintWriter printf(String format, Object... args) Parameters: format - A format string as described in Format string syntax args - Arguments referenced by the format specifiers in the format string. Returns: This writer Throws: IllegalFormatException NullPointerException Java import java.io.*; //Java program to demonstrate printf(String format, Object... args) method public class PrintWriterDemo { public static void main(String[] args) { String s = "for"; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // printf this string pr.printf("Geeks%sGeeks", s); // flush the stream pr.flush(); } } Output: GeeksforGeeks void println(): Terminates the current line by writing the line separator string. Syntax :public void println() Java import java.io.*; //Java program to demonstrate println() method public class PrintWriterDemo { public static void main(String[] args) { String s = "GeeksforGeeks"; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // printf this string pr.printf("%s", s); // flush the stream pr.flush(); } } Output: GeeksforGeeks void println(boolean x): Prints a boolean and then terminate the line. Syntax :public void println(boolean x) Java import java.io.*; //Java program to demonstrate println(boolean) method public class PrintWriterDemo { public static void main(String[] args) { // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // print a boolean and change line pr.println(true); pr.print("New Line"); // flush the stream pr.flush(); } } Output: true New Line void println(char x): Prints a character and then terminate the line. Syntax :public void println(char x) Java import java.io.*; //Java program to demonstrate println(char x) method public class PrintWriterDemo { public static void main(String[] args) { char c = 'a'; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // print a char and change line pr.println(c); pr.println('b'); // flush the stream pr.flush(); } } Output: a b void println(char[] x): Prints an array of characters and then terminate the line. Syntax :public void println(char[] x) Java import java.io.*; //Java program to demonstrate println(char[] x) method public class PrintWriterDemo { public static void main(String[] args) { char[] c = {'a', 'b', 'c'}; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // print an array and change line pr.println(c); // flush the stream pr.flush(); } } Output: abc void println(double x): Prints a double and then terminate the line. Syntax :public void println(double x) Java import java.io.*; //Java program to demonstrate println(double x) method public class PrintWriterDemo { public static void main(String[] args) { double c = 176.348; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // print a double and change line pr.println(c); // flush the stream pr.flush(); } } Output: 176.348 void println(float x): Prints a float and then terminate the line. Syntax :public void println(float x) Java //Java program to demonstrate println(float x) method import java.io.*; public class PrintWriterDemo { public static void main(String[] args) { float c = 5.76348f; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // print a float and change line pr.println(c); // flush the stream pr.flush(); } } Output: 5.76348 void println(int x): Prints an integer and then terminate the line. Syntax :public void println(boolean x) Java import java.io.*; //Java program to demonstrate println(int x) method public class PrintWriterDemo { public static void main(String[] args) { int c = 15; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // print an integer and change line pr.println(c); // flush the stream pr.flush(); } } Output: 15 void println(long x): Prints a long integer and then terminate the line. Syntax :public void println(long x) Java import java.io.*; //Java program to demonstrate println(long x) method public class PrintWriterDemo { public static void main(String[] args) { long c = 1252344125l; try { // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // print a long and change line pr.println(c); // flush the stream pr.flush(); } catch (Exception ex) { ex.printStackTrace(); } } } Output: 1252344125 void println(Object x) :Prints an Object and then terminate the line. Syntax :public void println(Object x) Java import java.io.*; //Java program to demonstrate println(Object x) method public class PrintWriterDemo { public static void main(String[] args) { Object c = 10; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // print an object and change line pr.println(c); // flush the stream pr.flush(); } } Output: 10 void println(String x) :Â Prints a String and then terminate the line. Syntax :public void println(boolean x) Java import java.io.*; //Java program to demonstrate println(String x) method public class PrintWriterDemo { public static void main(String[] args) { String c = "GeeksforGeeks"; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // print a string and change line pr.println(c); // flush the stream pr.flush(); } } Output: GeeksforGeeks protected void setError() :Â Sets the error state of the stream to true. Syntax :public void println(String x) Java import java.io.*; //Java program to demonstrate setError() method public class PrintWriterDemo extends PrintWriter { public PrintWriterDemo(OutputStream out) { super(out); } public static void main(String[] args) { char c[] = {70, 71, 72, 73, 74, 75, 76}; // create PrintWriter object PrintWriterDemo pr= new PrintWriterDemo(System.out); // write char 1-3 pr.write(c, 1, 3); // flush the stream pr.flush(); // set an internal error pr.setError(); } } Output: GHI void write(char [] buf, int off, int len) :Â Writes len char from the specified char array starting at offset off to this stream. Syntax :public void write(char [] buf, int off, int len) Overrides: write in class FilterOutputStream Parameters: buf - A char array off - Offset from which to start taking char len - Number of char to write Java import java.io.*; //Java program to demonstrate write() method public class PrintWriterDemo { public static void main(String[] args) { char c[] = {70, 71, 72, 73, 74, 75, 76}; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // write char 1-3 pr.write(c, 1, 3); // flush the stream pr.flush(); } } Output: GHI void write(int b) :Â Writes the specified char to this stream. Syntax :public void write(int b) Overrides: write in class Writer Parameters: b - The char to be written Java import java.io.*; //Java program to demonstrate write(int b) method public class PrintWriterDemo { public static void main(String[] args) { int c = 70; // create PrintWriter object PrintWriter pr= new PrintWriter(System.out); // write char c which is character F in ASCII pr.write(c); // flush the stream pr.flush(); } } Output: F void write(String s) : Writes a string. Syntax :public void write(String s) Parameters: s - String to be written Java import java.io.*; public class PrintWriterDemo { public static void main(String[] args) { String s = "Hello"; try { // create a new writer PrintWriter pw = new PrintWriter(System.out); // write strings pw.write(s); pw.write(" World"); // flush the writer pw.flush(); } catch (Exception ex) { ex.printStackTrace(); } } } Hello World void write(String s, int off, int len) : Writes a portion of a string. Syntax :public void write(String s, int off, int len) Parameters: s - A String off - Offset from which to start writing characters len - Number of characters to write Java import java.io.*; public class PrintWriterDemo { public static void main(String[] args) { String s = "Hello World"; try { // create a new writer PrintWriter pw = new PrintWriter(System.out); // write substrings pw.write(s, 0, 5); pw.write(s, 6, 5); // flush the writer pw.flush(); } catch (Exception ex) { ex.printStackTrace(); } } } HelloWorld rt Comment More infoAdvertise with us Next Article Java.io.PrintWriter class in Java | Set 2 N Nishant Sharma Improve Article Tags : Java Java-I/O Practice Tags : Java Similar Reads Java.io.PrintWriter class in Java | Set 1 Java PrintWriter class gives Prints formatted representations of objects to a text-output stream. It implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams. Unlike the PrintStream class, if au 5 min read Java.io.Printstream Class in Java | Set 2 Java.io.Printstream Class in Java | Set 1More Methods: PrintStream printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this output stream using the specified format string and arguments. Syntax :public PrintStream printf(Locale l, String format, Obje 6 min read Java.io.Printstream Class in Java | Set 1 A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the c 5 min read Java.io.StringWriter class in Java Java StringWriter class creates a string from the characters of the String Buffer stream. Methods of the StringWriter class in Java can also be called after closing the Stream as this will raise no IO Exception. Declaration in Java StringWriter Classpublic class StringWriter extends WriterConstructo 6 min read Java.io.Writer class in Java This abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both. 4 min read Java.io.ObjectInputStream Class in Java | Set 1 ObjectInputStream Class deserializes the primitive data and objects previously written by ObjectOutputStream. Both ObjectOutputStream and ObjectInputStream are used as it provides storage for graphs of object.It ensures that the object it is working for, matches the classes of JVM i.e Java Virtual M 9 min read Java.io.OutputStream class in Java This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Constructo 2 min read Java.io.ObjectOutputStream Class in Java | Set 1 An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. Only objects that support the java.io.Serializable in 9 min read PrintWriter close() method in Java with Examples The close() method of PrintWriter Class in Java is used to close the stream. Closing a stream deallocates any value in it or any resources associated with it. The PrintWriter instance once closed won't work. Also a PrintWriter instance once closed cannot be closed again. Syntax: public void close() 2 min read Java.io.OutputStreamWriter Class In Java, OutputStreamWriter class connects character streams to byte streams. It encodes Characters into bytes using a specified charset. Declaration of Java OutputStreamWriter Class public class OutputStreamWriter extends WriterConstructors of OutputStreamWriter Class in JavaConstructors in OutputS 5 min read Like