13.07.2015 Views

I/O Fundamentals

I/O Fundamentals

I/O Fundamentals

SHOW MORE
SHOW LESS
  • No tags were found...

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

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

Java Input and Outputdos.close(); // always close outermost!}catch(Exception e) {e.printStackTrace();}}}then read the data:import java.io.*;public class DataInputTest {public static void main(String[] args) {try {DataInputStream dis = new DataInputStream(new FileInputStream("config.bin"));int someInt = dis.readInt();float someFloat = dis.readFloat();char firstChar = dis.readChar();char secondChar = dis.readChar();System.out.println(someInt);System.out.println(someFloat);System.out.println(firstChar);System.out.println(secondChar);dis.close(); // always close outermost!}catch(Exception e) {e.printStackTrace();}}}Note that you must ensure that the order of read values is the same as the order inwhich they were written!We'll discuss the external format of the output a bit later.Compressing Input and OutputIf you need to send large blocks of data over the network or infrequently use largeblocks of data, you can reduce network bandwidth requirements or the file size ofthese chucks by taking advantage of the GZIPInputStream andGZIPOutputStream classes, introduced with Java 1.1. Hiding in thejava.util.zip package, these two classes can reduce your size requirementsconsiderably. The only cost is a few CPU cycles to perform the compression. Ifyou have the cycles to spare, it's well worth the effort to use the new streams,especially to reduce network bandwidth requirements.Java Input and Output -28© 1996-2003 jGuru.com. All Rights Reserved.

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

Saved successfully!

Ooh no, something went wrong!