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 Outputwe need to perform translation from a native format (one or more bytes percharacter) to two-byte characters in UNICODE.But what about binary files?Obviously we don't want to take a binary file such as a database and perform theUNICODE translation on its contents. We need two basic ways to handle data.Text I/O Versus Binary I/OJava's I/O classes are divided into two main groups, based on whether you wanttext or binary I/O.Reader and Writer classes handle text I/O. InputStream and OutputStreamclasses handle binary I/O.Any time you see "Reader" or "Writer" as part of a Java I/O class name, youshould immediately think "text I/O". Anytime you see "Stream" as part of theclass name, think "binary I/O".Reader and InputStreamJava supplies Readers and InputStreams to read data; their use is similar. Thefollowing table shows the most commonly used methods in these classes. See thejavadocs for the other methods available.Note that these two classes are abstract; you won't ever create an instance ofeither, but they provide the base implementation details for all other input classes.void close()int read()int read(type[])Closes the input. Always call this method whenyou are finished reading what you need, as it allowsthe VM to release locks on the file.Reads a single item from the file. In Reader, an itemis a char, while in InputStream it's a byte. Thereturn value will either be the item or -1 if there isno more data (end-of-file has been reached.)Attempts to fill the array with as much data aspossible. If enough data is available, the type[](char[] for Reader, byte[] for InputStream)will be filled with the data and the length of thearray will be returned.Java Input and Output -8© 1996-2003 jGuru.com. All Rights Reserved.

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

Saved successfully!

Ooh no, something went wrong!