Java.io.Printstream Class in Java | Set 1 (original) (raw)

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 declaration

public class PrintStream extends FilterOutputStream implements Appendable, Closeable

Field

protected OutputStream out:This is the output stream to be filtered.

Constructors and Description

Methods:

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

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

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

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;

import java.util.Locale;

class Printstream

{

`` public static void main(String args[]) throws FileNotFoundException

`` {

`` FileOutputStream fout= new FileOutputStream( "file.txt" );

`` PrintStream out= new PrintStream(fout);

`` String s= "First" ;

`` char c[]={ 'G' , 'E' , 'E' , 'K' };

`` out.print( true );

`` out.print( 1 );

`` out.print( 4 .533f);

`` out.print( "GeeksforGeeks" );

`` out.println();

`` out.print(fout);

`` out.println();

`` out.append( "Geek" );

`` out.println();

`` out.println(out.checkError());

`` out.format(Locale.UK, "Welcome to my %s program" , s);

`` out.flush();

`` 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 program

Next Article: Java.io.Printstream Class in Java | Set 2