Класс Java.io.Printstream в Java | Комплект 2
Java.io.Printstream Class in Java | Set 1
More 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, 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 output stream Throws: IllegalFormatException NullPointerException//Java program to demonstrate printf methodimportjava.io.*;importjava.util.Locale;classPrintStreamDemo{publicstaticvoidmain(String[] args){String s ="for";// create printstream objectPrintStream printStream =newPrintStream(System.out);// illustrating printf(Locale l, String format, Object... args) methodprintStream.printf(Locale.US,"Geeks%sGeeks", s);}}Output: GeeksforGeeks
- PrintStream printf(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(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 output stream Throws: IllegalFormatException NullPointerException//Java program to demonstrate printf(String format, Object... args) methodimportjava.io.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){String s ="for";// create printstream objectPrintStream obj=newPrintStream(System.out);// illustrating printf(String format, Object... args) methodobj.printf("Geeks%sGeeks", s);}}Output: GeeksforGeeks
- void println(): Terminates the current line by writing the line separator string.
Syntax :public void println()
//Java program to demonstrate println() methodsimportjava.io.PrintStream;classPrintStreamDemo{publicstaticvoidmain(String[] args){PrintStream obj =newPrintStream(System.out);//illustrating println();obj.println("GeeksforGeeks");}}Output: GeeksforGeeks
- void println(boolean x): Prints a boolean and then terminate the line.
Syntax :public void println(boolean x)
//Java program to demonstrate println(boolean) methodimportjava.io.*;classPrintStreamDemo{publicstaticvoidmain(String[] args){// create printstream objectPrintStream obj =newPrintStream(System.out);//illustrating println(boolean) methodobj.println(true);// flush the streamobj.flush();}}Output: true
- void println(char x): Prints a character and then terminate the line.
Syntax :public void println(char x)
//Java program to demonstrate println(char x) methodimportjava.io.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){charc ="g";// create printstream objectPrintStream obj =newPrintStream(System.out);// illustrating println(char x)obj.println(c);// flush the streamobj.flush();}}Output: g
- void println(char[] x): Prints an array of characters and then terminate the line.
Syntax :public void println(char[] x)
//Java program to demonstrate println(char[] x) methodimportjava.io.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){char[] c = {"G","E","E","K"};// create printstream objectPrintStream obj =newPrintStream(System.out);// illustrating println(char[] x)obj.println(c);// flush the streamobj.flush();}}Output: GEEK
- void println(double x): Prints a double and then terminate the line.
Syntax :public void println(double x)
//Java program to demonstrate println(double x) methodimportjava.io.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){doublec =5.42762;// create printstream objectPrintStream obj =newPrintStream(System.out);// illustrating println(double x)obj.println(c);// flush the streamobj.flush();}}Output:
5.42762
- 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.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){floatc =5.168502f;// create printstream objectPrintStream obj =newPrintStream(System.out);// illustrating println(float x)obj.println(c);// flush the streamobj.flush();}}Output:
5.168502f
- void println(int x): Prints an integer and then terminate the line.
Syntax :public void println(boolean x)
//Java program to demonstrate println(int x) methodimportjava.io.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){intc =5;// create printstream objectPrintStream obj =newPrintStream(System.out);// illustrating println(int x)obj.println(c);// flush the streamobj.flush();}}Output:
5
- void println(long x): Prints a long and then terminate the line.
Syntax :public void println(long x)
//Java program to demonstrate println(long x) methodimportjava.io.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){longc = 123456789l;try{// create printstream objectPrintStream obj=newPrintStream(System.out);// illustrating println(long x)obj.println(c);// flush the streamobj.flush();}catch(Exception ex){ex.printStackTrace();}}}Output:
123456789
- void println(Object x) :Prints an Object and then terminate the line.
Syntax :public void println(Object x)
//Java program to demonstrate println(Object x) methodimportjava.io.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){// create printstream objectPrintStream obj =newPrintStream(System.out);//illustrating println(Object X)obj.println(obj);// flush the streamobj.flush();}}Output:
java.io.PrintStream@15db9742
- void println(String x) :Prints a String and then terminate the line.
Syntax :public void println(boolean x)
importjava.io.*;importjava.io.*;//Java program to demonstrate println(String x) methodpublicclassPrintStreamDemo{publicstaticvoidmain(String[] args){String c ="GeeksforGeeks";// create printstream objectPrintStream ps =newPrintStream(System.out);// illustrating println(String x)ps.println(c);// flush the streamps.flush();}}Output:
GeeksforGeeks
- protected void setError() :Sets the error state of the stream to true.
Syntax :public void println(String x)
//Java program to demonstrate setError() methodimportjava.io.*;publicclassPrintStreamDemoextendsPrintStream{publicPrintStreamDemo(OutputStream out){super(out);}publicstaticvoidmain(String[] args){bytec[] = {65,66,67,68,69,70,71};// create printstream objectPrintStreamDemo obj =newPrintStreamDemo(System.out);// illustrating write() methodobj.write(c,1,3);// flush the streamobj.flush();//illustrating setError() methodobj.setError();}}Output:
BCD
- void write(byte[] buf, int off, int len) :Writes len bytes from the specified byte array starting at offset off to this stream.
Syntax :public void write(byte[] buf, int off, int len) Overrides: write in class FilterOutputStream Parameters: buf - A byte array off - Offset from which to start taking bytes len - Number of bytes to write//Java program to demonstrate write(int b) methodimportjava.io.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){bytec =65;// create printstream objectPrintStream obj =newPrintStream(System.out);//illustrating write(int b)obj.write(c);// flush the streamobj.flush();}}Output:
BCD
- void write(int b) :Writes the specified byte to this stream.
Syntax :public void write(int b) Overrides: write in class FilterOutputStream Parameters: b - The byte to be written
//Java program to demonstrate write(int b) methodimportjava.io.*;publicclassPrintStreamDemo{publicstaticvoidmain(String[] args){bytec =65;// create printstream objectPrintStream obj =newPrintStream(System.out);//illustrating write(int b)obj.write(c);// flush the streamobj.flush();}}Output:
A
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.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.