tam1

Java-PrintWriter

PrintWriter

The java.io.PrintWriter class is a subclass of java.io.Writer. PrintWriter is essentially a character-oriented version of PrintStream, that contains the familiar print( ) and println( ) methods from PrintStream. The PrintWriter class implements the abstract write() methods from java.io.Writer. The PrintWriter can send text either to an output stream or to another writer.

Print and Println Methods

PrintWriter supports the print( ) and println( ) methods for all types, including Object. If an argument is not a primitive type, the PrintWriter 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.

Difference Between PrintStream and PrintWriter

The main difference between PrintStream and PrintWriter is that PrintWriter does not contain methods for writing raw bytes, for which a program should use unencoded byte streams. The other, more minor difference is that automatic flushing is performed only when println( ) is invoked, not every time a newline character is seen.

PrintWriter Features

PrintWriter 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 writer has ever encountered an error during its lifetime. Most of the time, you just ignore this, since print writers are only used in situations where exhaustive error checking is unnecessary.

Optionally, a PrintWriter can be created so as to flush automatically; this means that the flush method is automatically invoked after every time println( ), printf( ), or format( ) is called. The format( ) and printf( ) methods allow you to mix text and data in very advanced ways, using a formatting string.

Some constructors allow a PrintWriter 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 PrintWriter object directs all output to the specified file.


PrintWriter Constructors

Constructor Description
PrintWriter​(File file) Creates a new PrintWriter, without automatic line flushing, with the specified file.
PrintWriter​(File file, String csn) Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriter​(File file, Charset charset) Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.
PrintWriter​(OutputStream out) Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.
PrintWriter​(OutputStream out, boolean autoFlush) Creates a new PrintWriter, with the specified automatic line flushing, from an existing OutputStream.
PrintWriter​(OutputStream out, boolean autoFlush, Charset charset) Creates a new PrintWriter, with the specified automatic line flushing and charset, from an existing OutputStream.
PrintWriter​(Writer out) Creates a new PrintWriter, without automatic line flushing, from an existing Writer.
PrintWriter​(Writer out, boolean autoFlush) Creates a new PrintWriter, with the specified automatic line flushing, from an existing Writer.
PrintWriter​(String fileName) Creates a new PrintWriter, without automatic line flushing, with the specified file name.
PrintWriter​(String fileName, String csn) Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.
PrintWriter​(String fileName, Charset charset) Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.



PrintWriter Methods

Method Description
PrintWriter​ append(char c) Appends the specified character to this writer.
PrintWriter​ append(CharSequence csq) Appends the specified character sequence to this writer.
PrintWriter​ append(CharSequence csq, int start, int end) Appends a subsequence of the specified character sequence to this writer.
PrintWriter ​format(String format,Object… args) Writes a formatted string to this writer using the specified format string and arguments.
PrintWriter​ printf(String format,Object… args) A convenience method to write a formatted string to this writer 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​(char[] buf) Writes an array of characters.
void write​(char[] buf, int off, int len) Writes a Portion of an array of characters.
void write​(int c) Writes a single character.
void write​(String s) Writes a string.
void write​(String s, int off, int len) Writes a portion of a string.
boolean checkError() Flushes the writer and checks its error state.
void clearError() Clears the internal error state of this writer.
void flush() Flushes the writer.
void close() Closes the writer.



Program

programoutput

Program Source

import java.io.PrintWriter;
import java.io.IOException;

public class Javaapp {
  
    public static void main(String[] args) throws IOException {
        
        try(PrintWriter ps = new PrintWriter("data.txt")){
            
            ps.print("Rolls Royce Motor Cars Limited established in ");
            ps.println(new Integer(1998));
            ps.println("Rolls Royce Top Models");
            ps.printf("%s : %d Crore"+System.lineSeparator(),"Rolls Royce Phantom",10);
            ps.format("%s : %d Crore","Rolls Royce Ghost  ",6);
        }
        try(PrintWriter ps = new PrintWriter(System.out)){
            
            ps.print("Rolls Royce Motor Cars Limited established in ");
            ps.println(new Integer(1998));
            ps.println("Rolls Royce Top Models");
            ps.printf("%s : %d Crore"+System.lineSeparator(),"Rolls Royce Phantom",10);
            ps.format("%s : %d Crore","Rolls Royce Ghost  ",6);
        }
    }
}

Leave a Comment