Java: Text File IO – Input (Swing)

Posted February 14, 2006 by William_Wilson in Java programming

The following tutorial explains the input of text files in Java.


These instructions are used to read in the files created in the output tech-recipe:
*The code segments will simply read the *.dat files to the console, but they can easily be re-directed to a JTextArea, or JList, etc.

When reading a file in, there are three exceptions that can be thrown:
java.io.EOFException
-which occurs if the End Of File marker is not found
This marker is automatically placed on a file with the .close() method and resembles the symbol /0.

java.io.FileNotFoundException
-which occurs if the file specified does not exist or cannot be opened

and
java.io.IOException
-which occurs if any other form of IO error occurs, such as a buffer overrun, underrun, corruption of data, and many other issues

CODE example:
/*This example reads a text file with FileInputStream */
import java.io.*;
public class FileInputStreamTest {
public static void main(String args[]) {
try {
FileInputStream in = new FileInputStream(“myFile.dat”);
while(in.available() > 0)
System.out.print((char)in.read() + ” “);
in.close();
} catch (FileNotFoundException e) {
System.out.println(“Error: Cannot open file for reading.”);
} catch (EOFException e) {
System.out.println(“Error: EOF encountered, file may be corrupted.”);
} catch (IOException e) {
System.out.println(“Error: Cannot read from file.”);
}
}
}

This program reads in the file one character at a time, and inputs a space in between each letter. The (char) is next to the in.read line, to typecast which essentially means to change the integers it will be reading back into their character representation.
*Try removing the typecasting and see what happens.*

notice that we still have to close the stream even though it is an input stream now.
-The JVM default is still to close these upon exit, but it can have odd affects on the rest of your program if they are left open.

To read in a file with DataOutputStream is very similar:
Code example:
/*This program reads in a text file using DataInputStream */
import java.io.*;
public class DataInputStreamTest {
public static void main(String args[]) {
try {
DataInputStream in = new DataInputStream(new FileInputStream(“myAccount.dat”));
String text = in.readUTF();
String text2 = in.readUTF();
in.close();
System.out.println(text + ” ” + text2);
} catch (FileNotFoundException e) {
System.out.println(“Error: Cannot open file for reading.”);
} catch (EOFException e) {
System.out.println(“Error: EOF encountered, file may be corrupted.”);
} catch (IOException e) {
System.out.println(“Error: Cannot read from file.”);
}
}
}

*Again we must close the stream.
This method is more complex as each type must be read in according to the order it was output. For this example, there is only one type of output into the text file, but there are many more types. (read ouput of text files)

Questions and Comments to: [email protected]
-William. ยง (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.