Saturday, December 14, 2024
HomeComputer programmingJava programmingJava: Text File IO - Output (Swing)

Java: Text File IO – Output (Swing)

The following tutorial describes the output of text files in Java.


The output streams in Java may throw exceptions, so these cases must be handled or caught.
The easier of the two IO options is output, so we will start with that:
Keep in mind that there are many ways to output text in Java. This tech-recipe only covers a few the basic methods.

The two exceptions that must be handled are as follows:
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 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, its character equivalent is supplied instead.
-In this case, the output text is as follows: HELLO!

When an output stream of any type is assigned a value (i.e., given a location or directory to write a file), 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. Use caution.

*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 separately, but allows for separation. 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.

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

Questions and comments to: [email protected]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!