Text I0 & Binary IO
There are two types of files in Java - text files and binary files. Files provide both sequential
and random access. A text file is processed as a sequence of characters. A binary file is
processed as a sequence of bytes. In a text file you have the illusion that the file is divided
into lines.
A big advantage of text files is their portability. In binary files, the representation used varies
from computer to computer. Java binary files are platform independent. They can be
interpreted by any computer that supports Java.
Reading from a Text File
Reading from a text file is facilitated by the class FileReader. Unfortunately, the methods
available from the class FileReader are not convenient for text processing. We usually embed
this stream within a BufferedReader object. FileReader will throw the exception
FileNotFoundException so the input statements should be enclosed in a try block and the
exception handled in the catch block.
import [Link].*;
public class ReadTextFile
public static void main (String [] args) throws IOException
File inFile = new File ("[Link]");
FileReader fReader = new FileReader (inFile);
BufferedReader bReader = new BufferedReader (fReader);
try
String line = [Link]();
while (line != null)
[Link] (line);
line = [Link]();
}
catch (Exception e)
[Link] ([Link]());
[Link]();
[Link](0);
finally
[Link]();
Writing to a Text File
To write text to a file you open an output stream by using the class FileWriter. If the file does
not exist a new empty file with this name is created. If the file already exists opening it erases
the data in the file. If you want to append to the file use the following option when creating
the FileWriter object:
FileWriter fWriter = new FileWriter (outFile, true);
The class PrintWriter has methods print(), printf() and println() that will allow us to write to a
file.
import [Link].*;
public class WriteTextFile
public static void main (String [] args) throws IOException
{
File outFile = new File ("[Link]");
FileWriter fWriter = new FileWriter (outFile);
PrintWriter pWriter = new PrintWriter (fWriter);
[Link] ("This is a line.");
[Link] ("This is another line."):
[Link]();
Binary Files
When designing binary file I/O applications it is a good idea to design the input and output
methods together. It is convenient to save data of different types such as int, double, and
String to files in such a way that these can be read back as integers, doubles, and strings.
Writing to a Binary File
The class DataOutputStream is used to output several types of data. It has methods:
void writeChar ( char ch )
void writeDouble ( double d )
void writeInt ( int i )
void writeUTF ( String s )
UTF stands for Unicode Text Format. It is the coding scheme for Java's Unicode character
set.
import [Link].*;
public class WriteBinaryFile
public static void main (String [] args) throws IOException
File outFile = new File ("[Link]");
FileOutputStream outStream = new FileOutputStream (outFile);
DataOutputStream output = new DataOutputStream (outStream);
String name = "John Doe";
long ssNum = 123456789;
double gpa = 3.85;
try
[Link] (name);
[Link] (ssNum);
[Link] (gpa);
catch (Exception e)
[Link] ([Link]());
[Link]();
[Link](0);
finally
[Link]();
Reading from a Binary File
The class DataInputStream is used to input several types of data. This class has the following
methods that allows us to input data:
char readChar()
double readDouble()
int readInt()
String readUTF()
import [Link].*;
public class ReadBinaryFile
public static void main (String [] args) throws IOException
File inFile = new File ("[Link]");
FileInputStream inStream = new FileInputStream (inFile);
DataInputStream input = new DataInputStream (inStream);
String name;
long ssNum;
double gpa;
try
while (true)
name = [Link]();
ssNum = [Link]();
gpa = [Link]();
[Link] (name + " " + ssNum + " " + gpa);
catch (EOFException e)
{
// Do nothing if it is the end of file.
catch (Exception e)
[Link] ([Link]());
[Link]();
[Link](0);
finally
[Link]();