Title: Java Input and Output
1Java Input and Output
- We will concentrate on IO from and to a file
- Can use files to store persistent data
- For instance, visitor counter
- Java provides the java.io package for io
operations
2Reading from and Writing to files
- Java supports two file types text and binary
- Text ? data is stored as characters
- Binary ? data is stored as raw bytes
- The type of the file is determined when the file
is written and depends on which class was used to
write to the file - ? to read from an existing file, we must know the
file type in order to select the appropriate
classes
3Java Reading from text files
- The class FileReader is designed to read
character files - It has several constructors, one of them takes a
String as its argument, representing the name of
the file - FileReader does not use buffering to read ahead ?
it is inefficient to read many lines of text - The class BufferedReader uses buffering, in
particular with its readLine method
4Java Reading from text files
- FileReader Constructor
- FileReader( String fileName ) throws
FileNotFoundException - BufferedReader constructor
- BufferedReader( Reader r )
- Class FileReader is a subclass of class Reader ?
a FileReader object is a Reader object ? can use
it as the argument of the BufferedReader
constructor
5Java Reading from text files
- Methods of class BufferedReader
- String readLine( ) throws IOException
- // reads a line of text returns a null String
when the end of the file is reached / - void close( )
- / releases resources allocated to the
BufferedReader object /
6Java Reading from text files
- Need to import the following classes
- java.io.FileReader
- java.io.BufferedReader
- java.io.FileNotFoundException
- Java.io.IOException
7Java Reading from text files
- try
-
- FileReader fr new FileReader( fileName )
- BufferedReader br new BufferedReader( fr )
- String s br.readLine( )
- // use a while loop to read the whole file if
needed - // process s
- br.close( )
-
8Java Reading from text files
-
- catch( FileNotFoundException fnfe )
-
- // output message ( file not found )
-
- catch( IOException ioe )
-
- // output message ( reading problem )
9Java Writing and appending
10Java Writing to text files
- Symmetry with reading from text files
- FileWriter class
- BufferedWriter class
11Java Writing to text files
- // FileWriter constructor
- FileWriter( String fileName, boolean mode)
throws IOException - mode false ? write
- mode true ? append
- // BufferedWriter constructor
- BufferedWriter( Writer w )
- Class FileWriter is a subclass of class Writer ?
a FileWriter object is a Writer object ? can use
it as the argument of the BufferedWriter
constructor
12Java Writing to text files
- Methods of class BufferedWriter
- void write( String s ) throws IOException
- void newLine( )
- // writes a line separator
- void close( )
- / releases resources allocated to the
BufferedWriter object /
13Java Writing to text files
- Need to import the following classes
- java.io.FileWriter
- java.io.BufferedWriter
- java.io.IOException
14Java Writing to text files
- try
-
- FileWriter fw new FileWriter( fileName, false
) - BufferedWriter bw new BufferedWriter( fw )
- bw.write( Hello world )
- bw.newLine( )
- bw.write( hello again )
- bw.close( )
-
- catch( IOException ioe )
-
- // feedback on exception that occured
15Java Writing basic data types text files
- We can write basic data types, such as booleans,
ints, doubles, chars, .. Directly to files - We can also write objects to file (and read them
as objects)
16Java Writing basic data types to text files
- Class FileOutputStream is designed to write a
stream of bytes to files - Class PrintWriter is designed to convert basic
data types to characters and write them to files
17Java Writing basic data types to text files
- // FileOutputStream constructor
- FileOutputStream( String fileName, boolean mode)
throws FileNotFoundException - mode false ? write
- mode true ? append
- // PrintWriter constructor
- PrintWriter( OutputStream os )
- Class FileOutputStream is a subclass of class
OutputStream ? a FileOutputStream object is an
OutputStream object ? can use it as the argument
of the PrintWriter constructor
18Java Writing basic data types to text files
- Methods of class PrintWriter
- void print( DataType dt )
- // writes the argument to the file
- void println( DataType dt )
- // same as print newline character
- void close( )
19Java Writing basic data types to text files
- Need to import the following classes
- java.io.FileOutputStream
- java.io.PrintWriter
- java.io.FileNotFoundException
20Java Writing basic data types to text files
- try
-
- FileOutputStream fos new FileOutputStream(
fileName, - false )
- PrintWriter pw new PrintWriter( fos )
- pw.println( 95 )
- pw.println( A )
- pw.println( true )
- pw.close( )
-
- catch( FileNotFoundException ioe )
-
- // output file not found
21Java Reading/Writing objects to files
- We can write objects to a file
- The objects must be instances of a class that
implement the interface Serializable - Use the ObjectOutputStream class to write to the
file (writeObject method) - Use the ObjectInputStream class to read objects
from a file (readObject method) - When the end of the file is reached, an
EOFException is thrown
22Java Visitor counter
- Store visitor count in a file
- Read file into a variable (use BufferedReader)
- Close file
- Increment variable by 1
- Write back variable to file (use PrintWriter)
- Close file