tam1

Java-PrintStream

PrintStream

We can use print streams to output data into a file or to the console. System.out and System.err are instances of the PrintStream class. This is a subclass of FilterOutputStream that converts primitive types and objects to text. System.out is primarily used for simple, character-mode applications. All characters printed by a PrintStream are converted into bytes using the given encoding or charset, or platform’s default character encoding if not specified.

Print and Println Methods

PrintStream supports the print( ) and println( ) methods for all types, including Object. If an argument is not a primitive type, the PrintStream methods will call the object’s toString( ) method and then display the result. Primitive types are converted with the appropriate String.valueOf( ) method. The print( ) and println () methods differ only in that println( ) prints a platform specific line terminator after printing its arguments and print( ) does not.

One aspect of making System.out simple for quick jobs is not in the PrintStream class at all but in the compiler. Because Java overloads the + operator to signify concatenation of strings, primitive data types, and objects, you can pass multiple variables to the print( ) and println( ) methods, which are then converted to strings and concatenated.

stringconcordination
PrintStream Features

PrintStream methods never throw IOExceptions. Each method in the class catches IOExceptions. When an exception occurs, an internal flag is set to true. You test this flag with the checkError( ) method. This method returns true if this print stream has ever encountered an error during its lifetime. Most of the time, you just ignore this, since print streams are only used in situations where exhaustive error checking is unnecessary.

Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte (‘\n’) is written. PrintStream class contains the powerful format( ) and printf( ) methods. These methods allow you to mix text and data in very advanced ways, using a formatting string.

Some constructors allow a PrintStream to be created from a File object or by specifying the name of a file. In either case, the file is automatically created. Any preexisting file by the same name is destroyed. Once created, the PrintStream object directs all output to the specified file.


PrintStream Constructors

Constructor Description
PrintStream(File file) Creates a new print stream, without automatic line flushing, with the specified file.
PrintStream(File file, String csn) Creates a new print stream, without automatic line flushing, with the specified file and charset.
PrintStream(File file, Charset charset) Creates a new print stream, without automatic line flushing, with the specified file and charset.
PrintStream(OutputStream out) Creates a new print stream. This stream will not flush automatically.
PrintStream(OutputStream out, boolean autoFlush) Creates a new print stream, with the specified automatic line flushing.
PrintStream(OutputStream out, boolean autoFlush, String encoding) Creates a new print stream, with the specified OutputStream, automatic line flushing and encoding.
PrintStream(OutputStream out, boolean autoFlush,
Charset charset)
Creates a new print stream, with the specified OutputStream, automatic line flushing and charset.
PrintStream(String fileName) Creates a new print stream, without automatic line flushing, with the specified file name.
PrintStream(String fileName, String csn) Creates a new print stream, without automatic line flushing, with the specified file name and charset.
PrintStream(String fileName, Charset charset) Creates a new print stream, without automatic line flushing, with the specified file name and charset.


PrintStream Methods
Method Description
PrintStream append(char c) Appends the specified character to this output stream.
PrintStream append (CharSequence csq) Appends the specified character sequence to this output stream.
PrintStream append (CharSequence csq, int start, int end) Appends a subsequence of the specified character sequence to this output stream.
PrintStream format(String format,Object… args) Writes a formatted string to this output stream using the specified format string and arguments.
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.
void print(boolean b) Prints a boolean value.
void print(char c) Prints a character.
void print(char[] s) Prints an array of characters.
void print(double d) Prints a double-precision floating-point number.
void print(float f) Prints a floating-point number.
void print(int i) Prints an integer.
void print(long l) Prints a long integer.
void print(Object obj) Prints an object.
void print(String s) Prints a string.
void println() Terminates the current line by writing the line separator string.
void println(boolean x) Prints a boolean and then terminate the line.
void println(char x) Prints a character and then terminate the line.
void println(char[] x) Prints an array of characters and then terminate the line.
void println(double x) Prints a double and then terminate the line.
void println(float x) Prints a float and then terminate the line.
void println(int x) Prints an integer and then terminate the line.
void println(long x) Prints a long and then terminate the line.
void println(Object x) Prints an Object and then terminate the line.
void println(String x) Prints a String and then terminate the line
void write(byte[] buf, int off, int len) Writes len bytes from the specified byte array starting at offset off to this stream.
write(int b) Writes the specified byte to this stream.
boolean checkError() Flushes the stream and checks its error state.
void clearError() Clears the internal error state of this stream.
void flush() Flushes the stream.
void close() Closes the stream.



Program

program

Program Source

import java.io.PrintStream;
import java.io.IOException;

public class Javaapp {
  
    public static void main(String[] args) throws IOException {
        
        try(PrintStream​ ps = new PrintStream​("data.txt")){
            
            ps.print("Kruger National Park ");
            ps.println("South Africa.");
            int km = 19485;
            ps.println("It covers an area of "+km+" square kilometres");
            short el = 11672;
            ps.println("Elephant : "+el);
            ps.printf("Lion     : %d"+System.lineSeparator(),new Short((short)2800));
            ps.write(new byte[]{76,101,111,112,97,114,100});
            ps.print("  : 2000");
        }
    }
}

Leave a Comment