Java Reference
In-Depth Information
is still widely used. In fact, all of the programs in this topic up to this point have used it!
Thus, it is examined here.
Console output is most easily accomplished with print( ) and println( ) , with which you
are already familiar. These methods are defined by the class PrintStream (which is the
type of the object referenced by System.out ). Even though System.out is a byte stream, it
is still acceptable to use this stream for simple console output.
Since PrintStream is an output stream derived from OutputStream , it also implements
the low-level method write( ) . Thus, it is possible to write to the console by using write( ) .
The simplest form of write( ) defined by PrintStream is shown here:
void write(int byteval )
This method writes the byte specified by byteval to the file. Although byteval is declared
as an integer, only the low-order 8 bits are written. Here is a short example that uses write(
) to output the character X followed by a new line:
You will not often use write( ) to perform console output (although it might be useful in
some situations), since print( ) and println( ) are substantially easier to use.
PrintStream supplies two additional output methods: printf( ) and format( ) . Both give
you detailed control over the precise format of data that you output. For example, you can
specify the number of decimal places displayed, a minimum field width, or the format of
a negative value. Although we won't be using these methods in the examples in this topic,
they are features that you will want to look into as you advance in your knowledge of Java.
Reading and Writing Files Using Byte Streams
Java provides a number of classes and methods that allow you to read and write files. Of
course, the most common types of files are disk files. In Java, all files are byte-oriented,
Search WWH ::




Custom Search