Introduction to Computer Science I
Download
Report
Transcript Introduction to Computer Science I
Technische Universität Darmstadt
Telecooperation/RBG
Introduction to Computer Science I
Topic 20: Streams and Input/Output
Prof. Dr. Max Mühlhäuser
Dr. Guido Rößling
Copyrighted material; for TUD student use only
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Outline
• Introduction to Input/Output Streams and Java
Input/Output (I/O)
• Overview of Processing Streams
• Wrapping Streams and the Decorator Pattern
• User Defined Streams, StreamTokenizer and
Random Access
Introduction to Computer Science I: T20
2
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Input / Output: Motivation
• So far:
– Program input data is either coded in the program’s source code,
or passed as parameters from the console
• define in Scheme
• Variable declaration and initialization in Java
• Actual arguments passed to functions in Scheme
• args parameters to main
– Program output is shown on the display
• Problems:
– Need to change the program in order to run it with different data
– “Closed world”:
• No interactivity with the outside world
• No influence on the run is possible after the program starts
– Results cannot be stored
• This lecture discusses other ways of input/output
Introduction to Computer Science I: T20
3
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Input / Output (I/O) - Streams
• Other sources / sinks for program data / results
– Keyboard entry (“standard I/O”),
– Files on local machine ("file I/O")
– Files/processes on the network ("network I/O")
– Main memory ("memory I/O")
• To deal with all these sources/destinations uniformly, the
concept of a data stream is introduced in Java
– Input and output streams (I/O streams for short)
• Streams abstract from I/O “devices”
– Hide details of the implementation and particularities of operating
various I/O devices
• Keyboard, Files, Programs, Network, Memory, …
– Provide unified interfaces for read/write data access
– Java program “talks to“ Java I/O stream objects
Introduction to Computer Science I: T20
4
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
I/O-Streams
• In order to read data, an input stream is attached to a data
source and the data is read element by element
• In order to write data, an output stream is attached to a data sink
and the data is written element by element
Introduction to Computer Science I: T20
5
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Streams and delayed Lists
• The concept of streams is more general than just I/O
• We saw streams in Scheme
– Delayed lists
– We could create „virtual“ lists
– We could link transparently data with first and rest
• These ideas fit well with I/O
–
–
–
–
Input: on access to the next list item, read the input value
Output: on extension of the list, write the output value
Input stream delayed list without cons
Output stream delayed list without first/rest
Introduction to Computer Science I: T20
6
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Predefined streams in java.io
• Two orthogonal classifications:
• According to the type of data
• According to the structure of the stream
Introduction to Computer Science I: T20
7
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Classification of streams
• According to the type of the data
– character streams read / write char (16-bit Unicode
character set)
– byte streams read / write byte (8 bit):
• used to deal with binary data, e.g., images, sound, etc.
• According to stream structure
– data streams: data is read/written directly from/to a
concrete source/sink device
– processing streams: data is read from/written to another
stream
• Data is filtered, buffered, manipulated, etc. after reading,
respectively before writing
Introduction to Computer Science I: T20
8
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Hierarchy of Stream Classes in java.io
• Character streams:
– java.io.Reader/java.io.Writer provide the
interface and a partial implementation for character
input/output streams
– Subclasses of Reader/Writer add/refine the
implementation in Reader/Writer
• Byte streams:
– java.io.InputStream/java.io.OutputStream
provide the interface and a partial implementation for
reading/writing bytes
– All other byte streams are subclasses of
InputStream/OutputStream
Introduction to Computer Science I: T20
9
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
Character Stream Classes (Examples)
©
Reader
shadows indicate
processing streams
StringReader
InputStreamReader
FileReader
FilterReader
PushbackReader
BufferedReader
LineNumberReader
Writer
StringWriter
OutputStreamWriter
FileWriter
FilterWriter
BufferedWriter
PrintWriter
Introduction to Computer Science I: T20
10
Byte Stream Classes (Examples)
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
FileInputStream
shadows indicate
processing streams
StringBufferInputStream
PipedInputStream
InputStream
ByteArrayInputStream
BufferedInputStream
ObjectInputStream
LineNumberInputStream
FilterInputStream
PushbackInputStream
SequenceInputStream
DataInputStream
FileOutputStream
PipedOutputStream
OutputStream
ByteArrayOutputStream
BufferedOutputStream
FilterOutputStream
PrintStream
ObjectOutputStream
DataOutputStream
Introduction to Computer Science I: T20
11
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Data Streams: Overview
Type of Device Character Streams
Main Memory
Pipe
Byte Streams
CharArrayReader,
CharArrayWriter
ByteArrayInputStream,
ByteArrayOutputStream
StringReader,
StringWriter
StringBufferInputStream
PipedReader,
PipedWriter
PipedInputStream,
PipedOutputStream
FileReader,
FileInputStream,
FileWriter
FileOutputStream
• CharArrayReader/CharArrayWriter
ByteArrayInputStream/ByteArrayOutputStream
– read from or write to an existing array in main memory
• StringReader / StringWriter, StringBufferInputStream
– read from or write to Strings in main memory
• FileReader/FileWriter
FileInputStream/FileOutputStream
– read from or write to a file on disc
File
Introduction to Computer Science I: T20
12
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Common patterns of I/O-streams
• Working with streams follows a pattern, independent of the
data type, source, or sink
• Reading
• Open stream: implicit by creation
• Read data as long as data is needed and available
• Close stream
• After reading/writing, you should close() the stream!
• Writing
• Open stream
• Write data as long as required
• Close stream
Introduction to Computer Science I: T20
13
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Common interfaces of I/O-streams
• Use I/O Streams only via common Interface
• Information hiding via subtype polymorphism
InputStream
public int read()
public int read(byte[] bbuf)
public int read(byte[] bbuf, int offset, int len)
Reader
public int read()
public int read(char[] cbuf)
public int read(char[] cbuf, int offset, int len)
OutputStream
public int write(int b)
public int write(byte[] bbuf)
public int write(byte[] bbuf, int offset, int len)
Writer
public int write(int c)
public int write(char[] cbuf)
public int write(char[] cbuf, int offset, int len)
Introduction to Computer Science I: T20
14
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
The java.io.Reader class
public
public
public
public
...
int read(char[] c) throws IOException
long skip(long n) throws IOException
void reset() throws IOException
int close() throws IOException
• public abstract int read(char[] c) throws
IOException
– reads the next Unicode characters into an array (c)
– Returns the number of characters read, or -1 if the end of the
stream has been reached ("EOF“, End of File)
– Any other problem causes an IOException,
• e.g., stream already closed, network connection lost, ...
For more details, see the API specification
http://java.sun.com/javase/6/docs/api/
Introduction to Computer Science I: T20
15
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Java Documentation
Introduction to Computer Science I: T20
16
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Java Documentation
Introduction to Computer Science I: T20
17
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
File Streams
• File streams represent I/O streams from and to files in the
file system:
– FileReader for reading and FileWriter for writing
character-wise from/to a file
– Similarly FileInputStream, FileOutputStream for
byte-wise reading/writing
• File streams are created by providing the source /
destination file by means of:
–
A file name (String)
–
A file object (java.io.File)
–
A file descriptor (java.io.FileDescriptor)
Introduction to Computer Science I: T20
18
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Class java.io.FileReader
Example: print file contents to screen (as int) and write 'a' to 'z' to a file
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ReadWriteFile {
// constructor etc.
public void readFrom(String fileName) throws IOException {
FileReader in = new FileReader(fileName);
int b;
while ((b = in.read()) != -1) System.out.print(b);
in.close();
}
}
public void writeAToZ(String filename) throws IOException {
FileWriter out = new FileWriter(filename);
for (char c = 'a'; c <= 'z'; c++) out.write(c);
out.close();
}
// main() etc.
Introduction to Computer Science I: T20
19
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Outline
• Introduction to Input/Output Streams and Java
Input/Output (I/O)
• Overview of Processing Streams
• Wrapping Streams and the Decorator Pattern
• User Defined Streams, Stream Tokenizer and
Random Access
Introduction to Computer Science I: T20
20
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Processing Streams
• A processing stream contains another (data or processing) stream
– The latter is used as a data source or data sink, respectively
• Data might be transformed or functionality added
–
–
–
–
–
data buffering
counting lines
converting bytes and chars
compression
…
processing stream
device data stream
data drain
Introduction to Computer Science I: T20
data source
21
Processing Streams: Overview
process
character streams
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
byte streams
filtering
FilterReader,
FilterWriter
FilterInputStream,
FilterOutputStream
buffering
BufferedReader,
BufferedWriter
BufferedInputStream,
BufferedInputStream
byte / char
conversion
InputStreamReader,
OutputStreamWriter
counting
LineNumberReader
datatype
handling
©
byte char
char byte
LineNumberInputStream
DataInputStream,
DataOutputStream
undoing
PushbackReader
PushbackInputStream
printing
PrintWriter
PrintStream
Introduction to Computer Science I: T20
22
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Roots of processing streams
• FilterReader/-Writer and
FilterInputStream/FilterOutputStream are general
super-classes for every processing stream
• They encapsulate an internal stream in
• Default implementations of an operation op:
pass on op() to the underlying stream
• concrete filter (sub-)classes refine this functionality
Introduction to Computer Science I: T20
23
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Java Documentation
Introduction to Computer Science I: T20
24
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Buffered Streams
• A buffered stream temporarily stores data from
another stream in an internal buffer
• Reading from a buffered stream fills the buffer
when it is empty
– Further "read"-operations will access the buffer without
reading from the attached stream
– When the buffer is empty, additional data will be read
from the underlying stream
• Writing to a buffered stream fills the buffer
before the attached data sink is written to
– The buffer is also emptied if the stream is flushed
explicitly (flush()) or closed (close())
Introduction to Computer Science I: T20
25
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Buffered Streams
• BufferedReader and BufferedWriter
– Subclasses of Reader/Writer, respectively
• BufferedInputStream and
BufferedOutputStream
– Subclasses of FilterInputStream /
FilterOutputStream
Introduction to Computer Science I: T20
26
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Buffered Streams
Data Sink
BufferedWriter
Program
write
write
b
bbuf
write bbuf
write bbuf
flush
Introduction to Computer Science I: T20
27
27
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Buffered Streams: Performance
Reading a file of 2.5MB (x=buffer size, y=time [ms])
Buffer sizeTime[ms]
Introduction to Computer Science I: T20
1
6433
2
3453
4
1935
8
988
16
471
32
365
64
363
128
87
256
327
512
65
1024
162
2048
214
4096
260
8192
61
16384
166
32768
159
28
Data Streams
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
• A data stream lets an application read/write primitive Java data types
from/to an underlying I/O stream in a machine-independent way.
• An application uses a DataOutputStream to write data that can
later be read by a DataInputStream.
FilterInputStream
FilterOutputStream
<<interface>>
DataInput
<<interface>>
DataOutput
DataOutputStream
DataInputStream
readBoolean(): boolean
readByte(): byte
readShort() : short
readChar() : char
readInt() : int
readFloat() : float
writeBoolean(boolean) : void
writeByte(byte) : void
writeShort(short) : void
writeChar(char) : void
writeInt(int) : void
writeFloat(float) : void
Introduction to Computer Science I: T20
29
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Data Streams: Example
import java.io.FileOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DataStreamExample {
String fileName = // some name;
// some methods
public void writeData() throws IOException {
FileOutputStream fos = new FileOutputStream(filename);
DataOutputStream out = new DataOutputStream(fos);
out.writeInt(9);
out.writeDouble(Math.PI);
out.writeBoolean(true);
out.close();
}
// other methods
public void readData() throws IOException {
FileInputStream fis = new FileInputStream(fileName);
DataInputStream in = new DataInputStream(fis);
int i = in.readInt();
double d = in.readDouble();
boolean b = in.readBoolean();
in.close();
System.out.println("Read "+ i + ", " + d + ", and " + b+ ".");
}
}
Introduction to Computer Science I: T20
30
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Standard-I/O
• Package java.lang contains the class System with the
following class variables:
static in
standard input
(keyboard)
static out
standard output
(screen)
static err
standard error messages
(screen)
• System.in yields an object of type InputStream
– enables to read() characters (bytes) from the keyboard
• System.out, System.err are of type PrintStream
– offer print(...) and println(...) to output data to
the screen
Introduction to Computer Science I: T20
31
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
PrintStream
• Adds the ability to conveniently
print user-readable representations
of various data values
FilterOutputStream
• Never throws an IOException
– Exceptional situations set an
internal flag that can be tested
via the checkError method
• All characters printed by a
PrintStream are converted into
bytes using the platform's default
character encoding
– PrintWriter should be used
in situations that require
writing characters rather than
bytes.
PrintStream
print(boolean) : void
print(double) : void
print(char) : void
print(double) : void
print(float) : void
...
println(Object): void
println(String): void
...
Introduction to Computer Science I: T20
32
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Outline
• Introduction to Input/Output Streams and Java
Input/Output (I/O)
• Overview of Processing Streams
• Wrapping Streams and the Decorator Pattern
• User Defined Streams, StreamTokenizer and
Random Access
Introduction to Computer Science I: T20
33
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
The Architecture of Java Streams
©
• Streams can be wrapped around each other
–
Abstraction layers, where underlying “primitive” streams are used by the
enclosing (“higher”, more comfortable) streams
BufferedOutputStream
FileOutputStream
ZipOutputStream
Data sink,
e.g. file
// create a buffered compressed output stream to a file
OutputStream out = new FileOutputStream(filename);
out = new BufferedOutputStream(out);
out = new ZipOutputStream(out);
// ... more features can be dynamically added
// the stream features are not visible for clients
Introduction to Computer Science I: T20
34
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
The Architecture of Java Streams
• The technique that lets us combine streams
at runtime is of more general interest.
– A general way to extend objects with
new features dynamically
• In software technology, such kind of
techniques are documented as design
patterns.
– Reusable, documented design ideas
– More in the lecture „SE Design“
• The technique underlying the architecture of
Java I/O streams is known as the Decorator
Pattern.
Introduction to Computer Science I: T20
35
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Outline
• Introduction to Input/Output Streams and Java
Input/Output (I/O)
• Overview of Processing Streams
• Wrapping Streams and the Decorator Pattern
• User Defined Streams, Stream Tokenizer and
Random Access
Introduction to Computer Science I: T20
36
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
User-Defined Processing Streams
• Sometimes it is useful to define streams that
process (e.g., filtering, statistics, production of
information chunks) data from other streams
• Best implemented by inheriting from
FilterReader/FilterInputStream or
FilterWriter/FilterOutputStream,
respectively
• In the following example, a process stream filters
out all lines which do not contain a certain
substring Unix "grep" command
Introduction to Computer Science I: T20
37
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
User-Defined Processing Streams
import java.io.BufferedReader;
import java.io.FilterReader;
import java.io.IOException;
class GrepReader extends FilterReader {
String substring;
BufferedReader in;
GrepReader(BufferedReader reader, String pattern) {
super(reader);
in = reader;
substring = pattern;
}
// return the next line containing the search pattern
String readLine() throws IOException {
String line;
while (((line = in.readLine()) != null) &&
(line.indexOf(substring) == -1)) ;
return line;
}
}
Introduction to Computer Science I: T20
38
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
User-Defined Processing Streams
import java.io.*;
public class Grep {
public static void main(String[] args) {
if ((args.length == 0) || (args.length > 2)) {
System.out.println("Usage: java Grep <substring> [<filename>]");
System.exit(0);
}
try {
Reader d;
if (args.length == 2)
d = new FileReader(args[1]);
else
d = new InputStreamReader(System.in);
GrepReader g = new GrepReader(new BufferedReader(d), args[0]);
String line;
while ((line = g.readLine()) != null) System.out.println(line);
g.close();
}
catch (IOException e) {
System.err.println(e);
}
}
}
stream being
searched
Introduction to Computer Science I: T20
pattern
to look for
39
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Random Access
• Until now, we have only considered sequential
streams that could only be read or written
sequentially
• Sequential streams are a good fit for sequential
storage media, e.g. magnetic tapes
• Random Access Files allow for non-sequential
access (random access) to the contents of a file
Introduction to Computer Science I: T20
40
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Motivating Example for Random Access
• Let us regard the file format of a ZIP archive:
– ZIP archives contain files and are usually compressed to
save disk space
– Apart from the files, there is an additional entry at the
end of the file, the so-called dir-entry (for directory)
– The dir-entry is used to keep track of which files are
contained and where – within the archive – they begin
• How to extract one file?
Introduction to Computer Science I: T20
41
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Motivating Example for Random Access
• Extracting a file using a sequential stream:
– Search the whole archive until the file is found
– Extract the file
• On average: half of the entries are searched…
• Extracting a file using a random access stream:
– Jump to the directory & read the entry of the file
– Jump to the recorded position of the file
– Extract the file
• Only two entities (directory & file) need to be read
• This approach is far more efficient
Introduction to Computer Science I: T20
42
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Random Access in Java
• Realized by the class RandomAccessFile
• Can be used for reading and writing—implements the
interfaces DataInput and DataOutput
• Similar to FileInputStream and FileOutputStream,
you open a RandomAccessFile on a file and pass a file
name or a File object as a parameter
• Additionally, you have to pass a parameter to specify
whether the file is opened for read-only or also for
writing
– You have to be able to read a file to be able to write to it
Introduction to Computer Science I: T20
43
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
The Class RandomAccessFile
• Creation a RandomAccessFile for reading file
“random.txt”:
– new RandomAccessFile("random.txt", "r");
• Creating a RandomAccessFile for reading and
writing on “random.txt”:
– new RandomAccessFile("random.txt", "rw");
• Afterwards, read and write operations may be used
to read and write data from/to the file
Introduction to Computer Science I: T20
44
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
The Class RandomAccessFile
• RandomAccessFile supports a file pointer: points to the
current position in a file
• Upon creation, the pointer is 0, i.e., beginning of the file
• Calls to read and write operations automatically move the file
pointer by the number of read, respectively written bytes
• RandomAccessFile also supports positioning operations
• int skipBytes(int n) throws IOException
moves read/write position by n bytes (relative positioning)
• native void seek(long pos) throws IOException
positions read/write position right before pos (absolute positioning)
• native long getFilePointer() throws IOException
returns the current read/write position
Introduction to Computer Science I: T20
45
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Stream Tokenizer
• java.io.StreamTokenizer supports the
generation of tokens out of character sequences
• A token is obtained by calling nextToken()
– Ignores Whitespace
• User can define what is to be regarded as whitespace
– The type of the token read in is stored in the attribute
ttype
• The token types are explained on the next slide
Introduction to Computer Science I: T20
46
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
StreamTokenizer: Token Types
• Basic token types:
– StreamTokenizer.TT_NUMBER – number scanned; the number
is stored in nval as a double
– StreamTokenizer.TT_WORD – a composed word is recognized
which is stored in sval
– StreamTokenizer.TT_EOL – end of line, if the tokenizer is
configured to recognize EOL as a token
(eolIsSignificant(true))
– StreamTokenizer.TT_EOF – end of file
– Any other value is an encoding of the character read in
Introduction to Computer Science I: T20
47
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Customizing StreamTokenizer
• public void wordChars(int low, int hi)
– Defines what is to be regarded as a word
– All characters in the interval [low, hi] are "word parts“
• public void whitespaceChars(int low, int
hi)
– Defines whitespace
– All characters in the interval [low, hi] are "space" characters
• public void eolIsSignificant(boolean flag)
– Defines whether the end of line character is of interest
• public void quoteChar(char c)
– Defines quote character
• See the API documentation for more features…
Introduction to Computer Science I: T20
48
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
StreamTokenizer: Example
• Read a list of tea data with the
following format:
– Each line contains data for one
type of tea, i.e., the end of line
token is of relevance
– Data items per tea type
(separated by spaces)
• Tea name in quotes
• Number of seconds required
• Number of recommended tea
spoons per liter
"Ali Baba's 40 scents" 180 5
"Asatsuyu" 90 7
"Generic Black Tea" 180 5
"Caramel" 120 6
"Ceylon Pekoe" 120 6
"China Jasmin" 120 6
"Chinese Love Dream" 150 5
"One For All" 540 1
"Cherry Cream" 120 6
• What would be a complex program
without further support, is easily
written with the help of
StreamTokenizer
Introduction to Computer Science I: T20
49
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
StreamTokenizer Example
void parseTeas(InputStream is) throws IOException {
StreamTokenizer stok = new StreamTokenizer(is);
stok.eolIsSignificant(true); // EOL is important!
stok.quoteChar('\"'); // quote char
int token = 0;
String teaName = null;
int nrSecs, nrSpoons;
while ((token = stok.nextToken()) != StreamTokenizer.TT_EOF) {
teaName = stok.sval;
// 1. token (name)
token = stok.nextToken();
nrSecs = (int)stok.nval;
// 2. (secs)
token = stok.nextToken();
nrSpoons = (int)stok.nval; // 3. (spoons)
token = stok.nextToken(); // consume EOL
System.out.println(teaName + "=>" + nrSpoons +
" spoons, seconds: " + nrSecs);
}
}
Normally, you should check the type actually read in before assigning it!
Introduction to Computer Science I: T20
50
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
Scanner
• Since Java 1.5, the Scanner class can be used for parsing
• A Scanner can be opened on a Stream, file, …
– Typical use: Scanner sc = new Scanner(System.in);
• The class offers many helpful methods, such as…:
– String next(); // returns the next token
– x nextX(); // for x = byte, double, float, int, long, short, Line
• Throws InputMismatchException if the type is not found
– boolean hasNextX(); // same x as above
• Checks if the next element would match type X
– You can redefine the delimiter, even to words
– Additionally, regular expressions can be used for matching
• Check the API documentation for more information
Introduction to Computer Science I: T20
51
Dr. G. Rößling
Prof. Dr. M. Mühlhäuser
RBG / Telekooperation
©
What else?
• Encrypted files, compressed files, files sent over
internet connections, ...
• Exceptions! All I/O involves exceptions!
– See part T16 for information on handling exceptions
try {
statements involving I/O
}
catch (IOException e) {
e.printStackTrace();
}
Introduction to Computer Science I: T20
52