Input and Output in Java Thursday, Jan 24, 2008 Nancy L. Harris
Reference for this topic http://java.sun.com/docs/books/tutorial/essential/io/index.html 1/18/2006 CS239 – Spring 2006 2
Pictorial view of data streams (from the Java tutorial) 1/18/2006 CS239 – Spring 2006 3
What can we read and write? Bytes – used for binary data, sounds, pictures Characters – used for textual data We will focus on character data 1/18/2006 CS239 – Spring 2006 4
What does a “stream” look like It is not organized as we are used to looking at a “file”. It is conceptually an infinitely long series of bytes. Some readers deal with those bytes as text characters. And each format item (new lines, tabs, spaces) have a corresponding character representation. 1/18/2006 CS239 – Spring 2008 5
What’s a file? A “file” can be thought of as a named bunch of sequential data. That data can be binary (like executable programs) or it can be textual (like the source files you make with JGrasp). Text files are still binary, but their data can be directly interpreted as characters from the Unicode character set. 1/18/2006 CS239 – Spring 2006 6
Processing a file To read from a file To write to a file Open the file Read its data Close the file To write to a file Write its data 1/18/2006 CS239 – Spring 2006 7
Making a copy of a file Involves reading from a source and writing to a target. Demo 1/18/2006 CS239 – Spring 2006 8
Some fun things you can do with File See the java api’s for some techniques for preventing file exceptions. 1/24/2008 CS239 – Spring 2008 9