55
Fall 2002 CS 150: Intro. to Computi ng 1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Embed Size (px)

Citation preview

Page 1: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 1

Streams and File I/O (That is, Input/Output)

OR

How you read data from files and write data to files

Page 2: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 2

Streams

• Abstractly, a stream is a flow of data– Data could be characters, numbers, bytes

consisting of binary digits, bytes consisting of binary encoding of objects, etc.

• If the data flows “out” of your program (and, say, to a file or the monitor) then the stream is an output stream

• If the data flows “in” to your program, then the stream is an input stream

Page 3: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 3

Streams

• In Java, file I/O (and also simple keyboard/monitor I/O) is handled by streams– In Java, a stream is an object that either

delivers data to its destination (such as a file or monitor) or takes data from a source (such as a file or keyboard) and delivers it to your program

– System.out is an example of an output stream

Page 4: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 4

Streams

Program

File,Monitor,Network,

Etc.

File,Keyboard,Network,

Etc.

output stream

input stream

Page 5: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 5

Remember:

• An input stream moves data into your program (not into a file)

• An output stream moves data out of your program (not out of the file)

Page 6: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 6

Binary vs Text Files• All data in any file is stored as a sequence of bits.

But, sometimes we “think” of the file as consisting of a sequence of characters (for example, your Java source code files), and some we think of as simply containing a sequence of binary digits (such as a file containing the machine code for a program)

• The files of characters are called text files• The files of bits are called binary files• Java has objects to handle I/O to both kinds of

files. We’ll only work with text files

Page 7: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 7

Text File I/O

• Best to just start with an example: we’ll add some file I/O to StudentRecord

Mustbe here!

Page 8: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 8

Text File I/O (cont.)

• We use the println() method in the class PrintWriter (not System.out.println(), but acts the same)

Returns a referenceto an OutputStreamobject

Page 9: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 9

Page 10: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 10

Page 11: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 11

What’s with these?!

Page 12: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 12

Another Look…

Page 13: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 13

What’s With This try…catch Thing?!

• This is an example of exception handling in Java (which we may cover more completely at a later date)

• For now, know that this says: execute the statements in the try block. If something goes wrong, then stop and execute the statements in the catch block– Lots can go wrong with file I/O (e.g. the file may not

exist, or you may not have permission to access it)

Page 14: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 14

Subtle and Very Important…If written thisway, the variableoutputStream is local to the try block!

Page 15: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 15

Still Another Look…Here it’svisible throughoutthe whole method

What’s with this?!

Page 16: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 16

So, How Do We Use These Things?!

• Just like you use System.out.println(), though there are also methods specially designed to print doubles, chars, etc.

Page 17: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 17

Writing To a File (cont.)This is a variable name!I could have called it anylegal variable name.

Page 18: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 18

Writing To a File (cont.)

Why don’t I needto use getName() here?

Page 19: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 19

Still More Writing To a File

Page 20: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 20

Page 21: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 21

close() the Stream!

• Calling the close() method:– Flushes the stream – Operating system releases all resources needed to

connect the stream to the file, and performs other housekeeping

– If stream isn’t closed, Java closes it when the program ends, but you’re taking a chance…

• If program ends abnormally, then Java may not be able to automatically close the stream and you could lose data

• You need to close a stream before reading from the same file

Page 22: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 22

A True Story…

Page 23: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 23

Why Ever flush() a Stream?

• There are situations where you want the stream to remain open (you’re still using it), but you need to be sure data goes to output device

• Writing to a network interface• During long operations on a file, flush stream in

case there is some kind of abnormality– OS crash, etc.

Page 24: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 24

Miscellaneous

• File names: The file name you give Java is simply a String. It doesn’t know about suffixes and the like. That’s the OS’s thing.

• Opening a text file for appending:Indicates open for appending

Page 25: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 25

Streams and File I/O Part II

Reading from Files

Page 26: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 26

BufferedReader Class• The BufferedReader class is the input

stream equivalent of the PrintWriter class– The constructor requires a similar setup– The class methods are analogous– Remember to import java.io.* – Remember this is for reading text files

Page 27: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 27

Page 28: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 28

The FileReader class is a subclass of the Reader class,so using it in the constructoris “legal”

Page 29: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 29

Page 30: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 30

What’s withthis overridething?

Note the exceptionsthat can be thrown

Page 31: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 31

Reading From Files

• As with writing to files, you need to know “where” you are in the file when reading

• Unlike with writing, you need to know when you have run out of stuff to read in the file

• When reading, you generally need to have a place (i.e. a variable) to put the data you have read

• Beware: there are many subtleties here

Page 32: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 32

Anything look strange here?

It should!

Page 33: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 33

Page 34: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 34

Example

• Some new file-reading code added to the code that created the file profsGrades.txt

Page 35: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 35

Example (cont.)

Adding a simple cast to a char will fix this problem,but there are others (see next slide)

Page 36: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 36

Page 37: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 37

Note the Changes…

• The entirety of the input code is in the try block (including closing the stream), not just the memory allocation for the stream

• There are two different classes of exceptions that are caught here: FileNotFoundException and IOException

• This is how you should write your I/O code!

Page 38: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 38

Running the Example Code

Page 39: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 39

Augmenting Our Code

• What the read documentation didn’t tell you is that the read() method returns the integer value 1 if there is no more file to read

• We’ll use this to modify our code to read (and print to standard output) the entire file

Page 40: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 40

NOTE! (see next slide)

Page 41: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 41

Notes From Last Slide• myChar is declared an int, since we’ll need

to check an integer value to see if we’re at the end of the file

• We use an infinite loop to keep iterating until we’ve read the entire file

• We call the read() method within the conditional for the if statement. Regardless of whether the condition is true, the call to read() occurs (this is a standard hack for this kind of code)

Page 42: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 42

More Notes…

• We use the print() method as opposed to the println() method – when a newline character is encountered, this will cause a new line in the output

• We perform the cast to char inside the System.out.print () statement

Page 43: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 43

There Are Easier Ways

• Use another version of the read() method to read characters into a char array

• Use the readline() method, which reads a whole line at a time into a String

Page 44: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 44

Page 45: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 45

More read() Documentation

Page 46: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 46

Often the Best Way…

Page 47: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 47

A problem

Page 48: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 48

The Problem

• The readLine() method returns the value null if it reaches the end of file. When this happens, the variable currentLine is assigned the value null, which causes a NullPointerException when we try to call its equals() method in the if statement. See the fix on either of the next two slides (the first is the recommended fix)

Page 49: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 49

A fix

Page 50: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 50

Another fix

Page 51: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 51

Reading Numeric Data Types

• Note that using any of the BufferedReader methods results in reading either a String or a char. To read a double or int or other numerical data type, you need to – Use methods like stringToInt()– Use methods like Double.parseDouble(String input)

• The method Integer.parseInt(String input) is the exact equivalent of stringToInt()

Page 52: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 52

Finally, Reading Input From the Keyboard

• The keyboard is an input device like any other, and we can read it with an input stream

• We use the read() method in System.in– This works like our other read() method, but reads

from the keyboard

• We use the above read() method to create a static readLine() method that we can call to read from the keyboard

Page 53: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 53

Page 54: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 54

This is needed in order to handle bothUnix and Windows style end of line conventions

Page 55: Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files

Fall 2002 CS 150: Intro. to Computing 55

A Tester For KeyboardIn

• Don’t forget that we need to use a try block with this