902 Files and Streams Chapter 16 back onto
902 Files and Streams Chapter 16 back onto the stream. PushbackInputStreams are used by programs (like compilers) that parse their inputs that is, break them into meaningful units (such as the keywords, identifiers and operators that the compiler must recognize). When object instance variables are output to a disk file, in a sense we lose the object s type information. We have only data, not type information, on a disk. If the program that is going to read this data knows what object type it corresponds to, then the data is simply read into objects of that type. Sometimes, we would like to read or write an entire object to a file. The ObjectInputStream and ObjectOutputStream classes, which respectively implement the ObjectInput and ObjectOutput interfaces, enable an entire object to be read from or written to a file (or other stream type). We often chain ObjectInput- Streams to FileInputStreams. (We also chain ObjectOutputStreams to File- OutputStreams.) The ObjectOutput interface contains method writeObject, which takes an Object that implements interface Serializable as an argument and writes its information to the OutputStream. Correspondingly, the ObjectInput interface requires method readObject, which reads and returns an Object from an Input- Stream. After the reading of an object, it can be cast to the desired type. Additionally, these interfaces include other Object-centric methods as well as the same methods as DataInput and DataOutputfor reading and writing primitive data types. Java stream I/O includes capabilities for inputting from byte arrays in memory and outputting to byte arrays in memory. A ByteArrayInputStream (a subclass of InputStream) reads from a byte array in memory. A ByteArrayOutputStream (a subclass of OutputStream) outputs to a byte array in memory. One application of byte-array I/O is data validation. A program can input an entire line at a time from the input stream into a byte array. Then, a validation routine can scrutinize the contents of the byte array and correct the data, if necessary. Then, the program can proceed to input from the byte array, knowing that the input data is in the proper format. Outputting to a byte array is a nice way to take advantage of the powerful output-formatting capabilities of Java streams. For example, data can be prepared in a bytearray, using the same formatting that will be displayed at a later time, then output to a disk file to preserve the screen image. A SequenceInputStream(a subclass of InputStream) enables concatenation of several InputStreams, so that the program sees the group as one continuous Input- Stream. As the program reaches the end of an input stream, that stream closes and the next stream in the sequence opens. In addition to the byte based streams, Java provides Reader and Writer classes, which are Unicode, two-byte, character based streams. Most of the byte-based streams have corresponding character-based Reader or Writer classes. Class BufferedReader (a subclass of abstract class Reader) and class BufferedWriter (a subclass of abstract class Writer) enable efficient buffering for character-based streams. Character-based streams use Unicode characters such streams can process data in any language that the Unicode character set represents. Class CharArrayReader and class CharArrayWriterread and write a stream of characters to a character array. A PushbackReader (a subclass of abstract class FilterReader) enables characters to be pushed back on a character stream. A LineNumberReader (a subclass of BufferedReader) is a buffered character-stream that keeps track of line numbers (i.e., a newline, a return or a carriage-return line-feed combination).
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.