100_lecture29

100_lecture29 - Introduction to Computation and Problem...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward Class 29: Class 29: Introduction to Streams Introduction to Streams November 22, 2005 November 22, 2005 2 Goals Just as Java has a modern approach to error handling inherited from C++, Java communicates with the outside world using another C++ inspired technique called streams. In this session, we will: Look at the classic stream code to read a file. Examine Java's architecture of stream classes Learn how to parse simple text input. On Tuesday, we will have an active learning session on streams. Please review these notes before then. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
3 JFileViewer JFileViewer is a small demonstration program that reads in a file and puts it up in a text area. It is composed of two classes: and main loop JTextViewer: ) GUI JFileViewer: main() the (rudimentary 4 JFileViewer, core try block (in English) try { open a stream from the file while there is more data read it close the file } catch append it to our GUI display any I/O errors 2
Background image of page 2
5 JFileViewer , core try block (in Java) try { in.close(); } { handleErr( e ); } 6 Traditional I/O schemes depending on the type of the source or destination, e.g., screen output files network sockets FileReader in = new FileReader( args[ 0 ] ); char [] buf = new char[ 512 ]; int nread; while( ( nread = in.read( buf ) ) >= 0 ) { view.append( new String( buf, 0, nread ) ); } catch ( IOException e ) The traditional approach uses different keyboard input interprocess pipes 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
7 Java I/O (pioneered in C++) Think of a stream as a data hose connecting the destination. e.g. RandomAccessFile Java’s preferred approach is to handle I/O using streams program to the outside world. It hides the details of the external data source or Java allows you to treat some I/O distinctively, 8 Streams are unidirectional An input stream controls data coming into the program from some source An output stream controls data leaving the program for some destination Input vs Output Streams If you want to read and write the same destination, you use 2 streams 4
Background image of page 4
9 Streams and I/O Channels arises from a platform-specific media File System Output Stream Program Input Stream File System Usually the other end of a stream leads to or service, for instance, a file system 10 Java Stream Classes Java provides InputStream OutputStream Reader Writer streams) BufferedReader a set of abstract stream classes that define the stream interfaces for different kinds of streams: : reads bytes : writes bytes : reads chars : writes chars a hierarchy of stream implementations: that are tailored for a particular data source or destination, e.g., FileReader reads chars from a file that add functionality to a preexisting stream (filter , e.g., 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 16

100_lecture29 - Introduction to Computation and Problem...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online