CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

33
CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05

Transcript of CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Page 1: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

CIS3931 - Intro to JAVA

Lecture Notes Set 6

2-June-05

Page 2: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

IO Streams

• IO = Input / Output

• The core JAVA language doesn’t have any IO methods– Must import java.io or other io package

• Stream = connection between program and data source

Page 3: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

IO Streams

• Input stream – handles data flowing into a program

• Output stream – handles data flowing out of a program

• IO Steams can connect two programs– Pipe : connects two executing programs

Page 4: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

IO DevicesObject Source? Destination?

Disk file Both

Running program Both

Monitor Destination

Keyboard Source

Internet connection

Both

Image scanner Source

Mouse Source

Page 5: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Processing Streams

• Processing stream – operates on the data supplied by another stream. Often acts as a buffer for the data coming from another stream.

• Buffer – block of main memory used as a work area.

Page 6: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Processing Stream - Example

BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));

Page 7: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Readers

• Reader – Abstract class for which all character-oriented input streams are derived.

Page 8: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Writers

• Writer is an abstract class from which all character-oriented streams are derived.

Page 9: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

InputStream

• Abstract class for which all byte-oriented input streams are derived.

Page 10: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Output Stream

• Abstract class from which all byte-oriented output streams are derived.

Page 11: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Writing Text Files

• IO streams are either character-oriented or byte-oriented. – Character-oriented IO is specialized for

handling character data. – Byte-oriented IO is general purpose IO that

involves all types of data.

• We will be discussing character-oriented output to a disk file and using a Writer stream.

Page 12: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

FileWriter

• Used for character output to a disk file

• FileWriter is a kind of OutputStreamWriter

• OutputSteamWriter is a kind of Writer

• Writer is a kind of Object

Page 13: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

FileWriter - Inheritance

http://java.sun.com/j2se/1.5.0/docs/api/index.html

Page 14: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Writing to a file

• Import java.io.*

• Create a FileWriter– FileWriter writer = new FileWriter(filename);

• Look in the API to see how to print out using the FileWriter

• Close the FileWriter

Page 15: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Writing to a file

• See FileWriting.java

Page 16: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

The importance of close()

• Computer terms often come from business terms …

• Closing a file means to gather everything that should go into it and file it away.

• If a file is not closed, the program might end before the operating system has finished writing to the data file.

• No close = possible loss of data• Once a file is closed, you can’t write to it.

Page 17: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

FileWriter Contructors

• See the API …

• FileWriter(String fileName, boolean append)

Page 18: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

FileWriter IOExceptions

• Useful when using the append option• Append expects filename to already exist• If the program can’t find the filename, it will

throw an exception• Exception should be handled so that the

program will not crash.• The constructor, the write() method, and

the close() method call all throw an exception.

Page 19: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Example

• See FileWriter2.java

Page 20: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Getting the filename from user input

• Create null contructor

• Create buffered reader

• Read filename into string

• Try to open filename

• Example : see FileWriter3.java

Page 21: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

BufferedWriter

• Allows for more efficient disk input/output

• Useful in programs that do extensive IO.

• Example :

BufferedWriter out = new BufferedWriter(new

FileWriter("stuff.txt"));

Page 22: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

PrintWriter

• Used to deal with end-of-line programs and other frustrations of file output.

• Uses println() method for outputting line of text with newline at the end.

• Often connected to BufferedWriter (which is connected to a FileWriter)

• PrintWriter’s methods do not throw exceptions

Page 23: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Example

• See FileWriter4.java

• Difference is that PrintWriter allows txt files to show up correctly in Windows programs such as Notepad

Page 24: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Reading

• Use BufferedReader

• Example : BufferedReader stdin = new BufferedReader(new

InputStreamReader( System.in ));

• The above example created a BufferedReader and connects it to the standard input (not a file).

Page 25: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

FileReader

• Used for input of character data from a disk file.

• Automatically translates the characters from the disk file format to the internal char format.

Page 26: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Example

• See FileReader.java

Page 27: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

close() with input files

• Not as important as with output files

• Helps to let the operating system manage resources more efficiently

• File must be closed for writing before it is opened for reading!

Page 28: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

File Input Techniques

• Counting loop – Increment a counter after each input line is read

• Sentinel-controlled loop – Read in lines until reaching a line that contains a special value.

• Result-controlled loop – Read in lines until a desired result has been achieved.

Page 29: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Counting Loops - Example

• Write a program to add up all the integers in a file except the first integer which says how many integers follow

4

23

53

64

91

Page 30: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Counting Loops - Example

• See AddUpAll.java

Page 31: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Sentinel Controlled Input Loop

• Special input value indicates there is no more data.

• Example : Average all the integers in a file. -1 = no more integers to average.

78

82

91

-1

Page 32: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

Sentinel Controlled Look - Example

• See AddUpAllSentinel.java

Page 33: CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05.

• Questions?