Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.

Similar presentations


Presentation on theme: "1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java."— Presentation transcript:

1 1 Putting Streams to use

2 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java goes overboard, gives you separate classes for selecting buffering, lookahead, random access, text formatting, zip format, or binary data. 4 abstract classes at base: InputStream, OutputStream, Reader and Writer.

3 3 Stream Zoo InputStream and OutputStream deal with bytes DataInputStream and DataOutputStream deal with basic java types. Binary data I/O DataInputStreams Read binary data from InputStream Methods read, readByte, readChar, readDouble... DataOutputStreams Write binary data to OutputStream Methods write, writeChar, writeInt...

4 4 Stream Zoo File processing Import java.io Definitions of stream classes FileInputStream and FileOutputStream Inherit from InputStream and OutputStream abstract classes FileInputStream and FileOutputStream give you streams attached to disk files. FileInputStream fin = new FileInputStream (“file.dat”) Only support at byte level

5 5 Problem FileInputStream has no methods to read numeric types DataInputStream has no methods to get data from a file Java has creative mechanism to combine two into filtered streams by feeding existing stream to the constructor of another stream.

6 6 Example FileInputStream fin = new FileInputStream (“file.dat”); DataInputStream din = new DataInputStream (fin); Double s = din.readDouble ( ); The newly created filtered stream still accesses data from the file attached to the file input stream, but it now has more capable interface. NOT BUFFERED

7 7 Example 2 Not buffered: every call to read contacts OS to ask it to dole out another byte. IF you want buffering, data input for a file, you combine 3 types. DataInputStream din = new DataInputStream (new BufferedInputStream (new FileInputStream (“file.dat”))); Note that DataInputStream is last because we want to use DataInputStream methods (and we want them to use buffered read method).

8 8 Files and Streams Buffering Improves I/O performance I/O is slow compared to processor Buffer stores data of many I/O operations When full, sends data Can be explicitly flushed (forced to send data)

9 9 Comparisons Other languages offer niceties such as buffering and lookahead automatically in stream libraries Java’s ability to combine provides greater flexibility

10 10 Text Streams Set of stream filters that bridges gap between Unicode-encoded text and character encoding used by local OS. Use classes that descend from Reader and Writer (similar methods to InputStream and OutputStream) FileReader and FileWriter attach a reader or writer to a file.

11 11 Text Streams For text output, use PrintWriter. Can print strings and numbers in text format Must be combined with destination writer PrintWriter out = new PrintWriter (new FileWriter (“file.out”)); String name = “Harry Hacker”; double salary = 75000.00; out.print (name); out.print (‘ ‘); out.println (salary); //don’t throw exceptions

12 12 Text Streams No analog to read data in text format. Only possibility for processing text input is BufferedReader BufferedReader in = new BufferedReader (new FileReader (“rocks.dat”)); readLine method reads a line of text. Returns null when no more input available String line; while ((line = in.readLine () ) != null) { … }

13 13 Text Streams To read numbers from text input, you need to read a string first, and then convert it. String s = in.readLine ( ); Double x = Double.parseDouble (s); If more than one number on a line, you must break up the input string Use StringTokenizer utility class.

14 14 Text Streams To read numbers from text input, you need to read a string first, and then convert it. String s = in.readLine ( ); Double x = Double.parseDouble (s); If more than one number on a line, you must break up the input string Use StringTokenizer utility class.

15 15 String Tokenizers and Delimited Text Must specify delimiters to separate out tokens StringTokenizer t = new StringTokenizer (line, “|”); StringTokenizer t = new StringTokenizer (line, “ \t\n\r”); //white space StringTokenizer t = new StringTokenizer (line); //default is white space

16 16 Reading Delimited Input bufferedReader in = new BufferedReader (new FileReader (“file.dat”)); String s = in.readLine ( ); StringTokenizer t = new StringTokenizer (s); String name = t.nextToken (); double Salary = Double.parseDouble (t.nextToken()); int year = Integer.parseInt (t.nextToken());

17 17 Putting it Together Let’s see a lot of concepts together!!! - File input - Array of objects - Exception handling - Array Lists - EOF detection - Example DataTestFile.java


Download ppt "1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java."

Similar presentations


Ads by Google