About File Handling
File handling in Java is frankly a
bit of a pig’s ear, but it’s not too complicated once you understand a few
basic ideas. The key things to remember are as follows.
You can read files using these
classes:
- FileReader for text files in your system’s default encoding (for example, files containing Western European characters on a Western European computer).
- FileInputStream for binary files and text files that contain ‘weird’ characters.
FileReader (for text files) should usually be wrapped in a BufferedFileReader.
This saves up data so you can deal with it a line at a time or whatever instead
of character by character (which usually isn’t much use).
If you want to write files,
basically all the same stuff applies, except you’ll deal with classes named FileWriter
with BufferedFileWriter for text files, or FileOutputStream for
binary files.
Reading
Ordinary Text Files in Java
If you want to read an ordinary text
file in your system’s default encoding (usually the case most of the time for
most people), use FileReader and wrap it in a BufferedReader.
In the following program, we read a
file called “temp.txt” and output the file line by line on the console.
import
java.io.*;
public
class Test {
public static void main(String [] args) {
// The name of the file to open.
String fileName = "temp.txt";
// This will reference one line at a
time
String line = null;
try {
// FileReader reads text files in
the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in
BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line =
bufferedReader.readLine()) != null) {
System.out.println(line);
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file
'" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file
'"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
Writing Text Files in Java
To write a text file in Java, use FileWriter instead of FileReader, and BufferedOutputWriter instead of BufferedOutputReader. Simple eh?Here’s an example program that creates a file called ‘temp.txt’ and writes some lines of text to it.
import java.io.*;
public class Test {
public static void main(String [] args) {
// The name of the file to open.
String fileName = "temp.txt";
try {
// Assume default encoding.
FileWriter fileWriter =
new FileWriter(fileName);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
bufferedWriter.write("Hello there,");
bufferedWriter.write(" here is some text.");
bufferedWriter.newLine();
bufferedWriter.write("We are writing");
bufferedWriter.write(" the text to the file.");
// Always close files.
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println(
"Error writing to file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
Comments
Post a Comment