Simple Java I/O: General Principles
Simple Java I/O: General Principles
Part I
General Principles
Streams
• All modern I/O is stream-based
• A stream is a connection to a source of data or to a
destination for data (sometimes both)
• An input stream may be associated with the keyboard
• An input stream or an output stream may be
associated with a file
• Different streams have different characteristics:
– A file has a definite length, and therefore an end
– Keyboard input has no specific end
How to do I/O
import java.io.*;
String s;
s = bufferedReader.readLine( );
Part II
LineReader and LineWriter
Text files
class LineReader {
BufferedReader bufferedReader;
LineReader(String fileName) {
FileReader fileReader = null;
try { fileReader = new FileReader(fileName); }
catch (FileNotFoundException e) {
System.err.println
("LineReader can't find input file: " + fileName);
e.printStackTrace( );
}
bufferedReader = new BufferedReader(fileReader);
}
readLine
String readLine( ) {
try {
return bufferedReader.readLine( );
}
catch(IOException e) {
e.printStackTrace( );
}
return null;
}
close
void close() {
try {
bufferedReader.close( );
}
catch(IOException e) { }
}
How did I figure that out?
class LineWriter {
PrintWriter printWriter;
LineWriter(String fileName) {
try {
printWriter =
new PrintWriter(
new FileOutputStream(fileName), true);
}
catch(Exception e) {
System.err.println("LineWriter can't " +
"use output file: " + fileName);
}
}
Flushing the buffer
void close( ) {
printWriter.flush( );
try { printWriter.close( ); }
catch(Exception e) { }
}
Simple Java I/O
Part III
FileDialogs
About FileDialogs
• The FileDialog class displays a window from
which the user can select a file
• The FileDialog window is modal--the
application cannot continue until it is closed
• Only applications, not applets, can use a
FileDialog; only applications can access files
• Every FileDialog window is associated with
a Frame
Typical FileDialog window
FileDialog constructors
• FileDialog(Frame f)
– Creates a FileDialog attached to Frame f
• FileDialog(Frame f, String title)
– Creates a FileDialog attached to Frame f, with the
given title
• FileDialog(Frame f, String title, int type)
– Creates a FileDialog attached to Frame f, with the
given title; the type can be either FileDialog.LOAD or
FileDialog.SAVE
Useful FileDialog methods I
• String getDirectory()
– Returns the selected directory
• String getFile()
– Returns the name of the currently selected file,
or null if no file is selected
• int getMode()
– Returns either FileDialog.LOAD or
FileDialog.SAVE, depending on what the
dialog is being used for
Useful FileDialog methods II
• void setDirectory(String directory)
– Changes the current directory to directory
• void setFile(String fileName)
– Changes the current file to fileName
• void setMode(int mode)
– Sets the mode to either FileDialog.LOAD or
FileDialog.SAVE
Using a FileDialog
• Using a FileDialog isn’t difficult, but it is
lengthy
• See my LineReader class (in the
Encryption assignment) for a complete
example
Simple Java I/O
Part IV
Serialization
Serialization
• You can also read and write objects to files
• Object I/O goes by the awkward name of
serialization
• Serialization in other languages can be very
difficult, because objects may contain
references to other objects
• Java makes serialization (almost) easy
Conditions for serializability
• If an object is to be serialized:
– The class must be declared as public
– The class must implement Serializable
– The class must have a no-argument constructor
– All fields of the class must be serializable:
either primitive types or serializable objects
Implementing the Serializable
interface
• To “implement” an interface means to define all
the methods declared by that interface, but...
• The Serializable interface does not define any
methods!
– Question: What possible use is there for an interface
that does not declare any methods?
– Answer: Serializable is used as flag to tell Java it
needs to do extra work with this class
open
use
close Writing objects to a file
ObjectOutputStream objectOut =
new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(fileName)));
objectOut.writeObject(serializableObject);
objectOut.close( );
open
use
close Reading objects from a file
ObjectInputStream objectIn =
new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(fileName)));
myObject = (itsType)objectIn.readObject( );
objectIn.close( );
What have I left out?
• Encrypted files, compressed files, files sent
over internet connections, ...
• Exceptions! All I/O involves Exceptions!
• try { statements involving I/O }
catch (IOException e) {
e.printStackTrace ( );
}
The End