|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--lava.io.FileRewriter
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.
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 |
public FileRewriter(java.io.File file) throws java.io.FileNotFoundException, java.io.IOException
file
- the file to rewritejava.io.FileNotFoundException
- if the specified file does not exist, is not readable, or is not writeablejava.io.IOException
- if an I/O error occursMethod Detail |
public final java.io.InputStream getInputStream() throws java.io.IOException
InputStream
that reads from the file being rewritten.FileRewriter
java.io.IOException
- if an I/O error occurspublic final java.io.Reader getReader() throws java.io.IOException
Reader
that reads from the file being rewritten.FileRewriter
java.io.IOException
- if an I/O error occurspublic final java.io.OutputStream getOutputStream() throws java.io.IOException
OutputStream
that writes to the file being rewritten.FileRewriter
java.io.IOException
- if an I/O error occurspublic final java.io.Writer getWriter() throws java.io.IOException
Writer
that writes to the file being rewritten.FileRewriter
java.io.IOException
- if an I/O error occurs
|
Sharkysoft home | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |