Unformatted text preview:

1Input / Output FrameworkDate Chapter11/6/2006 Chapter 10, start Chapter 1111/13/2006 Chapter 11, start Chapter 1211/20/2006 Chapter 1211/27/2006 Chapter 1312/4/2006 Final Exam12/11/2006 Project DueHome Home java.io FrameworkA set of classes used for:reading and writing from files reading from consoleHome Streams of Bytes• A Stream is a sequential sequence of bytes. It can be used as a source of Input or a destination of Output– We read information form an Input Stream – We write information into an Output StreamReaderWriter StreamHome Streams of Bytes• Standard I/O Streams in Java– System.in• Represent keyboard input, or disk– System.out• (represents a particular window in the OS– System.err• (represents a particular window in the OSReaderWriter Stream2Home Streams of Bytes• The Java Class Library contains many classes for defining I/O streams with various characteristics– Files– Memory– Strings– Objects– Characters– Raw Bytes– BufferingHome System.out• System is a class in java.lang package• out is a a static constant field, which is an object of classPrintStream.• PrintStream is a class in java.io package• Since out is static we can refer to it using the class nameSystem.out• PrintStream Class has 2 methods for printing, print andprintln that accept any argument type and print to the standard java console.System.out.print(“What’s Up?”);Home Input Streams: System.in• System.in: the standard input stream– By default, reads characters from the keyboard• Can use System.in many ways– Directly (low-level access)– Through layers of abstraction (high-level access)ProgramSystem.inHome System.in ObjectClass BufferReader (returns String)Class InputStreamReader (returns unicode characters)Object InputStream System.inReturns bytesBufferedReader inStream = new BufferedReader(new InputStreamReader(System.in));3Selected Input Classes in the java.io PackageInput stream to read raw bytes of data from filesFileInputStreamClass to read character filesFileReaderClass to read/recover objects from a file written using ObjectOutputStreamObjectInputStreamClass providing more efficient reading of character files (Strings)BufferedReaderClass to read input data streams of charactersInputStreamReaderAbstract superclass representing a stream of raw bytesInputStreamAbstract superclass for input classesReaderDescriptionClassHome Hierarchy for Input ClassesClass to read character filesClass to read input data streamsInput stream to read raw bytes of data from filesHome Selected java.io Output Classes Class to write objects to a file ObjectOutputStream Output stream for writing raw bytes of data to files FileOutputStreamSupports printing various data types convenientlyPrintStreamPrints basic data types, Strings, and objectsPrintWriterMore efficient writing to character filesBufferedWriterClass for writing to character filesFileWriterAbstract superclass representing an output stream of raw bytesOutputStreamClass to write output data streamsOutputStreamWriterAbstract superclass for output classesWriterDescriptionClassHome Hierarchy for Output ClassesHome4Reading from the Java Console• System.in is the default standard input device, which is tied to the Java Console. • We have read from the console by associating a Scanner object with the standard input device:Scanner scan = new Scanner( System.in );• We can also read from the console using these subclasses of Reader:– InputStreamReader– BufferedReader, uses buffering (read-ahead) for efficient readingOpening an InputStream• When we construct an input stream or output stream object, the JVM associates the file name, standard input stream, or standard output stream with our object. This is opening the file. • When we are finished with a file, we optionally call the close method to release the resources associated with the file. • In contrast, the standard input stream (System.in), the standard output stream (System.out), and the standard error stream (System.err) are open when the program begins. They are intended to stay open and should not be closed.Home Software Engineering Tip Calling the close method is optional. When the program finishes executing, all the resources of any unclosed files are released. It is good practice to call the close method, especially if you will be opening a number of files (or opening the same file multiple times.) Do not close the standard input, output, or error devices, however. They are intended to remain open. Home Console Input Class ConstructorsBufferedReader( Reader r )constructs a BufferedReader object from a Reader object – here the Reader object will be an InputStreamReader object.BufferedReaderInputStreamReader( InputStream is ) constructs an InputStreamReader object from an InputStream object. For console input, the InputStream object is System.in.InputStreamReaderConstructorClassHome5Methods of the BufferedReaderClass• Because an IOException is a checked exception, we must call these methods within a try block.close( )releases resources associated with an open input stream. Throws an IOException.void readLine( )reads a line of text from the currentInputStream object, and returns the text as a String. Throws an IOException.StringMethod name and argument listReturn valueHome Home Console Input Exampleimport java.io.InputStreamReader; import java.io.BufferedReader;import java.io.IOException;public class ConsoleInput {public static void main( String [] args ) {String stringRead = "";try {InputStreamReaderisr = new InputStreamReader( System.in );BufferedReaderbr = new BufferedReader( isr );System.out.println( "Please enter a phrase or sentence > " );stringRead = br.readLine( );}catch( IOException ioe ){System.out.println( ioe.getMessage( ) );}System.out.println( "The string read was " + stringRead );}}Alternative Coding• This code:InputStreamReader isr = new InputStreamReader( System.in );BufferedReader br = new BufferedReader( isr );can also be coded as one statement using an anonymous object:BufferedReader br = new BufferedReader(new InputStreamReader( System.in ) );because the object reference isr is used only once.Home Hiding the Complexity• We can hide the complexity by encapsulating tryand catch blocks into a UserInput class, which is similar in concept to the Scanner class. • We write our class so that the client program can retrieve user input with just one line of code. • The UserInput class also validates that the


View Full Document

IIT CS 201 - Input / Output Framework

Download Input / Output Framework
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Input / Output Framework and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Input / Output Framework 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?