Although byteval is declared as an integer in write(), why only the low-order eight bits are written to the file in Java I/O streams

From an abstract given in Herbert Schildt book on Java.

To write to a file, you can use the write( ) method defined by FileOutputStream. Its simplest form is shown here:

void write(int byteval) throws IOException

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

Does that means only 2^8 = 256 combinations can be written. What if we have to include the Extended ASCII or Unicode ones.

Secondly,

Java defines two types of streams: byte and character. Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data. Character streams provide a convenient means for handling input and output of characters. They use Unicode and, therefore, can be internationalized. Also, in some cases, character streams are more efficient than byte streams.

At the lowest level, all I/O is still byte-oriented. The character-based streams simply provide a  
convenient and efficient means for handling characters.

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. The simplest form of write( ) defined by PrintStream is shown here:

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. Here is a short example that uses write( ) to output the character “A” followed by a newline to the screen:

class WriteDemo {

   public static void main(String args[]) {

    int b;
    b = 'A';
    System.out.write(b);
    System.out.write('\n');
    System.out.write(65);
    System.out.write('\n');
    System.out.write('A');
    System.out.write('\n');

  }
}

Output:

A
A
A

Even though System.out is a byte stream, then why does it output in the form of Characters and not in the form of bytes. (Or Am i taking it wrong. Byte Streams have nothing to do with the representation of the output.)

Upvotes: 1

Views: 277

Answers (2)

Jim Garrison
Jim Garrison

Reputation: 86774

Outside your Java program everything is a stream of bytes.

In Java, streams are raw bytes. You can use streams directly to access the external data in its 'native' unmodified form.

Java readers are wrappers around streams that convert between bytes and characters (16-bit entities) using some encoding convention (i.e. UTF-8, ISO-8850-1).

System.out is a PrintStream, which is kind of a hybrid. It is fundamentally a byte stream, so write(65) writes the ASCII character A. However, PrintStream also happens to include some methods that use the system default encoding to convert Character and String values to bytes. So if your default encoding was UTF-8 you could write

char uc = '÷';         // Unicode 0x00f7
System.out.print(uc);

The output would be a 2-byte sequence 0xC3,0xB7 which is the UTF-8 encoding of the ÷ symbol.

Upvotes: 1

Drejc
Drejc

Reputation: 14286

Because System.out is a PrintStream by default assigned (console output) any call to System.out.write(int) will convert the int value to an ASCII char.

So if you want to print 65 you must System.out.write("65");

Everything depends on the Stream assigned to your writer.

Upvotes: 2

Related Questions