Back to ICS211

Mobius strip

Java I/O

Java input and output is accomplished using the classes in the java.io package. You can learn more about them in the Java API. The Java Tutorial also offers an overview.

InputStreams and OutputStreams deal with bytes. Readers and Writers deal with characters. InputStreamReaders translate bytes to characters for input, and OutputStreamWriters translate characters to bytes for output. Bytes need to be translated into characters (and vice versa) based on a character encoding (such as ANSI, Unicode, etc.), though usually these details are taken care of for you when you just implicitly accept the default encoding. Wrapping a stream (or reader or writer) with a buffer provides for more efficient reading and writing. Wrapping a stream with a PrintStream or PrintWriter means you can use the familiar print and println methods.

Between the Java API and Java Tutorial (and maybe a good book), you should be able to familiarize yourself with these different classes in order to know which tools best solve your current problem. However, what follows here are the most commonly-used configurations.

The four most common I/O problems you will face are writing to the screen, reading from the keyboard, and reading and writing to a text file.

Writing to the Screen

You already know how to do this--just write to System.out:

System.out.print("Text to the screen.");
System.out.println("Text to the screen with a newline after it.");

Reading User Input from the Keyboard

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String userInput = stdin.readLine();

Reading from a file

BufferedReader filein = new BufferedReader(new FileReader("samplefile.txt"));
String nextLine = filein.readLine();
filein.close();

Writing to a file

PrintWriter fileout = new PrintWriter(new BufferedWriter(new FileWriter("samplefile.txt"));
fileout.println("Some text I want in the file.");
fileout.close();


When performing the last three operations--keyboard input, file input, and file output--you will need to catch IOExceptions.

You can use different variable names for your BufferedReaders and PrintWriters than I have here. Also, you may have code between opening a stream, writing or reading to the stream, and closing the stream. Remember to close any file streams you open. This is especially important when writing to a file, since all your output may not be stored in the file if you forget to flush() or close() the stream.

It is also possible to use Java streams to read or write to other devices besides the hard disk, or to access resources across a network. We won't be covering such details in this course, but you can learn more from the Java API, the Java Tutorial, or a good book on Java programming.

Example Code

FileViewer.java
This program reads from a file and displays text to the screen.
FileAppender.java
This program gets user input from the keyboard and writes text to a file. It uses a different version of the FileWriter constructor than the example above because it appends to the file, rather than overwriting it.

Both of these example programs also show how to process command line arguments.



~ztomasze Index : TA Details: ICS211: Java I/O
http://www2.hawaii.edu/~ztomasze
Last Edited: 10 Mar 2005
©2002 by Z. Tomaszewski.