Presentation is loading. Please wait.

Presentation is loading. Please wait.

MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources.

Similar presentations


Presentation on theme: "MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources."— Presentation transcript:

1 MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources

2 Goals 1.Read bytes/characters from a file into a program 2.Create a new file 3.Write bytes/characters from a program to a file

3 Traditional I/O The traditional approach uses different schemes depending on the type of the source or destination, e.g., –keyboard input –screen output –files –interprocess pipes –network sockets

4 Java I/O  Java’s preferred approach is to handle I/O using streams (pioneered in C++)  Java provides –set of abstract stream classes that define the stream interfaces –hierarchy of stream implementations

5 Input vs Output Streams  We use streams to save data or retrieve data  A stream is an object that takes information from one source and sends it to another  Input streams - read data from a source  Output streams – write data to a source  If you want to read and write the same destination, you use 2 streams

6 Byte Streams vs. Character Streams  Byte Streams are used to read and write data in byte format (such as executable programs, word- processing documents, MP3 music files, etc)  Character Streams are used to read and write data in the form of characters – individual letters, numbers, punctuation, and the like (such as text files, word processing documents, web pages and the like).

7 Using streams to input/output data 1. Create a stream object associated with the data 2. Call methods of the stream to either put information in the stream or take information out of it 3. Close the stream by calling the object’s close() method

8 Streams and I/O Channels Usually the other end of a stream leads to or arises from a platform-specific media service, for instance, a file system File System Output Stream Program Input Stream File System

9 What Streams Share  Java Streams are FIFO queues –Streams deliver information in the order it was inserted into the underlying channel  Standard Java streams only provide sequential access without rewind, backup, or random access

10 Coupling Streams  Java streams may be combined by using one stream as a constructor argument to another  This is usually done to add functionality and/or convert the data  Stream pipelines are constructed –from the data source to the program or –from the data destination back to the program

11 Stream Pipeline – Reading I File Input Stream Input Stream Reader Buffered Reader Stream Tokenizer

12 Stream Pipeline – Reading II  A FileInputStream reads bytes from a file  An InputStreamReader converts a byte stream to characters  A BufferedReader buffers a character stream for efficiency  A StreamTokenizer parses a character stream into tokens

13 Stream Pipeline - Reading III FileInputStream f = new FileInputStream( path ); InputStreamReader i = new InputStreamReader( f ); BufferedReader b = new BufferedReader( i ); StreamTokenizer t = new StreamTokenizer( b );

14 StreamTokenizer (1)  Java supplies a special class called StreamTokenizer that accepts a Reader as a constructor argument.  It breaks the input character stream into tokens, sequences of 1 or more contiguous characters that "belong" together.  The user accesses these tokens by calling the nextToken() method, which always returns an int representing the type of the next token: –word, –number, –end of file (EOF), –end of line (EOL, optional), and –otherCharacter, returned as int value of the 16 bit character code

15 StreamTokenizer (2)  When a StreamTokenizer recognizes a word, the public member sval contains the String representing the word.  When a number token is recognized, public member nval contains its value.  StreamTokenizer also ignores whitespace (blanks and tabs) and C, C++, and Java style comments by default.  StreamTokenizer instance methods can change the definition of “word” or “number” and can turn on or off features like ignoring comments.

16 StringTokenizer  Similarly, a StringTokenizer breaks a string into tokens.  The tokenization method is much simpler than the one used by the StreamTokenizer class.  The following is one example of the use of the tokenizer. The code: StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { println(st.nextToken()); } prints the following output: this is a test

17 Reading primitive Java data types File Input Stream Data Input Stream  A FileInputStream reads bytes from a file  An DataInputStream reads primitive Java data types from a byte stream in a machine-independent way.

18 Reading character files Buffered Reader Stream Tokenizer  A FileReader reads a character stream from a file (equivalent to a InputStreamReader on a FileInputStream, assuming the default character encoding and the default byte-buffer size are appropriate)  A BufferedReader buffers a character stream for efficiency  A StreamTokenizer parses a character stream into tokens File Reader

19 Stream Pipeline – Writing I Buffered Writer Ouput Stream Writer File Output Stream

20 Stream Pipeline – Writing II  A BufferedWriter buffers a character stream for efficiency (buffering characters so as to provide for the efficient writing of single characters, arrays, and strings)  A OuputStreamWriter converts characters to a byte stream according to a specified character encoding. (The encoding that it uses may be specified by name, or the platform's default encoding may be accepted. )  A FileOutputStream writes bytes to a file

21 Write to a byte file  A DataOutputStream writes primitive Java data types to a byte stream.  A FileOutputStream writes bytes to a file. Data Output Stream File Output Stream

22 Write to a character file  A PrintWriter print formatted representations of objects to a text-output stream  A FileWriter writes to character files. Print Writer File Writer

23 A slightly complicated example  First File: Employee_May.dat Name, SSN, hourly rate, salary to date Paul Njoroge, 555-12-3456, 65, 20000 Evelyn Eastmond, 555-22-2222, 70, 30000 Peilei Fan, 555-33-4444, 60, 15000 Ethan Howe, 555-44-5555, 80, 40000 Naveen Goela, 555-66-8888, 75, 20000  Second file: Hours.dat, contains 5 integers, which are the number of hours each employee has worked for that month. The integers have the same sequence as that of the employee records. Content: 50 60 40 50 70

24 What we need to do: Our program 1. reads the number of hours worked from Hours.dat, 2. calculates the monthly salary for that employee, 3. updates her salary to date, 4. and print the new data to a file called Employee_June.dat

25 Five steps for the program 1. Import the Headers 2. Read the Data File (Hours.dat) 3. Read the Text File (Employee_May.dat) 4. Update the Data 5. Output the File

26 1. Import the Headers  import java.io.*;  import java.util.*;

27 2. Read the Data File (Hours.dat)  1. Create a File object representing Hours.dat  2. Connect the file object to an input stream (FileInputStream)  3. Attach a filter stream (DataInputStream) to the input stream  4. Read 5 integers from the data input stream (call readInt() method of DataInputStream)  5. Close the input stream File f = new File("Hours.dat"); FileInputStream fin= new FileInputStream(f); DataInputStream din = new DataInputStream(fin); int[] hours = new int[10]; for (int i=0; i<5; i++) hours[i] = din.readInt(); din.close();

28 3. Read the Text File (Employee_May.dat)  6. Connect Employee_May.dat to a FileReader  7. Attach a BufferedReader to the file reader  8. Read 5 strings from the buffered reader  9. Close the buffered reader FileReader fr = new FileReader("Employee_May.dat"); BufferedReader in = new BufferedReader(fr); String[] records = new String[5]; for (int j=0; j<5; j++) records[j] = in.readLine(); in.close();

29 4. Update the Data (steps)  The readLine() function return a string that contains all 4 fields of an employee record. We need to find the hourly rate and salary to date.  10. Assign the string to a StringTokenizer  11. Find the 3rd and 4th tokens in the string  12. Calculate the salary for this month and add it to the salary to date  In this process, we used a utility class called StringTokenizer which breaks the string into individual pieces (tokens) based on the delimiter. In our example,the delimiter is ", "

30 4. Update the Data (code) StringTokenizer st; String name, ssn; double hourlyRate, salary; for (int k=0; k<5; k++) { st = new StringTokenizer(records[k], ", "); name = st.nextToken(); ssn = st.nextToken(); hourlyRate = Double.parseDouble(st.nextToken()); salary = Double.parseDouble(st.nextToken()); salary += hourlyRate * hours[k]; records[k] = name + ", " + ssn + ", " + hourlyRate + ", " + salary }

31 5. Output the Data  13. Create a new FileWriter with the file name Employee_June.dat  14. Attach a PrintWriter to the file writer  15. Write the string array to the PrintWriter  16. Close the PrintWriter FileWriter fw = new FileWriter("Employee_June.dat"); PrintWriter out = new PrintWriter(fw); for (int i=0; i<5; i++) out.println(records[i]); out.close();

32 Pop Quiz  What package do you need to import in order to write/read a file?  What package do you need to import in order to use StringTokenizer?  To read a byte file, what pipes do you need to use, for instance, read a byte file “MyInt.dat” which contains integer data types?  To read a text file (“MyText.txt”), what pipes do you need to use?

33 Pop Quiz (2) 1. What do you need to use to write a text file? 2. What do you need to use to write a byte file? 3. Can you use a text editor such as word pad, note pad, or microsoft word to see the content of a byte file? What about a text file? 4. Do you know the name of the person who sits next to you? 5. What kind of Exception do you have to deal with when you read/write a file? How are you going to deal with it?


Download ppt "MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources."

Similar presentations


Ads by Google