Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Y.Daniel Liang

Similar presentations


Presentation on theme: "Introduction to Java Y.Daniel Liang"— Presentation transcript:

1 Introduction to Java Y.Daniel Liang
Input and Output Text and Binary I/O Chapter 19 Introduction to Java Y.Daniel Liang

2 Introduction to Java Y.Daniel Liang
File Input and Output Programs to run are in the FileIO folder: 1. ScannerDriver.java 2. ScannerFile.java ScannerTextfile.txt (input file) ScannerOutputfile.txt (output file) 3. Inventory.java (Driver) InventoryItem.java input file: inventory.txt Introduction to Java Y.Daniel Liang

3 Introduction to Java Y.Daniel Liang
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

4 Introduction to Java Y.Daniel Liang
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

5 Introduction to Java Y.Daniel Liang
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

6 Introduction to Java Y.Daniel Liang
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

7 Introduction to Java Y.Daniel Liang
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

8 Introduction to Java Y.Daniel Liang
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

9 Introduction to Java Y.Daniel Liang
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

10 Introduction to Java Y.Daniel Liang
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

11 Introduction to Java Y.Daniel Liang
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

12 Introduction to Java Y.Daniel Liang
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

13 Introduction to Java Y.Daniel Liang
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

14 Introduction to Java Y.Daniel Liang
The Scanner class The simplest tool for reading text files is the Scanner class. Class java.util.Scanner Introduction to Java Y.Daniel Liang

15 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

16 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

17 Introduction to Java Y.Daniel Liang
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

18 Introduction to Java Y.Daniel Liang
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

19 Introduction to Java Y.Daniel Liang
Cont’d. try{ custNumber = Long.parseLong (tokenizer. nextToken()); balance = Double.parseDouble(tokenizer. 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

20 Introduction to Java Y.Daniel Liang
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

21 Introduction to Java Y.Daniel Liang
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

22 Introduction to Java Y.Daniel Liang
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

23 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. Introduction to Java Y.Daniel Liang Introduction to Java Y.Daniel Liang

24 Introduction to Java Y.Daniel Liang
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

25 Introduction to Java Y.Daniel Liang
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. Introduction to Java Y.Daniel Liang 25

26 Introduction to Java Y.Daniel Liang
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

27 Introduction to Java Y.Daniel Liang
Binary I/O Classes Introduction to Java Y.Daniel Liang 27

28 Introduction to Java Y.Daniel Liang
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

29 Introduction to Java Y.Daniel Liang
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

30 Introduction to Java Y.Daniel Liang
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

31 Introduction to Java Y.Daniel Liang
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

32 FileInputStream/FileOutputStream
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

33 Introduction to Java Y.Daniel Liang
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

34 Introduction to Java Y.Daniel Liang
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

35 Introduction to Java Y.Daniel Liang
FileOutputStream The program TestFileStream uses binary I/O to write ten bytes values from 1 to 10 to a file named temp.dat TestFileStream Run Introduction to Java Y.Daniel Liang 35

36 FilterInputStream/FilterOutputStream
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

37 DataInputStream/DataOutputStream
DataInputStream reads bytes from the stream and converts them into appropriate primitive type values or strings. DataOutputStream converts primitive type values or strings into bytes and output the bytes to the stream. Introduction to Java Y.Daniel Liang

38 Introduction to Java Y.Daniel Liang
DataInputStream DataInputStream extends FilterInputStream and implements the DataInput interface. Introduction to Java Y.Daniel Liang

39 Introduction to Java Y.Daniel Liang
DataOutputStream DataOutputStream extends FilterOutputStream and implements the DataOutput interface. Introduction to Java Y.Daniel Liang

40 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

41 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

42 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

43 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

44 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

45 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 Run Introduction to Java Y.Daniel Liang

46 Introduction to Java Y.Daniel Liang
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

47 Introduction to Java Y.Daniel Liang
BufferedInputStream/BufferedOutputStream Reduces the number of reads and writes Using buffers to speed up I/O BufferedInputStream/BufferedOutputStream does not contain new methods. All the methods BufferedInputStream/BufferedOutputStream are inherited from the InputStream/OutputStream classes. Introduction to Java Y.Daniel Liang 47

48 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

49 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

50 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

51 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 Run Introduction to Java Y.Daniel Liang 51 51

52 Introduction to Java Y.Daniel Liang
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. Introduction to Java Y.Daniel Liang

53 Introduction to Java Y.Daniel Liang
ObjectInputStream ObjectInputStream extends InputStream and implements ObjectInput and ObjectStreamConstants. Introduction to Java Y.Daniel Liang 53

54 Introduction to Java Y.Daniel Liang
ObjectOutputStream ObjectOutputStream extends OutputStream and implements ObjectOutput and ObjectStreamConstants. Introduction to Java Y.Daniel Liang 54

55 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

56 Introduction to Java Y.Daniel Liang
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

57 Introduction to Java Y.Daniel Liang
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 ClassNotFoundException-it is a checked exception, so you need to catch or declare it. Introduction to Java Y.Daniel Liang

58 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

59 Introduction to Java Y.Daniel Liang
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

60 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

61 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 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 Introduction to Java Y.Daniel Liang

62 Introduction to Java Y.Daniel Liang
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

63 Introduction to Java Y.Daniel Liang
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

64 Introduction to Java Y.Daniel Liang
RandomAccessFile Introduction to Java Y.Daniel Liang


Download ppt "Introduction to Java Y.Daniel Liang"

Similar presentations


Ads by Google