23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

unsigned<br />

byte<br />

unsigned<br />

short<br />

N/A readUnsignedByte() One unsigned byte<br />

N/A readUnsignedShort()<br />

String writeBytes(String s) N/A<br />

String writeChars(String s) N/A<br />

String writeUTF(String s) readUTF()<br />

7.1.2 Constructors<br />

<strong>Java</strong> I/O<br />

Two bytes, big-endian,<br />

unsigned<br />

The low-order byte of each<br />

character in the string from<br />

first to last<br />

Both bytes of each character in<br />

the string from first to last<br />

A signed short giving the<br />

number of bytes in the UTF-8<br />

encoded string, followed by<br />

the UTF-8 encoding of the<br />

string<br />

The DataInputStream and DataOutputStream classes have exactly the constructors you<br />

would expect:<br />

public DataInputStream(InputStream in)<br />

public DataOutputStream(OutputStream out)<br />

These chain the data streams to the underlying streams passed as arguments. For example, to<br />

read formatted data from a file called data.txt and write formatted data to output.dat, you<br />

would create the two streams dis and dos:<br />

DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));<br />

DataOutputStream dos = new DataOutputStream(<br />

new FileOutputStream("output.dat"));<br />

We will now take up the data formats used by data streams and the methods used to write data<br />

in those formats.<br />

7.2 Reading and Writing Integers<br />

The DataOutputStream class has methods for writing all of <strong>Java</strong>'s primitive integer data<br />

types: byte, short, int, and long. The DataInputStream class has methods to read these<br />

types. It also has methods for reading two integer data types not directly supported by <strong>Java</strong> or<br />

the DataOutputStream class: the unsigned byte and the unsigned int.<br />

7.2.1 Integer Formats<br />

While <strong>Java</strong>'s platform independence guarantees that you don't have to worry about precise<br />

data formats when working exclusively in <strong>Java</strong>, you frequently need to read data created by a<br />

program written in another language. Similarly, it's not unusual to have to write data that will<br />

be read by a program written in a different language. For example, most <strong>Java</strong> network clients<br />

(like Hot<strong>Java</strong>) talk primarily to servers written in other languages, and most <strong>Java</strong> network<br />

servers (like the <strong>Java</strong> Web Server) talk primarily to clients written in other languages. You<br />

cannot naively assume that the data format <strong>Java</strong> uses is the data format other programs will<br />

understand; you must take care to understand and recognize the data formats being used.<br />

98

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!