Javaio
Javaio
Part I
General Principles
2
Streams
3
How to do I/O
import java.io.*;
4
open
use
Why Java I/O is hard close
5
open
use
Opening a stream close
6
open
use
Example of opening a stream close
7
open
use
Using a stream close
8
open
use
Example of using a stream close
int charAsInt;
charAsInt = fileReader.read( );
10
open
use
Reading lines close
String s;
s = bufferedReader.readLine( );
11
open
use
Closing close
Part II
LineReader and LineWriter
14
My LineReader class
class LineReader {
BufferedReader bufferedReader;
15
Basics of the LineReader
constructor
Create a FileReader for the named file:
FileReader fileReader =
new FileReader(fileName);
16
The full LineReader constructor
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);
}
17
readLine
String readLine( ) {
try {
return bufferedReader.readLine( );
}
catch(IOException e) {
e.printStackTrace( );
}
return null;
}
18
close
void close() {
try {
bufferedReader.close( );
}
catch(IOException e) { }
}
19
How did I figure that out?
I wanted to read lines from a file
I thought there might be a suitable readSomething method,
so I went to the API Index
Note: Capital letters are all alphabetized before lowercase in the Index
I found a readLine method in several classes; the most
promising was the BufferedReader class
The constructor for BufferedReader takes a Reader as an
argument
Reader is an abstract class, but it has several implementations,
including InputStreamReader
FileReader is a subclass of InputStreamReader
There is a constructor for FileReader that takes as its
argument a (String) file name
20
The LineWriter class
class LineWriter {
PrintWriter printWriter;
21
The constructor for LineWriter
LineWriter(String fileName) {
try {
printWriter =
new PrintWriter(
new FileOutputStream(fileName),
true);
}
catch(Exception e) {
System.err.println("LineWriter can’t " +
"use output file: " + fileName);
}
}
22
Flushing the buffer
23
PrintWriter
24
writeLine
25
close
void close( ) {
printWriter.flush( );
try {
printWriter.close( );
}
catch(Exception e) { }
}
26
Simple Java I/O
Part III
JFileChooser (not done in class)
28
Typical JFileChooser window
29
JFileChooser constructors
JFileChooser()
Creates a JFileChooser starting from the user’s directory
JFileChooser(File currentDirectory)
Constructs a JFileChooser using the given File as the path
JFileChooser(String currentDirectoryPath)
Constructs a JFileChooser using the given path
30
Useful JFileChooser methods I
int showOpenDialog(Component
enclosingJFrame);
Asks for a file to read; returns a flag (see below)
31
Useful JFileChooser methods II
File getSelectedFile()
showOpenDialog and showSaveDialog return a flag
telling what happened, but don’t return the selected file
After we return from one of these methods, we have to ask
the JFileChooser what file was selected
If we are saving a file, the File may not actually exist yet—
that’s OK, we still have a File object we can use
32
Using a File
Assuming that we have successfully selected a File:
33
Simple Java I/O
Part IV
Serialization(not done in class)
35
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
36
Implementing Serializable
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
37
Writing objects to a file
ObjectOutputStream objectOut =
new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(fileName)));
objectOut.writeObject(serializableObject);
objectOut.close( );
38
Reading objects from a file
ObjectInputStream objectIn =
new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(fileName)));
myObject = (itsType)objectIn.readObject( );
objectIn.close( );
39
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 ( );
}
40
The End
“There is no reason anyone would want a
computer in their home.”
--Ken Olson,
President/founder of Digital Equipment Corp.,
1977
--Thomas Watson
Chairman of IBM,|
1943
41