Input and Output Text and Binary I/O: Introduction To Java Y.Daniel Liang 1
Input and Output Text and Binary I/O: Introduction To Java Y.Daniel Liang 1
Input and Output Text and Binary I/O: 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
File Input and Output Programs to run contd. 4. TestPrint.java 5. TestPrintWriter.java Team #1 output file: pw.txt 6. TestFileStream Team #2
8. Copy.java Team #4
Welcome.java (source file) Temp. java (target file) 9. TestObjectStreamForArray Team #5
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
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
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.
10
Introduction contd. Data are stored in two basic formats: text and binary
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
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
14
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
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();
Contd. 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)
20
21
PrintWriter Methods
void print(Object o) void print(String s) void println(String s) void print(char c) void print(char[] cArray) void print(int i) void print(long l) void print(float f)
void print(double d)
void print(boolean b)
Introduction to Java Y.Daniel Liang 22
Streams
In Java, all I/O is handled in streams.
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.
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
25
25
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
26
27
27
Stream Classes
The stream classes can be categorized into two types: byte streams and character streams.
28
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
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
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 FileOutputStream OutputStream FilterOutputStream ObjectOutputStream BufferedOutputStream DataOutputStream 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.
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.
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 FileOutputStream OutputStream FilterOutputStream ObjectOutputStream BufferedOutputStream DataOutputStream 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 FileOutputStream OutputStream FilterOutputStream ObjectOutputStream BufferedOutputStream DataOutputStream 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 java.io.DataInput +readBoolean(): boolean Reads a Boolean from the input stream. +readByte(): byte Reads a byte from the input stream. +readChar(): char +readFloat(): float +readDouble(): float DataInputStream +DataInputStream( in: InputStream) +readInt(): int +readLong(): long +readShort(): short +readLine(): String +readUTF(): String Reads a character from the input stream. Reads a float from the input stream. Reads a double from the input stream. Reads an int from the input stream. Reads a long from the input stream. Reads a short from the input stream. Reads a line of characters from input. Reads a string in UTF format.
FilterInputStream
38
DataOutputStream
DataOutputStream extends FilterOutputStream and implements the DataOutput interface.
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 +writeInt(v: int): void +writeLong(v: long): void +writeShort(v: short): void +writeUTF(s: String): void Writes a double value to the output stream. Writes an int value to the output stream. Writes a long value to the output stream. Writes a short value to the output stream. Writes two bytes of length information to the output stream, followed by the UTF representation of every character in the string s.
FilterOutputStream
39
40
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.
41
41
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.
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.
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
46
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)
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));
49
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
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
52
ObjectInputStream
ObjectInputStream extends InputStream and implements ObjectInput and ObjectStreamConstants.
java.io.InputStream
53
ObjectOutputStream
ObjectOutputStream extends OutputStream and implements ObjectOutput and ObjectStreamConstants.
java.io.OutputStream
54
TestObjectOutputStream TestObjectInputStream
Introduction to Java Y.Daniel Liang
Run
Run
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);
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
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
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 dont 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 operatingsystem-specific font descriptors. These values should not be serialized.
Introduction to Java Y.Daniel Liang 62
RandomAccessFile
DataInput java.io.RandomAccessFile +RandomAccessFile(file: File, mode: String) +RandomAccessFile(name: String, mode: String) +close(): void +getFilePointer(): long +length(): long +read(): int +read(b: byte[]): int +read(b: byte[], off: int, len: int) : int +seek(long pos): void +setLength(newLength: long): void +skipBytes(int n): int +write(b: byte[]): void +write(byte b[], int off, int len) +write(b: byte[], off: int, len: int): void Creates a RandomAccessFile stream with the specified File object and mode. Creates a RandomAccessFile stream with the specified file name string and mode. Closes the stream and releases the resource associated with the stream. Returns the offset, in bytes, from the beginning of the file to where the next read or write occurs. Returns the length of this file. Reads a byte of data from this file and returns 1 an the end of stream. Reads up to b.length bytes of data from this file into an array of bytes. Reads up to len bytes of data from this file into an array of bytes. Sets the offset (in bytes specified in pos) from the beginning of the stream to where the next read or write occurs. Sets a new length of this file. Skips over n bytes of input discarding the skipped bytes. Writes b.length bytes from the specified byte array to this file, starting at the current file pointer. Writes len bytes from the specified byte array starting at offset off to this file. DataInput
64