Java: text file IO – output (swing)

Contributor Icon Contributed by William_Wilson Date Icon February 14, 2006  
Tag Icon Tagged: Java programming

the output of text files in Java


The output streams in Java may throw exceptions (the recipe on Exceptions should probably be read first, assuming i’ve written already), so these cases must be handled or caught.
The easier of the 2 IO options is output, so we will start with that:
Keep in mind that there are many ways to output text in Java, this recipe covers a couple of the more basic ways.

The 2 exceptions that must be handled are:
java.io.FileNotFoundException
-which occurs if the specified input or output file cannot be found or created.
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.

*be sure to import the java.io.* if you do not know the exact extension of the stream you are using.

Code Example:
/*This first example uses the most basic (I think) of all the output streams, the FileOutputStream */
import java.io.*;
public class FileOutputStreamTest {
public static void main(String args[]) {
try {
FileOutputStream out = new FileOutputStream(”myFile.dat”);
out.write(’H'); out.write(’E');
out.write(76); out.write(’L');
out.write(’O'); out.write(’!');
out.close();
} catch (FileNotFoundException e) {
System.out.println(”Error: Cannot open file for writing.”);
} catch (IOException e) {
System.out.println(”Error: Cannot write to file.”);
}
}
}

The file outputStream can handle integers or characters, if an integer is provided it’s character equivalent is supplied instead.
-In this case the output text is: HELLO!

When an output stream of any type is assigned a value (ie: given a location or directory to write a file too) it is open and remains open until either the close() method is called upon it, or the end of the program.
-java’s JVM by default will close all open streams to prevent memory leak and corruption, but not all compilers will catch this, so be careful.

*notice the error messages in both of the Exception catch blocks, each is written in such a way as to describe which error was triggered, or caught

Another output CODE example
/*This example uses the DataOutputStream instead, showing the use of UTF*/
import java.io.*;
import java.text.*;
public class FileOutputStreamTest {
public static void main(String args[]) {
try{
String theText = “HELLO!”;
DataOutputStream out = new DataOutputStream(new FileOutputStream(”myFile2.dat”));
out.writeUTF(theText);
//add my tag to the end of the converted file
out.writeUTF(”\n-William Wilson. ยง”);
out.close();
} catch (FileNotFoundException e) {
System.out.println(”Error: Cannot open file for writing.”);
} catch (IOException e) {
System.out.println(”Error: Cannot write to file.”);
}}}

In this example the file is written to using the UTF extension method which handles strings with this stream. This stream is more difficult as each type (int, String, byte, etc) must be handled seperatly, but allows for seperation. There are many types of write allowed with this method:

* writeUTF(String aString)
* writeInt(int anInt)
* writeFloat(float aFloat)
* writeLong(long aLong)
* writeDouble(double aDouble)

* writeShort(short aShort)
* writeBoolean(boolean aBool)
* writeByte(int aByte)
* writeChar(char aChar)

*NOTE if you use the ObjectOutputStream and change the object that is being written after it is written, it most likely cannot be read in afterwards as it is not serializable, which i will cover soon.

again the stream must be closed with close(), and both errors must be caught.

Questions and comments to: william_a_wilson@hotmail.com

Previous recipe | Next recipe |
 
blog comments powered by Disqus