Java.io.PrintWriter class in Java | Set 2
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 NullPointerExceptionimportjava.io.*;importjava.util.Locale;//Java program to demonstrate printf methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {String s ="for";// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// printf this stringpr.printf(Locale.CANADA,"Geeks%sGeeks", s);// flush the streampr.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 NullPointerExceptionimportjava.io.*;//Java program to demonstrate printf(String format, Object... args) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {String s ="for";// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// printf this stringpr.printf("Geeks%sGeeks", s);// flush the streampr.flush();}}Output: GeeksforGeeks
- void println(): Terminates the current line by writing the line separator string.
Syntax :public void println()
importjava.io.*;//Java program to demonstrate println() methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {String s ="GeeksforGeeks";// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// printf this stringpr.printf("%s", s);// flush the streampr.flush();}}Output: GeeksforGeeks
- void println(boolean x): Prints a boolean and then terminate the line.
Syntax :public void println(boolean x)
importjava.io.*;//Java program to demonstrate println(boolean) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// print a boolean and change linepr.println(true);pr.print("New Line");// flush the streampr.flush();}}Output: true New Line
- void println(char x): Prints a character and then terminate the line.
Syntax :public void println(char x)
importjava.io.*;//Java program to demonstrate println(char x) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {charc ='a';// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// print a char and change linepr.println(c);pr.println('b');// flush the streampr.flush();}}Output: a b
- void println(char[] x): Prints an array of characters and then terminate the line.
Syntax :public void println(char[] x)
importjava.io.*;//Java program to demonstrate println(char[] x) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {char[] c = {'a','b','c'};// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// print an array and change linepr.println(c);// flush the streampr.flush();}}Output: abc
- void println(double x): Prints a double and then terminate the line.
Syntax :public void println(double x)
importjava.io.*;//Java program to demonstrate println(double x) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {doublec =176.348;// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// print a double and change linepr.println(c);// flush the streampr.flush();}}Output:
176.348
- void println(float x): Prints a float and then terminate the line.
Syntax :public void println(float x)
//Java program to demonstrate println(float x) methodimportjava.io.*;publicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {floatc =5.76348f;// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// print a float and change linepr.println(c);// flush the streampr.flush();}}Output:
5.76348
- void println(int x): Prints an integer and then terminate the line.
Syntax :public void println(boolean x)
importjava.io.*;//Java program to demonstrate println(int x) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {intc =15;// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// print an integer and change linepr.println(c);// flush the streampr.flush();}}Output:
15
- void println(long x): Prints a long integer and then terminate the line.
Syntax :public void println(long x)
importjava.io.*;//Java program to demonstrate println(long x) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {longc = 1252344125l;try{// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// print a long and change linepr.println(c);// flush the streampr.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)
importjava.io.*;//Java program to demonstrate println(Object x) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {Object c =10;// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// print an object and change linepr.println(c);// flush the streampr.flush();}}Output:
10
- void println(String x) : Prints a String and then terminate the line.
Syntax :public void println(boolean x)
importjava.io.*;//Java program to demonstrate println(String x) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {String c ="GeeksforGeeks";// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// print a string and change linepr.println(c);// flush the streampr.flush();}}Output:
GeeksforGeeks
- protected void setError() : Sets the error state of the stream to true.
Syntax :public void println(String x)
importjava.io.*;//Java program to demonstrate setError() methodpublicclassPrintWriterDemoextendsPrintWriter {publicPrintWriterDemo(OutputStream out) {super(out);}publicstaticvoidmain(String[] args) {charc[] = {70,71,72,73,74,75,76};// create PrintWriter objectPrintWriterDemo pr=newPrintWriterDemo(System.out);// write char 1-3pr.write(c,1,3);// flush the streampr.flush();// set an internal errorpr.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 writeimportjava.io.*;//Java program to demonstrate write() methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {charc[] = {70,71,72,73,74,75,76};// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// write char 1-3pr.write(c,1,3);// flush the streampr.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
importjava.io.*;//Java program to demonstrate write(int b) methodpublicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {intc =70;// create PrintWriter objectPrintWriter pr=newPrintWriter(System.out);// write char c which is character F in ASCIIpr.write(c);// flush the streampr.flush();}}Output:
F
- void write(String s) : Writes a string.
Syntax :public void write(String s) Parameters: s - String to be written
importjava.io.*;publicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {String s ="Hello";try{// create a new writerPrintWriter pw =newPrintWriter(System.out);// write stringspw.write(s);pw.write(" World");// flush the writerpw.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 writeimportjava.io.*;publicclassPrintWriterDemo {publicstaticvoidmain(String[] args) {String s ="Hello World";try{// create a new writerPrintWriter pw =newPrintWriter(System.out);// write substringspw.write(s,0,5);pw.write(s,6,5);// flush the writerpw.flush();}catch(Exception ex) {ex.printStackTrace();}}}HelloWorld
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.rt



