Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS3931 - Intro to JAVA Lecture Notes Set 6 2-June-05."— Presentation transcript:

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

2 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

3 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

4 IO Devices ObjectSource? Destination? Disk fileBoth Running programBoth MonitorDestination KeyboardSource Internet connection Both Image scannerSource MouseSource

5 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.

6 Processing Stream - Example BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));

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

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

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

10 Output Stream Abstract class from which all byte-oriented output streams are derived.

11 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.

12 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

13 FileWriter - Inheritance http://java.sun.com/j2se/1.5.0/docs/api/index.html

14 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

15 Writing to a file See FileWriting.java

16 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.

17 FileWriter Contructors See the API … FileWriter(String fileName, boolean append)

18 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.

19 Example See FileWriter2.java

20 Getting the filename from user input Create null contructor Create buffered reader Read filename into string Try to open filename Example : see FileWriter3.java

21 BufferedWriter Allows for more efficient disk input/output Useful in programs that do extensive IO. Example : BufferedWriter out = new BufferedWriter(new FileWriter("stuff.txt"));

22 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

23 Example See FileWriter4.java Difference is that PrintWriter allows txt files to show up correctly in Windows programs such as Notepad

24 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).

25 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.

26 Example See FileReader.java

27 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!

28 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.

29 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

30 Counting Loops - Example See AddUpAll.java

31 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

32 Sentinel Controlled Look - Example See AddUpAllSentinel.java

33 Questions?


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

Similar presentations


Ads by Google