22-io_inheritance - CSE143 Lecture22...

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 Document Right 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 OutputStream
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 Document Right 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: Method Description public int read () throws IOException reads/returns a byte (-1 if no bytes remain) public void close () throws IOException stops reading Constructor public FileInputStream(String name) throws IOException public ByteArrayInputStream(byte[] bytes) public SequenceInputStream(InputStream a, InputStream b)
Background image of page 4
5 Output streams constructing an output stream: methods common to all output streams: Method Description public void write (int b) throws IOException writes a byte public void close () throws IOException stops writing (also flushes) public void flush() throws IOException forces any writes in  buffers to be written Constructor public FileOutputStream(String name) throws IOException public ByteArrayOutputStream() public PrintStream(File file) public PrintStream(String fileName)
Background image of page 5

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

View Full Document Right 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' 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
Background image of page 7

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

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

{[ snackBarMessage ]}

Page1 / 23

22-io_inheritance - CSE143 Lecture22...

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