3.2.4 Text Output3.2.3.4 String Tokenization3.2 Streams and Files3.2.5 Binary Input and Output

3.2.4 Text Output

Compared to the complexity of handling text input, writing output to a text file is rather simple. The basis of text output to files is the class FileWriter:  

   public class FileWriter extends Writer
   {
      public FileWriter(String fileName) throws IOException;
      public void close() throws IOException;
      ...
   }

We have to create a FileWriter object which opens the denoted file for writing; if this is not possible (e.g., if we do not have the corresponding directory permissions), an IOException is raised.

   String fileName = "out.txt";
   FileWriter fwriter = new FileWriter(fileName);

Writing to a text file character by character is rather inefficient; it is better to have the output operations buffered in memory such that only larger blocks of characters are written. For this purpose, we have the class BufferedWriter:

 

   public class BufferedWriter extends Writer
   {
     public BufferedWriter(Writer writer);
     public void write(char[] cbuf, int off, int len) 
       throws IOException;
     public void flush() throws IOException;
     public void close() throws IOException;
     ...
   }

An object of this class may be connected to an arbitrary Writer object writer. Its core is the method write which allows to write characters from an array segment cbuf[off...off+len-1]. However, when this method returns it is not guaranteed that the characters have been actually written to writer; they may still reside in the internal buffer maintained by BufferedWriter. Only when the method flush is called, we are sure that the previously written characters have been transferred to writer.

We can create a new BufferedWriter stream bwriter that is connected the stream fwriter created above:

   BufferedWriter bwriter = new BufferedWriter(fwriter);

 

The interface of BufferedWriter is too low-level for most applications; more functionality is provided by the PrintWriter class:

   public class PrintWriter extends Writer
   {
     public void PrintWriter(Writer writer);
     public void flush() throws IOException;
     public void close() throws IOException;

     public void print(int x);
     public void println(int x);
     ...
   }

We can thus create a PrintStream object that is connected to the stream bwriter created above as:

   PrintWriter out = new PrintWriter(bwriter);

PrintWriter implements all of the methods found in class PrintStream (of which System.out is an object); we can thus use out much like System.out.

   out.println("Number: " + 1);

 

When the file is closed, the contents are permanent in the disk file:

   out.close();

Example  The following method takes a number n and a file name name and writes the first positive n integer values and their reciprocal values to the corresponding disk file. The success of the operation is denoted by the boolean return value.
   public static boolean writeTable(int n, String name)
   {
     PrintWriter out = null;
     try
     {
       FileWriter fwriter = new FileWriter(name);
       BufferedWriter bwriter = new BufferedWriter(bwriter);
       out = new PrintWriter(bwriter);
       for (int i=1; i<=n; i++)
       {
         double r = 1.0/i;
         out.println(i + "\t" + r);
       }
     }
     catch(IOException e)
     {
        return false;
     }
     finally
     {
       try { if (out != null) out.close(); } catch(IOException e) { }
     }
     return true;
   }


© Wolfgang Schreiner; February 3, 2005

3.2.4 Text Output3.2.3.4 String Tokenization3.2 Streams and Files3.2.5 Binary Input and Output