Title: Streams and File I/O in Java
1Streams and File I/O in Java
- Eric Allen
- Rice University
2Streams and Lazy Evaluation
- Java I/O is based on the notion of streams
- Streams are sequences of data (whose elements may
be computed on demand) - Streams originated from functional programming,
as an alternative to mutation
3A Very Simple Stream
class OneStream extends IntStream public int
next() return 1
4A Slightly More Complex Stream
- public class NatStream
- private int current 0
-
- public int next()
- return current
-
-
5Streams And Computation
- Streams can be composed and filtered to produce
more complex streams - Lets apply the Union, Composite, and Command
Patterns to construct composable streams of ints
6(No Transcript)
7abstract class IntStream public abstract int
next() class NatStream extends IntStream
private int current 0 public int next()
return current
8abstract class ComposableIntStream extends
IntStream private Operator op private
IntStream left private IntStream right
public ComposableIntStream(Operator _op,
IntStream _left,
IntStream _right) op
_op left _left right _right
public int next() return op.apply(left.next(),
right.next())
9abstract class Operator public abstract int
apply(int left, int right) // for
example, class Adder extends Operator public
int apply(int left, int right) return left
right // and class Multiplier extends
Operator public int apply(int left, int
right) return left right
10Now we can construct all sorts of nifty
composable IntStreams
class EvenStream extends ComposableIntStream
public EvenStream() super(new Adder(), new
NatStream(), new NatStream()) class
SquareStream extends ComposableIntStream
public SquareStream() super(new
Multiplier(), new NatStream(), new NatStream())
11Building the Natural Numbers
class ZeroStream extends IntStream public int
next() return 0 class AdderStream extends
ComposableIntStream public AdderStream(IntStre
am left, IntStream right) super(new
Adder(), left, right)
12Building the Natural Numbers
class AlternateNatStream extends IntStream
IntStream value new ZeroStream() public
int next() value new AdderStream(new
OneStream(), value) return value.next()
13In fact, streams can be used as a foundation for
all of number theory!
- Exercise (optional/not for credit) Extend
IntStreams to include a stream of prime numbers - Hint define the notion of filtered streams, as a
subtype of ComposableIntStreams
14Applications of Streams
- Streams are natural models of many real-world
systems - Stock prices
- Mouse/keyboard/monitor input
- Radio signals
- Human input to a program (DrJava interactions)
- Contents of a file
15Output Streams Model Systems That Take Input
- Just as we can read from sources, we can write to
destinations - Output streams take input as its computed
16I/O Streams in Java
- java.io.InputStream, java.io.OutputStream
- Readers, writers are adapters to streams, to make
certain sorts of I/O easier
17Reading from a File
- gt FileReader fReader new FileReader(fn)
- gt fReader.read()
- 97
- gt (char)fReader.read()
- b
18Writing to a File
- gt FileWriter fWriter new FileWriter(fn)
- gt fWriter.write()
19Testing Writers
- Readers/Writers can be composed using
PipedReaders and PipedWriters - PipedReader pReader new PipedReader()
- PipedWriter pWriter new PipedWriter(pReader)
20Testing Writers
- By always writing to a Writer field (as opposed
to hard-wired System.out, etc.), you can test
your classes more easily - Pass in PipedWriters in your test cases and check
whats sent to a corresponding PipedReader
21Stream Tokenization
- Often we want to view elements of a stream as
structures larger than the elements themselves
22Stream Tokenization
- Consider the syntactic components of a Java
program keywords, vars, etc. - class C extends D implements E
- These elements are more complex than just
characters
23Stream Tokenization
- In such cases, we can place a Façade stream over
the original s.t. the elements of the Façade are
sequences of the original elements
24Stream Tokenizer
- Java provides a built-in StreamTokenizer class
- Warning The design of this class is ugly
- Always put an Adapter class over it first (or
write your own!) - This class is very powerful, but its biased
toward parsing Java code
25Using StreamTokenizer
- gt Reader r new BufferedReader(new
InputStreamReader(si)) - gt StreamTokenizer tokenizer new
StreamTokenizer(r) - gt tokenizer.nextToken()
- 42