22-io_inheritance - CSE 143 Lecture 22 I/O Streams;...

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

View Full Document Right Arrow Icon
CSE 143 Lecture 22 I/O Streams; Exceptions; Inheritance read 9.3, 6.4 slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Input and output streams stream : an abstraction of a source or target of data – 8-bit bytes flow to (output) and from (input) streams • can represent many data sources: – files on hard disk – another computer on network –web page – input device (keyboard, mouse, etc.) • represented by java.io classes – InputStream
Background image of page 2
3 Streams and inheritance • all input streams extend common superclass InputStream ; all output streams extend common superclass OutputStream – guarantees that all sources of data have the same methods – provides minimal ability to read/write one byte at a time
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Input streams • constructing an input stream: (various objects also have methods to get streams to read them) • methods common to all input streams: Description Method stops reading public void close () throws IOException reads/returns a byte (-1 if no bytes remain) public int read () throws IOException public SequenceInputStream(InputStream a, InputStream b) public ByteArrayInputStream(byte[] bytes) Constructor public FileInputStream(String name) throws IOException
Background image of page 4
5 Output streams • constructing an output stream: • methods common to all output streams: forces any writes in buffers to be written public void flush() throws IOException Description Method stops writing (also flushes) public void close () throws IOException writes a byte public void write (int b) throws IOException public PrintStream(File file) public PrintStream(String fileName) public ByteArrayOutputStream() Constructor public FileOutputStream(String name) throws IOException
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 int vs. char •The read and write methods work an int (byte) at a time. • For text files, each byte is just an ASCII text character. –an int can be cast to char as needed: FileInputStream in = new FileInputStream("myfile.txt"); int n = in.read(); // 81 char ch = (char) n; // 'Q' –a char can be passed where an int is wanted without casting: FileOutputStream out = new FileOutputStream("outfile.txt"); char ch = 'Q'; out.write(ch);
Background image of page 6
7 I/O and exceptions exception : An object representing an error. checked exception : One that must be handled for the program to compile. • Many I/O tasks throw exceptions.
Background image of page 7

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

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

This note was uploaded on 01/04/2011 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Page1 / 23

22-io_inheritance - CSE 143 Lecture 22 I/O Streams;...

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

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