Showing posts with label println(). Show all posts
Showing posts with label println(). Show all posts

Sunday, 26 January 2014

PrintWriter class in java

how to use printwriter class..

The PrintWriter Class


Although using System.out to write to the console is acceptable, its use is recommended mostly for debugging purposes or for sample programs. Java is through a PrintWriter stream. PrintWriteris one of the character-based classes.Using a character-based class for console output makes it easier to internationalize your program. PrintWriter supports the print( ) and println( ) methods for all types including Object.

Writing Console Output in java

how to work writing console output

Writing Console Output


Console output is most easily accomplished with print( ) and println( ). These methods are defined by the class PrintStream (which is the type of object referenced by System.out). Even though System.out is a byte stream, using it for simple program output is still acceptable.Because PrintStream is an output stream derived from OutputStream, it also implements the low-level method write( ). Thus, write( ) can be used to write to the console.

General form of writing console output

void write(int byteval)

This method writes to the stream the byte specified by byteval. Although byteval is declared as an integer, only the low-order eight bits are written.

Example for writing console output

// Demonstrate System.out.write().
class WriteDemo
 {
public static void main(String args[])
 {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
}
}

You will not often use write( ) to perform console output (although doing so might be useful in some situations), because print( ) and println( ) are substantially easier to use.