Sharkysoft home

lava.io
Class FileRewriter

java.lang.Object
  |
  +--lava.io.FileRewriter

public class FileRewriter
extends java.lang.Object

Rewrites files in place.

Details: A FileRewriter assists the user in rewriting files in place. Normally, when an application rewrites a file while reading it, it does so as follows: The file is accessed in read-only mode, while the revised contents are sent to a temporary file. Then, after the entire source file has been processed, the source file is closed, deleted, and the temporary file is renamed to the location of the original file. This class facilitates this process by shielding the user from most of these steps and performing them automatically.

Given a constructed instance of a FileRewriter, the user can obtain an input source by calling either getInputStream or getReader. Note that only one of these two input sources can be selected for any given FileRewriter instance. Similarly, an output destination can be obtained by calling either getOutputStream or getWriter, but not both. The objects returned by these methods can then be used to read, filter, and rewrite the file's contents.

When you have finished rewriting the file, simply call the close method of the source stream and destination stream. Be sure to close the source stream first, as closing the destination stream will cause the temporary file to automatically replace the source file.

Example: Rewrite the file 'story.txt', making sure all occurances of the letter 'a' are capitalized:

FileRewriter fr = new FileRewriter (new File ("story.txt")); Reader reader = fr . getReader (); Writer writer = fr . getWriter (); while (true) { int c = reader . read (); if (c < 0) break; if (c == 'a') writer . write ('A'); else writer . write ((char) c); } reader . close(); writer . close (); // Closes and renames the dest file.

Author:
Sharky

Constructor Summary
FileRewriter(java.io.File file)
          Initializes the members of this instance in preparation for rewriting the specified file.
 
Method Summary
 java.io.InputStream getInputStream()
          Returns an InputStream that reads from the file being rewritten.
 java.io.OutputStream getOutputStream()
          Returns an OutputStream that writes to the file being rewritten.
 java.io.Reader getReader()
          Returns a Reader that reads from the file being rewritten.
 java.io.Writer getWriter()
          Returns a Writer that writes to the file being rewritten.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileRewriter

public FileRewriter(java.io.File file)
             throws java.io.FileNotFoundException,
                    java.io.IOException
Initializes the members of this instance in preparation for rewriting the specified file.
Parameters:
file - the file to rewrite
Throws:
java.io.FileNotFoundException - if the specified file does not exist, is not readable, or is not writeable
java.io.IOException - if an I/O error occurs
Method Detail

getInputStream

public final java.io.InputStream getInputStream()
                                         throws java.io.IOException
Returns an InputStream that reads from the file being rewritten.
Returns:
the input source for this FileRewriter
Throws:
java.io.IOException - if an I/O error occurs

getReader

public final java.io.Reader getReader()
                               throws java.io.IOException
Returns a Reader that reads from the file being rewritten.
Returns:
the input source for this FileRewriter
Throws:
java.io.IOException - if an I/O error occurs

getOutputStream

public final java.io.OutputStream getOutputStream()
                                           throws java.io.IOException
Returns an OutputStream that writes to the file being rewritten.
Returns:
the output source for this FileRewriter
Throws:
java.io.IOException - if an I/O error occurs

getWriter

public final java.io.Writer getWriter()
                               throws java.io.IOException
Returns a Writer that writes to the file being rewritten.
Returns:
the output source for this FileRewriter
Throws:
java.io.IOException - if an I/O error occurs

Sharkysoft home