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
Input and Output Text and Binary I/O Chapter 19 Introduction to Java Y.Daniel Liang 1 File Input and Output Programs to run are in the FileIO folder: 1. ScannerDriver.java 2. ScannerFile.java ScannerTextfile.txt ScannerOutputfile.txt 3. Inventory.java (Driver) InventoryItem.java input file: inventory.txt (input file) (output file) Introduction to Java Y.Daniel Liang 2 File Input and Output Programs to run contd. 4. TestPrint.java 5. TestPrintWriter.java Team #1 output file: pw.txt 6. TestFileStream Team #2 7. TestDataStream Team #3 output file: temp.dat Introduction to Java Y.Daniel Liang 3 File Input and Output Programs to run contd. 8. Copy.java Team #4 Welcome.java (source file) Temp. java (target file) 9. TestObjectStreamForArray Team #5 Introduction to Java Y.Daniel Liang 4 Objectives •To understand how I/O is processed in Java •To distinguish between text I/O and binary I/O •To read data from the console using the Scanner class •To read data from a file using the Scanner class Introduction to Java Y.Daniel Liang 5 Objectives contd. •To write data to a file using the PrintWriter class •To read and write bytes using FileInput Stream and FileOutputStream •To read and write primitive values and strings using DataInputStream/ DataOutPutStream Introduction to Java Y.Daniel Liang 6 Objectives contd. •To store and restore objects using ObjectOutputStream and ObjectInputStream and to understand how objects are serialized and what kind of objects can be serialized To use the Serializable interface to enable objects to be serializable To know how to serialize arrays Introduction to Java Y.Daniel Liang 7 Input and Output Introduction Data held in variables, arrays, and objects are temporary. Data are lost when the program ceases processing To save the data created during the life of the program, you must save the data in a file onto a disk, CD, or some other storage device . Then the file can be transported and can be read later by other programs Introduction to Java Y.Daniel Liang 8 Introduction Redirection of Input and Output It is tedious to test programs by typing data in for every test run. Testing is far easier if the program reads input from a file. You can prepare the file once and reuse it for many tests. The command line interfaces of most operating systems provide a way to link a file to the input of a program, as if all the characters in the file had actually been typed by the user . Introduction to Java Y.Daniel Liang 9 Introduction Redirection of Input and Output example: java ClassName < data.txt > output.txt Use input redirection to avoid repetitive typing during testing. Use output redirection to save your program output in a file. Introduction to Java Y.Daniel Liang 10 Introduction contd. Data are stored in two basic formats: text and binary Text file is represented in human-readable form Examples of text files that you can read: Files that are created with a simple text exitor , such as Windows Vista NotePad, as well as Java source code, and HTML Files Introduction to Java Y.Daniel Liang 11 Introduction contd. Data stored in a binary file are represented in binary form. You cannot read binary files. The files are designed to be read by programs. Binary files consists of a series of bits Text file consist of a series of characters. Introduction to Java Y.Daniel Liang 12 Introduction contd. The advantages of binary files are that: 1. They are more efficient to process than text files. Text I/O requires encoding and decoding whereas binary I/O does not. 2. Binary files are independent of the encoding scheme on the host machine and thus are portable. Introduction to Java Y.Daniel Liang 13 The Scanner class The simplest tool for reading text files is the Scanner class. Class java.util.Scanner Introduction to Java Y.Daniel Liang 14 to Get Input from the Console Using the Scanner Class Run ScannerDriver program in the FileIO Folder next(): reading a string. A string delimeter is a space nextByte(): reading an integer of the byte type nextShort(): reading an integer of the short type nextInt(): reading an integer of the int type nextLong(): reading an integer of the long type nextFloat(): reading a number of the float type nextDouble(): reading number of the double type Introduction to Java Y.Daniel Liang 15 To Get Input from a text file Using the Scanner Class Run ScannerFile Program in the FileIO Folder The action of reading data from a file is called file input. Introduction to Java Y.Daniel Liang 16 To read text data from a disk file, we use the FileReader and BufferedReader objects. • We first construct a FileReader object with the name of the file • Then we associate a BufferedReader object to the file. • Then we read data, using the readLine method of the BufferedReader class. • Finally, we convert the String to a primitive data type as necessary. • Run Programs: InventoryItem (Driver) Inventory Use input file: inventory.txt Introduction to Java Y.Daniel Liang 17 Sample code for processing a text file: String file = “customer.txt”; String line; try{ FileReader fr = new FileReader (file); BufferedReader inFile = new BufferedReader(fr); line = inFile.readLine(); while(line != null && count < MAX) { tokenizer = new StringTokenizer(line); name = tokenizer.nextToken(); Introduction to Java Y.Daniel Liang 18 Cont’d. try{ custNumber = Long.parseLong (tokenizer. nextToken()); balance = Double.parseDouble(tokenizer. nextToken()); phone = tokenizer.nextToken(); custsArray[count++] = new Customer(name, custNumber, balance, phone); } // end inner try block catch (NumberFormatException e) { } line = inFile.readLine(); }// end while block inFile.close(); Introduction to Java Y.Daniel Liang 19 PrintWriter is an object we use to generate an output text file. PrintWriter supports only two output methods: print println (for print line) Run program TestPrinterWriter output file: pw.txt Introduction to Java Y.Daniel Liang 20 Constructors of the PrintWriter • PrintWriter(Writer out) • PrintWriter(Writer out, boolean autoFlush) • PrintWriter(OutputStream out) • PrintWriter(OutputStream out, boolean autoFlush) Introduction to Java Y.Daniel Liang 21 PrintWriter Methods • void print(Object o) • void print(String s) • void println(String s) • void print(char c) • void print(char[] cArray) • • • • void void void void print(int i) print(long l) print(float f) print(double d) • void print(boolean b) Introduction to Java Y.Daniel Liang 22 Streams In Java, all I/O is handled in streams. A stream is an abstraction of the continuous one-way flow of data. The program receives data thru the input stream and sends data thru the output stream. Any source of input or destination for output is called a stream. Input Stream Program File Output Stream Introduction to Java Y.Daniel Liang 23 Streams All streams except random-access file streams flow in only one direction; therefore if you want to input and output, you need two separate stream objects. Streams are objects. Stream objects have methods that read and write data , flush the stream, close the stream, and count the number of bytes in the stream, etc. Introduction to Java Y.Daniel Liang 24 Binary I/O Text I/O requires encoding and decoding. The JVM converts a Unicode to a file specific encoding when writing a character and coverts a file specific encoding to a Unicode when reading a character. Binary I/O does not require conversions. When you write a byte to a file, the original byte is copied into the file. When you read a byte from a file, the exact byte in the file is returned. Text I/O program (a) The Unicode of the character e.g. "199" , Encoding/ Decoding The encoding of the character is stored in the file 00110001 00111001 00111001 0x31 0x39 0x39 Binary I/O program (b) A byte is read/written e.g. 199 , The same byte in the file 00110111 0xC7 Introduction to Java Y.Daniel Liang 25 25 Binary I/O Classes The design of the Java I/O classes is a good example of applying inheritance, where common methods are generalized in superclasses, and sub classes provide specialized methods. See next slide. It list some of the classes for performing binary I/O. InputStream is the root for binary input classes. OutputStream is the root for binary output classes Introduction to Java Y.Daniel Liang 26 Binary I/O Classes FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object OutputStream FileOutputStream BufferedOutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream Introduction to Java Y.Daniel Liang 27 27 Stream Classes The stream classes can be categorized into two types: byte streams and character streams. The InputStream/OutputStream class is the root of all byte stream classes, the Reader/Writer class is the root of all character stream. The subclasses of InputStream/ OutputStream are analogous to the subclasses of Reader/Writer. Introduction to Java Y.Daniel Liang 28 To Read and Write Binary Data from /To a Disk File FileInputStream and FileOutputStream- all methods in these classes are inherited from InputStream and OutputStream (java.io. FileInputStream and java.io.FileOutputStream ) Create a FileInputStream: FileInputStream inputStream = new FileInputStream inputStream ( “input.bin”); To write Binary Data to a Disk File: FileOutputStream outputStream = new FileOutputStream outputStream ( “output.bin”); Introduction to Java Y.Daniel Liang 29 InputStream java.io.InputStream int read(byte[] b) throws IOException abstract int read() throws IOException void close() throws IOException int available() throws IOException long skip(long n) throws IOException Note: The abstract InputStream class defines the methods for the input stream of bytes Introduction to Java Y.Daniel Liang 30 OutputStream java.io.OutputStream abstract void write(int b) throws IOException void write(byte[] b) throws IOException void close() throws IOException void flush() throws IOException Note: The abstract OutputStream class defines the methods for the output stream of bytes. Introduction to Java Y.Daniel Liang 31 FileInputStream/FileOutputStream FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object OutputStream FileOutputStream BufferedOutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream FileInputStream/FileOutputStream associates a binary input/output stream with an external file. All the methods in FileInputStream/FileOuptputStream are inherited from its superclasses. Introduction to Java Y.Daniel Liang 32 FileInputStream To construct a FileInputStream, use the following constructors: public FileInputStream(String filename) public FileInputStream(File file) A java.io.FileNotFoundException would occur if you attempt to create a FileInputStream with a nonexistent file. Introduction to Java Y.Daniel Liang 33 FileOutputStream To construct a FileOutputStream, use the following constructors: public FileOutputStream(String filename) public FileOutputStream(File file) public FileOutputStream(String filename, boolean append) public FileOutputStream(File file, boolean append) If the file does not exist, a new file would be created. If the file already exists, the first two constructors would delete the current contents in the file. To retain the current content and append new data into the file, use the last two constructors by passing true to the append parameter. Introduction to Java Y.Daniel Liang 34 FileOutputStream The program TestFileStream uses binary I/O to write ten bytes values from 1 to 10 to a file named temp.dat TestFileStream Introduction to Java Y.Daniel Liang Run 35 FilterInputStream/FilterOutputStream FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object OutputStream FileOutputStream BufferedOutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream Filter streams are streams that filter bytes for some purpose. The basic byte input stream provides a read method that can only be used for reading bytes. If you want to read integers, doubles, or strings, you need a filter class to wrap the byte input stream. Using a filter class enables you to read integers, doubles, and strings instead of bytes and characters. FilterInputStream and FilterOutputStream are the base classes for filtering data. When you need to process primitive numeric types, use DatInputStream and DataOutputStream to filter bytes. Introduction to Java Y.Daniel Liang 36 DataInputStream/DataOutputStream DataInputStream reads bytes from the stream and converts them into appropriate primitive type values or strings. FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object OutputStream FileOutputStream BufferedOutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream DataOutputStream converts primitive type values or strings into bytes and output the bytes to the stream. Introduction to Java Y.Daniel Liang 37 DataInputStream DataInputStream extends FilterInputStream and implements the DataInput interface. InputStream FilterInputStream DataInputStream +DataInputStream( in: InputStream) java.io.DataInput +readBoolean(): boolean Reads a Boolean from the input stream. +readByte(): byte Reads a byte from the input stream. +readChar(): char Reads a character from the input stream. +readFloat(): float Reads a float from the input stream. +readDouble(): float Reads a double from the input stream. +readInt(): int Reads an int from the input stream. +readLong(): long Reads a long from the input stream. +readShort(): short Reads a short from the input stream. +readLine(): String Reads a line of characters from input. +readUTF(): String Reads a string in UTF format. Introduction to Java Y.Daniel Liang 38 DataOutputStream DataOutputStream extends FilterOutputStream and implements the DataOutput interface. OutputStream FilterOutputStream DataOutputStream +DataOutputStream( out: OutputStream) java.io.DataOutput +writeBoolean(b: Boolean): void Writes a Boolean to the output stream. +writeByte(v: int): void Writes to the output stream the eight low-order bits of the argument v. +writeBytes(s: String): void Writes the lower byte of the characters in a string to the output stream. +writeChar(c: char): void Writes a character (composed of two bytes) to the output stream. +writeChars(s: String): void Writes every character in the string s, to the output stream, in order, two bytes per character. +writeFloat(v: float): void Writes a float value to the output stream. +writeDouble(v: float): void Writes a double value to the output stream. +writeInt(v: int): void Writes an int value to the output stream. +writeLong(v: long): void Writes a long value to the output stream. +writeShort(v: short): void Writes a short value to the output stream. +writeUTF(s: String): void Writes two bytes of length information to the output stream, followed by the UTF representation of every character in the string s. Introduction to Java Y.Daniel Liang 39 Characters and Strings in Binary I/O A Unicode consists of two bytes. The writeChar(char c) method writes the Unicode of character c to the output. The writeChars(String s) method writes the Unicode for each character in the string s to the output. Introduction to Java Y.Daniel Liang 40 Characters and Strings in Binary I/O Why UTF-8? What is UTF-8? UTF-8 (8-bit UCS/Unicode Transformation Format) is a coding scheme that allows systems to operate with both ASCII and Unicode efficiently. Most operating systems use ASCII. Java uses Unicode. The ASCII character set is a subset of the Unicode character set. Since most applications need only the ASCII character set, it is a waste to represent an 8-bit ASCII character as a 16-bit Unicode character. Introduction to Java Y.Daniel Liang 41 41 Characters and Strings in Binary I/O Why UTF-8? What is UTF-8? The UTF-8 is an alternative scheme that stores a character using 1, 2, or 3 bytes. ASCII values (less than 0x7F) are coded in one byte. Unicode values less than 0x7FF are coded in two bytes. Other Unicode values are coded in three bytes. Introduction to Java Y.Daniel Liang 42 42 Using DataInputStream/DataOutputStream Data streams are used as wrappers on existing input and output streams to filter data in the original stream. They are created using the following constructors: public DataInputStream(InputStream instream) public DataOutputStream(OutputStream outstream) The statements given below create data streams. The first statement creates an input stream for file in.dat; the second statement creates an output stream for file out.dat. DataInputStream infile = new DataInputStream(new FileInputStream("in.dat")); DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat")); Introduction to Java Y.Daniel Liang Using DataInputStream/DataOutputStream DataInputStream and DataOutputStream read and write data primitive types values and strings in a machine-independent fashion. Thereby enabling you to write a data file on one machine and read it on another machine that has a different operating system or file structure. An application uses a data output stream to write data that can later be read by a program using a data input stream. Introduction to Java Y.Daniel Liang Using DataInputStream/DataOutputStream The TestDataStream program writes student names and scores to a file named temp.dat and reads the data back from the file. TestDataStream Introduction to Java Y.Daniel Liang Run Order and Format See program TestDataStream CAUTION: You have to read the data in the same order and same format in which they are stored. For example, since names are written in UTF-8 using writeUTF, you must read names using readUTF. Checking End of File TIP: If you keep reading data at the end of a stream, an EOFException would occur. So how do you check the end of a file? You can use input.available() to check it. input.available() == 0 indicates that it is the end of a file. Introduction to Java Y.Daniel Liang 46 BufferedInputStream/BufferedOutputStream Reduces the number of reads and writes Using buffers to speed up I/O FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object OutputStream FileOutputStream BufferedOutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream BufferedInputStream/BufferedOutputStream does not contain new methods. All the methods BufferedInputStream/BufferedOutputStream are inherited from the InputStream/OutputStream classes. 47 Introduction to Java Y.Daniel Liang 47 Constructing BufferedInputStream/BufferedOutputStream // Create a BufferedInputStream public BufferedInputStream(InputStream in) public BufferedInputStream(InputStream in, int bufferSize) // Create a BufferedOutputStream public BufferedOutputStream(OutputStream out) public BufferedOutputStream(OutputStream out, int bufferSize) Introduction to Java Y.Daniel Liang 48 Constructing BufferedInputStream/BufferedOutputStream Improve the performance of the TestDataStream program by adding buffers in the streams in lines 6-7 and 21-22, as follows: DataOutputStream output = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(“temp.dat”)); DataInputStream input = new DataInputStream( new BufferedInputStream(new FileInputStream(“temp.dat”)); Introduction to Java Y.Daniel Liang 49 Case Studies: Copy File This case study develops a program that copies files. The user needs to provide a source file and a target file as command-line arguments using the following command: java Copy source target The program copies a source file to a target file and displays the number of bytes in the file. If the source does not exist, tell the user the file is not found. If the target file already exists, tell the user the file already exists. Introduction to Java Y.Daniel Liang 50 50 Case Studies: Copy File The program uses the File class to check whether the source file and the target file exist. The program copies a Java source file to an identical Java file Copy Introduction to Java Y.Daniel Liang Run 51 51 Optional Object I/O DataInputStream/DataOutputStream enables you to perform I/O for objects in addition to primitive type values and strings. ObjectInputStream/ObjectOutputStream enables you to perform I/O for objects in addition for primitive type values and strings. FileInputStream DataInputStream InputStream FilterInputStream BufferedInputStream ObjectInputStream Object OutputStream FileOutputStream BufferedOutputStream FilterOutputStream DataOutputStream ObjectOutputStream PrintStream Introduction to Java Y.Daniel Liang 52 ObjectInputStream ObjectInputStream extends InputStream and implements ObjectInput and ObjectStreamConstants. java.io.InputStream ObjectStreamConstants java.io.DataInput java.io.ObjectInputStream +ObjectInputStream(in: InputStream) java.io.ObjectInput +readObject(): Object Introduction to Java Y.Daniel Liang Reads an object. 53 ObjectOutputStream ObjectOutputStream extends OutputStream and implements ObjectOutput and ObjectStreamConstants. ObjectStreamConstants java.io.OutputStream java.io.DataOutput java.io.ObjectOutput java.io.ObjectOutputStream +ObjectOutputStream(out: OutputStream) +writeObject(o: Object): void Writes an object. Introduction to Java Y.Daniel Liang 54 Using Object Streams You may wrap an ObjectInputStream/ObjectOutputStream on any InputStream/OutputStream using the following constructors: // Create an ObjectInputStream public ObjectInputStream(InputStream in) // Create an ObjectOutputStream public ObjectOutputStream(OutputStream out) TestObjectOutputStream Run TestObjectInputStream Run Introduction to Java Y.Daniel Liang 55 Object Streams The ObjectInputStream class can save entire object out to a disk, and the C class can read them back in. Objects are saved in binary format; therefore, you use streams and not writers. For example, you can write a Bank Account object to a file: BankAccount bk = new …; ObjectOutputStream out = new ObjectOutputStream ( new FileOutputStream(“bank.dat”)); Out.writeObject(bk); Introduction to Java Y.Daniel Liang 56 Object Streams Use Object streams to save and restore all instance fields of an object automatically. When reading the object back in, you use the readObject method of the ObjectInputStream class. That method returns an Object reference, so you need to remember the types of the objects that you saved and use a cast: ObjectInputStream in = new ObjectInputStream ( new FileInputStream(“bank.dat”)); BankAccount bk = (BankAccount) in.readObject( ); The readObject method can throw a ClassNotFoundExceptionit is a checked exception, so you need to catch or declare it. Introduction to Java Y.Daniel Liang 57 The Serializable Interface Not all objects can be written to an output stream. Objects that can be written to an object stream is said to be serializable. A serializable object is an instance of the java.io.Serializable interface. So the class of a serializable object must implement Serializable. The Serializable interface is a marker interface. It has no methods, so you don't need to add additional code in your class that implements Serializable. public MyClass implements Serializable{ // Definition of the class } Implementing this interface enables the Java serialization mechanism to automate the process of storing the objects and arrays. Introduction to Java Y.Daniel Liang 58 The transient Keyword If an object is an instance of Serializable, but it contains non-serializable instance data fields, can the object be serialized? The answer is no. To enable the object to be serialized, you can use the transient keyword to mark these data fields to tell the JVM to ignore these fields when writing the object to an object stream. Introduction to Java Y.Daniel Liang 59 The transient Keyword, cont. Consider the following class: public class Foo implements java.io.Serializable { private int v1; private static double v2; private transient A v3 = new A(); } class A { } // A is not serializable When an object of the Foo class is serialized, only variable v1 is serialized. Variable v2 is not serialized because it is a static variable, and variable v3 is not serialized because it is marked transient. If v3 were not marked transient, a java.io.NotSerializableException would occur. Introduction to Java Y.Daniel Liang 60 Serializing Arrays An array is serializable if all its elements are serializable. So an entire array can be saved using writeObject into a file and later restored using readObject. Listing 16.12 stores an array of five int values an array of three strings, and an array of two JButton objects, and reads them back to display on the console. Note : java.io.File implements Comparable and Serializable TestObjectStreamForArray Introduction to Java Y.Daniel Liang Run 61 More about the Serializable Interface The process of saving objects to a stream is called serialization because each object is assigned a serial number on the stream. If the same object is saved twice, only the serial number is written out the second time. When the objects are read back in, duplicate serial numbers are restored as reference to the same object. Why don’t all classes implement Serializable? For security reasons, some programmers may not want to serialize classes with confidential contents. Once a class is serializable, anyone can write its objects to disk and analyze the disk file. There are also some classes that contain values that are meaningless once a program exists, such as operating–system-specific font descriptors. These values should not be serialized. Introduction to Java Y.Daniel Liang 62 Random Access Files All of the streams you have used so far are known as read-only or write-only streams. The external files of these streams are sequential files that cannot be updated without creating a new file. It is often necessary to modify files or to insert new records into files. Java provides the RandomAccessFile class to allow a file to be read from and writen to at random locations. Introduction to Java Y.Daniel Liang 63 RandomAccessFile DataInput DataInput java.io.RandomAccessFile +RandomAccessFile(file: File, mode: String) Creates a RandomAccessFile stream with the specified File object and mode. +RandomAccessFile(name: String, mode: String) Creates a RandomAccessFile stream with the specified file name string and mode. +close(): void Closes the stream and releases the resource associated with the stream. +getFilePointer(): long Returns the offset, in bytes, from the beginning of the file to where the next read or write occurs. +length(): long Returns the length of this file. +read(): int Reads a byte of data from this file and returns –1 an the end of stream. +read(b: byte[]): int Reads up to b.length bytes of data from this file into an array of bytes. +read(b: byte[], off: int, len: int) : int Reads up to len bytes of data from this file into an array of bytes. +seek(long pos): void Sets the offset (in bytes specified in pos) from the beginning of the stream to where the next read or write occurs. +setLength(newLength: long): void Sets a new length of this file. +skipBytes(int n): int Skips over n bytes of input discarding the skipped bytes. +write(b: byte[]): void +write(byte b[], int off, int len) Writes b.length bytes from the specified byte array to this file, starting at the current file pointer. +write(b: byte[], off: int, len: int): void Writes len bytes from the specified byte array starting at offset off to this file. Introduction to Java Y.Daniel Liang 64