Menu
Topics Index
...
`


Input/Output: Exploring java.io > Byte Streams >
Siva Nookala - 15 Apr 2016
PrintStream class enables you to write formatted data to an underlying OutputStream.PrintStream never throws an IOException.The PrintStream class contains format() and printf() methods.The PrintStream has a wide selection of contructors that enable you to connect it to a File, or an OutputStream.

PrintStream has the following Constructors.
ConstructorDescription
PrintStream(File file)It creates a new print stream, without automatic line flushing, with the specified file.
PrintStream(File file, String csn)It creates a new print stream, without automatic line flushing, with the specified file and charset.
PrintStream(OutputStream out),PrintStream(OutputStream out, boolean autoFlush),PrintStream(OutputStream out, boolean autoFlush, String encoding)These creates a new print stream.
PrintStream(String fileName)It creates a new print stream, without automatic line flushing, with the specified file name.
The following are the some important PrintStream Methods.
CMethodDescription
PrintStream append(char c)It appends the specified character to this output stream.
PrintStream append(CharSequence csq, int start, int end)It appends a subsequence of the specified character sequence to this output stream.
boolean checkError()It flushes the stream and checks its error state.
protected void clearError()It clears the internal error state of this stream.
void close()It closes stream.
void flush()It flushes stream.
void println(char x)It prints a character and then terminate the line.
void write(byte[] buf, int off, int len)It writes len bytes from the specified byte array starting at offset off to this stream.
PrintStreamDemo
import java.io.*;

class PrintStreamDemo
{
    public static void main(String arg[])
    {
        String s = "Alone we can do so little";//LINE A
        
        PrintStream ps = new PrintStream(System.out); //LINE B
        
        ps.format(" %s, Together we can do so much ", s);// LINE C
        
        ps.close();// LINE D
    
    }
}
OUTPUT

Alone we can do so little, Together we can do so much

DESCRIPTION

At LINE A we created a String. At LINE B we created PrintStream object. At LINE C we formated String. At LINE D we closed PrintStream.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.