Presentation is loading. Please wait.

Presentation is loading. Please wait.

I/O Streams A stream is a sequence of bytes that flows from a source to a destination In a program, we read information from an input stream and write.

Similar presentations


Presentation on theme: "I/O Streams A stream is a sequence of bytes that flows from a source to a destination In a program, we read information from an input stream and write."— Presentation transcript:

1 I/O Streams A stream is a sequence of bytes that flows from a source to a destination In a program, we read information from an input stream and write information to an output stream A program can manage multiple streams at a time The java.io package contains many classes that allow us to define various streams with specific characteristics

2 I/O Stream Categories The classes in the I/O package divide input and output streams into other categories An I/O stream is either a character stream, which deals with text data byte stream, which deals with byte data An I/O stream is also either a data stream, which acts as either a source or destination processing stream, which alters or manages information in the stream

3 I/O class hierarchy class java.lang.Object class java.io.InputStream
class java.io.ByteArrayInputStream class java.io.FileInputStream class java.io.FilterInputStream class java.io.OutputStream class java.io.ByteArrayOutputStream class java.io.FileOutputStream class java.io.FilterOutputStream class java.io.Reader class java.io.BufferedReader class java.io.InputStreamReader class java.io.Writer class java.io.BufferedWriter class java.io.OutputStreamWriter

4 Sources of data streams
There are three standard I/O streams: standard input – defined by System.in standard output – defined by System.out standard error – defined by System.err We use System.out when we execute println statements System.in is declared to be a generic InputStream reference, and therefore usually must be mapped to a more useful stream with specific characteristics FileInputStream and FileReader are classes whose constructors open a file for reading

5 Processing streams Processing classes have constructors that take InputSteams as input and produce InputStreams with added functionality BufferedReader, and BufferedWriter allow you to write bigger chunks of text to a stream. Buffering is a way of combining multiple reads or writes into a single action. It is a good idea when working with text. Examples: readLine() in BufferedReader and newLine() in BufferedWriter

6 IOExceptions The following exception classes are defined in the java.io package: CharConversionException EOFException FileNotFoundException InterruptedIOException InvalidClassException InvalidObjectException NotActiveException NotSerializableException ObjectStreamException OptionalDataException StreamCorruptedException SyncFailedException UnsupportedEncodingException UTFDataFormatException WriteAbortedException

7 Reading from a file: Listing 8.7
StringTokenizer tokenizer; String line, name, file="inventory.dat"; try { FileReader fr = new FileReader (file); BufferedReader inFile = new BufferedReader (fr); line = inFile.readLine(); while (line != null) { tokenizer = new StringTokenizer (line); name = tokenizer.nextToken(); try { units = Integer.parseInt (tokenizer.nextToken()); } catch (NumberFormatException exception) { System.out.println ("Error in input. Line ignored:");

8 The Keyboard Class The Keyboard class was written by the authors of your textbook to facilitate reading data from standard input Now we can examine the processing of the Keyboard class in more detail The Keyboard class: declares a useful standard input stream handles exceptions that may be thrown parses input lines into separate values converts input stings into the expected type handles conversion problems Take a look at the code and ask questions next class


Download ppt "I/O Streams A stream is a sequence of bytes that flows from a source to a destination In a program, we read information from an input stream and write."

Similar presentations


Ads by Google