Last Updated : 12 Sep, 2023
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 checkError method. Optionally, a PrintStream can be created so as to flush automatically. All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The PrintWriter class should be used in situations that require writing characters rather than bytes.
Class declarationpublic class PrintStream extends FilterOutputStream implements Appendable, CloseableField
protected OutputStream out:This is the output stream to be filtered.Constructors and Description
Syntax :public PrintStream append(char c) Parameters: c - The 16-bit character to append Returns: This output stream
Syntax :public PrintStream append(CharSequence csq, int start, int end) Parameters: csq - The character sequence from which a subsequence will be appended. start - The index of the first character in the subsequence end - The index of the character following the last character in the subsequence Returns: This output stream Throws: IndexOutOfBoundsException
Syntax :public PrintStream append(CharSequence csq) Parameters: csq - The character sequence to append. Returns: This output stream
Syntax :public boolean checkError() Returns: true if and only if this stream has encountered an IOException other than InterruptedIOException, or the setError method has been invoked
Syntax :protected void clearError()
Syntax :public void close() Overrides: close in class FilterOutputStream
Syntax :public void flush() Overrides: flush in class FilterOutputStream
Syntax :public PrintStream format(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. The number of arguments is variable and may be zero. Returns: This output stream Throws: IllegalFormatException NullPointerException
Syntax :public PrintStream format(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. The number of arguments is variable and may be zero. Returns: This output stream Throws: IllegalFormatException NullPointerException
Syntax :public void print(boolean b)
Syntax :public void print(char c)
Syntax :public void print(char[] s)
Syntax :public void print(double b)
Syntax :public void print(float f)
Syntax :public void print(int i)
Syntax :public void print(long l)
Syntax :public void print(Object obj)
Syntax :public void print(String s)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Locale;
//Java program to demonstrate PrintStream methods
class Printstream
{
public static void main(String args[]) throws FileNotFoundException
{
FileOutputStream fout=new FileOutputStream("file.txt");
//creating Printstream obj
PrintStream out=new PrintStream(fout);
String s="First";
//writing to file.txt
char c[]={'G','E','E','K'};
//illustrating print(boolean b) method
out.print(true);
//illustrating print(int i) method
out.print(1);
//illustrating print(float f) method
out.print(4.533f);
//illustrating print(String s) method
out.print("GeeksforGeeks");
out.println();
//illustrating print(Object Obj) method
out.print(fout);
out.println();
//illustrating append(CharSequence csq) method
out.append("Geek");
out.println();
//illustrating checkError() method
out.println(out.checkError());
//illustrating format() method
out.format(Locale.UK, "Welcome to my %s program", s);
//illustrating flush method
out.flush();
//illustrating close method
out.close();
}
}
Note: The output might not be visible on online IDE as it is not able to read the file.
Output:true14.533GeeksforGeeks java.io.FileOutputStream@1540e19dGeek false Welcome to my First programNext Article: Java.io.Printstream Class in Java | Set 2
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4