50
Chapter 9 1 Streams and File I/O Chapter 9

Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 1

Streams and File I/O

Chapter 9

Page 2: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 2

Reminders

• Project 6 released: due Nov 03 @ 10:30 pm• Project 4 regrades due by midnight tonight• Discussion groups now twice a week (7-9

pm: M in CS G066, W in Phys 11)

Page 3: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 3

Exam 2• Tuesday, October 25• 7:00 – 8:00 PM• Physics 112• Covers chapters 5-9 (and material from 1-4

as well)• Similar format as Exam 1

– 20 MC– 4 programming

Page 4: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 4

Why Use Files for I/O?

• Keyboard input and screen output deal only with temporary data, which is lost when the program ends.

• Files permit data to be stored permanently (or at least until a program changes the file).

• Input files can be used over and over by different programs.

• Files also provide convenient storage and retrieval of large quantities of data.

Page 5: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 5

Text Files and Binary Files

• All data in a file is stored as binary digits.– Files with contents that must be treated as

sequences of binary digits are called binary files; binary files can be read only by machines.

Page 6: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 6

Text Files and Binary Files,cont.

• Sometimes, it is more convenient to think of a file’s contents as a sequence of characters.– Files with streams and methods to make

them look like sequences of characters are called text files; text files can be read by people.

Page 7: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 7

Text Files and Binary Files, cont.

• However, all files are binary in the computer– Just a sequence of 0s and 1s: it’s how we

(or programs) interpret those 0s and 1s

Page 8: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 8

Text File Output Example

Page 9: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 9

Use toString for Output

Page 10: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 10

Text File Input Example

Page 11: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 11

The StringTokenizer Class, cont.

• Tokens are nonwhitespace characters.• example

StringTokenizer tokenizer = new StringTokenizer(“Read my lips!”)

while (tokenizer.hasMoreTokens()){

System.out.println (tokenizer.nextToken());

}

Page 12: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 12

The StringTokenizer Class, cont.

• This will produceReadmylips!

Page 13: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 13

The StringTokenizer Class, cont.

• Separators are whitespace characters unless otherwise specified.

• To specify a set of separators, a string consisting of all the separator characters is given as a second argument to the constructor.

• example… new StringTokenizer(“Read my lips!”, “\n.,!”);

Page 14: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 14

Some Methods in Class StringTokenizer

• constructorspublic StringTokenizer(String theString)

public StringTokenizer(String theString, String delimiters)

• more tokens?public boolean hasMoreTokens()

• next tokenpublic String nextToken()

• remaining tokenspublic int countTokens()

Page 15: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 15

Example: StringTokenizer• Display the words separated by any of the following

characters: space, new line (\n), period (.) or comma (,).

String inputLine = keyboard.nextLine();StringTokenizer wordFinder = new StringTokenizer(inputLine, " \n.,");

while(wordFinder.hasMoreTokens()){ System.out.println(wordFinder.nextToken());}

Page 16: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 16

Example: StringTokenizer

String inputLine = keyboard.nextLine();StringTokenizer wordFinder = new StringTokenizer(inputLine, " \n.,");

while(wordFinder.hasMoreTokens()){ System.out.println(wordFinder.nextToken());}

Question2bor!tooBee

Entering "Question,2b.or !tooBee." gives this output:

Page 17: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 17

Java Tip: Testing for the End of a Text File

• When method readLine in class BufferedReader attempts to read beyond the end of a file, the method returns the value null.

• When method read attempts to read beyond the end of a file, the method returns the value -1.

Page 18: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 18

Using the File Class, cont.

Page 19: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 19

Binary Files

• Binary files store data in the same format used for main memory.

• Bytes in main memory and bytes in binary files are read similarly, which leads to efficiency.

• Binary files created by a Java program on one computer can be read by a Java program on a different computer.

Page 20: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 20

Binary Files, cont.• Class ObjectInputStream and class

ObjectOutputStream are used to process binary files.– Data is read or written, one byte at a time.– Numbers and characters are converted

automatically to bytes for storage in a binary file.

– Data in files can be treated as Java primitive data types, as strings, or as other objects.

Page 21: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 21

Opening a Binary File

• syntaxObjectOutputStream Output_Stream_Name =

new ObjectOutputStream (new FileOutputStream(File_Name));

• exampleObjectOutputStream myOutputStream =

new ObjectOutputStream (new FileOutputStream

(“myfile.dat”));

Page 22: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 22

ObjectOutputStream

Page 23: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 23

Output to Binary Files Using ObjectOutputStream,

cont.• The numbers are not in human-readable form

because there are no lines or other separators.

Page 24: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 24

Some Methods in Class ObjectOutputStream

• to createpublic ObjectOutputStream(OutputStream

streamObject)• to create a stream

new ObjectOutputStream (new FileOutputStream (File_Name_or_File_Object))

• to write a primitive typepublic void writeInt(int n) throws IOException

Page 25: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 25

Some Methods in Class ObjectOutputStream,

cont.• to write a primitive type, cont.

public void writeLong(long n) throws IOException

public void writeDouble(double x) throws IOException

public void writeFloat(float x) throws IOException

Page 26: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 26

Some Methods in Class ObjectOutputStream,

cont.public void writeChar(int n) throws IOException

public void writeBoolean(boolean b) throws IOException

• to write a Stringpublic void writeUTF(String aString)

throws IOException

Page 27: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 27

Some Methods in Class ObjectOutputStream,

cont.• There is no method writeString.• Instead, use method writeUTF.• UTF stands for Unicode Text Format.• UTF provides short, efficient codes for ASCII

characters.

Page 28: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 28

Some Methods in Class ObjectOutputStream,

cont.• To write an object

public void writeObject(Object anObject) throws IOException,

NotSerializableException, InvalidClassException

• to closepublic void close() throws IOException

Page 29: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 29

Different Types in the Same File

• Different types can be written to the same file.• However, the different types must be read

from the file just as they were written to the file.

Page 30: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 30

Reading Input from a Binary File Using

ObjectInputStream• A file written using ObjectOutputStream can be

read using ObjectInputStream.• The methods in class ObjectInputStream

correspond to the methods in class ObjectOutputStream.

Page 31: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 31

Some Methods in Class ObjectInputStream

• to createObjectInputStream (InputStream streamObject)

• to create a streamnew ObjectInputStream (new

FileInputStream (File_Name_or_File_Object)

• to read a primitive typepublic int readInt() throws IOException

Page 32: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 32

Some Methods in Class ObjectInputStream, cont.• to read a primitive type, cont.

public long readLong() throws IOException

public double readDouble() throws IOException

public float readFloat() throws IOException

public char readChar() throws IOException

public boolean ReadBoolean() throws IOException

Page 33: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 33

Some Methods in Class ObjectInputStream, cont.• to read a String

public String readUTF() throws IOException

• to read an objectpublic Object readObject()

throws ClassNotFoundException, InvalidClassException, OptionalDataException, IOException

• to closepublic void close() throws IOException

Page 34: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 34

Opening an Input File

• syntaxObjectInputStream Input_Stream_Name = new ObjectInputStream(new FileInputStream(File_Name));

• exampleObjectInputStream myInputStream = new ObjectInputStream(new FileInputStream(“myfile.dat”));

Page 35: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 35

The EOFException Class

• ObjectInputStream methods that read from a binary file throw an EOFException when they try to read beyond the end of the file.

• When using class ObjectInputStream, the class EOFException can test for the end of a file.

Page 36: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 36

The EOFException Class

Page 37: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 37

Checking for the End of File

• Different classes with file reading methods check for the end of a file in different ways.– Binary files throw an exception in the class EOFException.

– A text file returns a special value, such as null.

• Be sure to test for the end of the file in the correct way.

Page 38: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 38

The Classes FileInputStream and FileOutputStream

• We used stream class FileInputStream when we created a stream of class ObjectInputStream.

• We used stream class FileOutputStream when we created a stream of class ObjectOutputStream.

Page 39: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 39

Binary I/O of Class Objects

• Using method writeObject of class ObjectOutputStream you can output class objects to a binary file, and then read objects from the file using method readObject of class ObjectInputStream.

• However, the class being written and read must be serializable.

Page 40: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 40

Binary I/O of Class Objects, cont.

• To make a class serializable, add implements Serializable to the class heading.

• examplepublic class SomeClass implements

Serializable• The Serializable interface is available after

importing java.io.*

Page 41: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 41

Binary I/O of Class Objects,

Page 42: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 42

Page 43: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 43

Files and toString• Method toString provides convenient output

to the screen and to a text file.• However, method toString is not needed for

object I/O to a binary file.

Page 44: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 44

The Serializable Interface

• A class which is serializable affects how Java performs file I/O with objects of the class.– Java assigns a serial number to each

object of the class that it writes to a stream of type ObjectOutputStream.

– If the object is written more than once, Java writes only the serial number for the object.

Page 45: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 45

Graphics Supplement

Page 46: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 46

Graphics Supplement, cont.

Page 47: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 47

Graphics Supplement, cont.

Page 48: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 48

Graphics Supplement, cont.

Page 49: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 49

Summary

• You have become familiar with the concept of an I/O stream.

• You now understand the difference between binary files and text files.

• You have learned how to save data in a file.• You have learned how to read data from a

file.

Page 50: Chapter 9 Streams and File I/O - Purdue Universitycs180/Fall2005Web/slides/rec... · 2005. 10. 21. · Chapter 9 4 Why Use Files for I/O? • Keyboard input and screen output deal

Chapter 9 50

Summary, cont.

• You have learned how use the classes ObjectOutputStream and ObjectInputStream to read and write class objects with binary files.