Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
I/O Ji Zhenyan [email protected] Tel: 063 165713 Ji Zhenyan, ITM, Mitthögskolan Contents • What’s I/O? • I/O Streams ¾ Character streams ¾ Byte Streams • • • • • • File streams Pipe Streams How to Concatenate Files DataInputStream & DataOutputStream Writing Customized Filtered Streams Object Serialization ¾ Write to an ObjectOutputStream ¾ Read from an ObjectOutputStream ¾ Providing Object Serialization Ji Zhenyan, ITM, Mitthögskolan What’s I/O? • “I” means “input”: a program opens a stream on an information source (a file, memory, a socket) and reads the information serially. • “O” means “output”: a program can send information to an external destination by opening a stream to a destination and writing the information out serially. Ji Zhenyan, ITM, Mitthögskolan I/O Streams • java.io contains character streams and byte streams that perform the same processing but for the different data type: ¾ java.io. Reader --streams that read 16-bit characters ¾ java.io.Writer --streams that write 16-bit characters. ¾ java.io.InputStream --streams that read 8-bit bytes. ¾ java.io.OutputStream --streams that write 8-bit bytes. Ji Zhenyan, ITM, Mitthögskolan Character streams • Hierarchy of input character streams: Ji Zhenyan, ITM, Mitthögskolan • Hierarchy of output character streams: Ji Zhenyan, ITM, Mitthögskolan Byte Streams • Hierarchy of input byte streams: Ji Zhenyan, ITM, Mitthögskolan • Hierarchy of output byte streams: Ji Zhenyan, ITM, Mitthögskolan File Streams • The file streams -- FileReader, FileWriter, FileInputStream, and FileOutputStream --each read or write from a file on the native file system. • A file stream can be created from a file name in the form of a string, a File object, or a FileDescriptor object. • Examples: ¾ Copy.java ¾ CopyBytes.java Ji Zhenyan, ITM, Mitthögskolan Use Pipe Streams • Pipes are used to channel the output from one program (or thread) into the input of another. ¾ Without pipe streams, the program would have to store the results somewhere (such as in a file or in memory) between each step. Ji Zhenyan, ITM, Mitthögskolan ¾ With pipe streams, the output from one method could be piped into the next Ji Zhenyan, ITM, Mitthögskolan • PipedReader and PipedWriter (and their input and output stream counterparts PipedInputStream and PipedOutputStream ) implement the input and output components of a pipe. • Example: ¾ RhymindWords, ReverseThread & SortThread Ji Zhenyan, ITM, Mitthögskolan How to Concatenate Files • The class java.io.SequenceInputStream can create a single input stream from multiple input sources. • Example: ¾Concatenate.java & ListOfFiles.java Ji Zhenyan, ITM, Mitthögskolan DataInputStream & DataOutputStream • DataOutputStream, like other filtered output streams, must be attached to some other OutputStream. • DataInputStream also must be attached to some other InputStream. • Example: ¾DataIOTest.java Ji Zhenyan, ITM, Mitthögskolan Writing Customized Filtered Streams • Steps: ¾ Create a subclass of FilterInputStream and FilterOutputStream. Input and output streams often come in pairs, so it's likely that you will need to create both input and output versions of your filter stream. ¾ Override the read and write methods. ¾ Override any other methods that you might need. ¾ Make sure the input and output streams work together. Ji Zhenyan, ITM, Mitthögskolan Object Serialization • The key to writing an object is to represent its state in a serialized form sufficient to reconstruct the object as it is read. Thus reading and writing objects is a process called object serialization. • Two streams, java.io.ObjectInputStream and java.io.ObjectOutputStream, can read and write objects. Ji Zhenyan, ITM, Mitthögskolan Write to an ObjectOutputStream • FileOutputStream out = new FileOutputStream("theTime"); ObjectOutputStream s = new ObjectOutputStream(out); s.writeObject("Today"); s.writeObject(new Date()); s.flush(); ObjectOutputStream must be constructed on another stream. This code constructs an ObjectOutputStream on a FileOutputStream, thereby serializing the object to a file named theTime. Ji Zhenyan, ITM, Mitthögskolan Read from an ObjectOutputStream • FileInputStream in = new FileInputStream("theTime"); ObjectInputStream s = new ObjectInputStream(in); String today = (String)s.readObject(); Date date = (Date)s.readObject(); ObjectInputStream must be constructed on another stream. In this example, the objects were archived in a file, so the code constructs an ObjectInputStream on a FileInputStream. • Example: DataIOTest2.java Ji Zhenyan, ITM, Mitthögskolan Providing Object Serialization • An object is serializable only if its class implements the Serializable interface • Serializable is an empty interface public class MySerializableClass implements Serializable { ... } Ji Zhenyan, ITM, Mitthögskolan Ji Zhenyan, ITM, Mitthögskolan