10.2 Writing Data to a Text File
The simplest way to write text to a file requires us to use PrintWriter class from the standard package java.io . The class PrintWriter has the familiar print() and println() methods we have been using for writing to the console. The following program writes the name of four oceans to the text file.
import java.io.PrintWriter; // Step 1 import java.io.IOException; public class FileWriteDemo { public static void main(String[] args) throws IOException { // Open the file. PrintWriter out = new PrintWriter("oceans.txt"); // Step 2 // Write the name of four oceans to the file out.println("Atlantic"); // Step 3 out.println("Pacific"); out.println("Indian"); out.println("Arctic"); // Close the file. out.close(); // Step 4 } }
Now discuss the above prgram step by step:
In step 1, you import the package to use PrintWriter class.
import java.io.PrintWriter; // Step 1
In step 2, you create the PrintWriter object out and associates it with the file hello.txt .
PrintWriter out = new PrintWriter("oceans.txt");// Step 2
In step 3, you store the output in the file oceans.txt.
out.println("Atlantic");// Step 3
In step 4, once the output is completed, you close the output files by using the method close.
out.close();
Adding a throws Clause to the Method Header
During program execution, various things can happen — For example, suppose you create a PrintWriter object and pass the name of a file to its constructor The PrintWriter object attempts to create the file, but unexpectedly the disk is full and the file cannot be created. If such things happen, we say that an exception has occurred. If an exception occurs in a method, then the method should either handle the exception or throw it for the calling environment to handle.
In next chapter you will learn all about exceptions, but for now, we will simply allow our methods to throw any exceptions that might occur. For example our main method should look like this :
public static void main(String[] args) throws IOException
When above program is compiled and run output is stored in text file oceans.txt. The following figure shows the contents of the file displayed in Notepad.
Appending Data to a File
Remember that if a file that you are opening with the PrintWriter object with the name already exists in the directory it will be deleted and a new empty file with the same name is created. It's often useful to be able to append data to an existing file rather than overwriting it. To append data in an existing file you can use FileWriter class. You pass two arguments to the FileWriter constructor: name of the file, and the boolean value true. Here is an example:
FileWriter fw = new FileWriterl("hello.txt", true);
Any data written to the file will be appended to the file's existing contents. (If the file does not exist, it will be created.)
When you create the Printwriter object, you pass a reference to the FileWriter object as an argument to the Printwriter constructor. For example, look at the following code:
FileWriter fw = new FileWriter("oceans.txt",true); Printwriter out = new PrintWriter(fw);
Here is a complete example :
import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class AppendFileDemo { public static void main(String[] args) throws IOException { // Open the file in append mode. FileWriter fw = new FileWriter("oceans.txt",true); PrintWriter out = new PrintWriter(fw); // Append the name of ocean to the file out.println("Southern"); // Close the file. out.close(); } }
When above program is compiled and run output is appended in text file oceans.txt. The following figure shows the contents of the file displayed in Notepad.
