Streams and File I/O in Java - PowerPoint PPT Presentation

About This Presentation
Title:

Streams and File I/O in Java

Description:

Streams and File I/O in Java Eric Allen Rice University Streams and Lazy Evaluation Java I/O is based on the notion of streams Streams are sequences of data (whose ... – PowerPoint PPT presentation

Number of Views:131
Avg rating:3.0/5.0
Slides: 26
Provided by: EricA224
Category:
Tags: file | java | spring | streams

less

Transcript and Presenter's Notes

Title: Streams and File I/O in Java


1
Streams and File I/O in Java
  • Eric Allen
  • Rice University

2
Streams 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

3
A Very Simple Stream
class OneStream extends IntStream public int
next() return 1
4
A Slightly More Complex Stream
  • public class NatStream
  • private int current 0
  • public int next()
  • return current

5
Streams 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)
7
abstract class IntStream public abstract int
next() class NatStream extends IntStream
private int current 0 public int next()
return current
8
abstract 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())
9
abstract 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
10
Now 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())

11
Building 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)
12
Building the Natural Numbers
class AlternateNatStream extends IntStream
IntStream value new ZeroStream() public
int next() value new AdderStream(new
OneStream(), value) return value.next()

13
In 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

14
Applications 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

15
Output 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

16
I/O Streams in Java
  • java.io.InputStream, java.io.OutputStream
  • Readers, writers are adapters to streams, to make
    certain sorts of I/O easier

17
Reading from a File
  • gt FileReader fReader new FileReader(fn)
  • gt fReader.read()
  • 97
  • gt (char)fReader.read()
  • b

18
Writing to a File
  • gt FileWriter fWriter new FileWriter(fn)
  • gt fWriter.write()

19
Testing Writers
  • Readers/Writers can be composed using
    PipedReaders and PipedWriters
  • PipedReader pReader new PipedReader()
  • PipedWriter pWriter new PipedWriter(pReader)

20
Testing 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

21
Stream Tokenization
  • Often we want to view elements of a stream as
    structures larger than the elements themselves

22
Stream 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

23
Stream 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

24
Stream 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

25
Using StreamTokenizer
  • gt Reader r new BufferedReader(new
    InputStreamReader(si)) 
  • gt StreamTokenizer tokenizer new
    StreamTokenizer(r)
  • gt tokenizer.nextToken()
  • 42
Write a Comment
User Comments (0)
About PowerShow.com