J12:More on Files

Download Report

Transcript J12:More on Files

Putting Streams to use
1
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.
2
Stream Zoo



deal with bytes
DataInputStream and DataOutputStream deal
with basic java types.
Binary data I/O
InputStream

OutputStream
DataInputStreams



and
Read binary data from InputStream
Methods read, readByte, readChar,
readDouble...
DataOutputStreams


Write binary data to OutputStream
Methods write, writeChar, writeInt...
3
Stream Zoo

File processing

Import java.io


Definitions of stream classes FileInputStream and
FileOutputStream
Inherit from InputStream and OutputStream


abstract classes
and FileOutputStream give you
streams attached to disk files.
FileInputStream


FileInputStream fin = new FileInputStream (“file.dat”)
Only support at byte level
4
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.
5
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
6
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).
7
Files and Streams

Buffering

Improves I/O performance


Buffer stores data of many I/O operations


I/O is slow compared to processor
When full, sends data
Can be explicitly flushed (forced to send
data)
8
Comparisons


Other languages offer niceties such as
buffering and lookahead automatically in
stream libraries
Java’s ability to combine provides greater
flexibility
9
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.
10
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

11
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)
{ …
}
12
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.
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
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
15
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());
16
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
17