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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

The details of this format are too complicated to discuss here. You can order the actual<br />

specification [1] from the IEEE for about $29.00. That's approximately $1.50 a page, more than<br />

a little steep in my opinion. The specification isn't available online, but it was published in the<br />

February 1985 issue of ACM SIGPLAN Notices (Volume 22, #2, pp. 9-18), which should be<br />

available in any good technical library. The main thing you need to know is that these formats<br />

are supported by most modern RISC architectures and by all Pentium and Motorola 680x0<br />

chips with either external or internal floating-point units (FPUs). Nowadays the only chips<br />

that don't natively support this format are a few embedded processors and some old 486SX,<br />

68LC040, and other earlier FPU-less chips in legacy hardware. And even these systems are<br />

able to emulate IEEE 754 floating-point arithmetic in software.<br />

The DataInputStream class reads and the DataOutputStream class writes floating-point<br />

numb 1 ers of either four or eight bytes in length, as specified in the IEEE 754 standard. They<br />

do not support the 10-byte and longer long double, extended double, and double double<br />

formats supported by some architectures and compilers. If you have to read floating-point<br />

data written in some format other than basic IEEE 754 float and double, you'll need to write<br />

your own class to convert the format to four- or eight-byte IEEE 754.<br />

7.3.1 Writing Floating-Point Numbers<br />

There are two methods in the DataOutputStream class that write floating-point numbers,<br />

writeFloat() and writeDouble():<br />

public final void writeFloat(float f) throws <strong>IO</strong>Exception<br />

public final void writeDouble(double d) throws <strong>IO</strong>Exception<br />

Both of these methods throw an <strong>IO</strong>Exception if something goes wrong with the underlying<br />

stream. Otherwise, they're fairly innocuous and can convert any float or double to bytes and<br />

write it on the underlying stream.<br />

Example 7.4 fills a file called roots.dat with the square roots of the numbers to 1000. First a<br />

FileOutputStream is opened to roots.dat. This stream is chained to a DataOutputStream,<br />

whose writeDouble() method writes the data into the file.<br />

Example 7.4. Writing Doubles with a DataOutputStream<br />

import java.io.*;<br />

public class RootsFile {<br />

public static void main(String[] args) {<br />

try {<br />

FileOutputStream fout = new FileOutputStream("roots.dat");<br />

DataOutputStream dout = new DataOutputStream(fout);<br />

for (int i = 0; i

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

Saved successfully!

Ooh no, something went wrong!