|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.darwinsys.io.FileSaver
public class FileSaver
Save a user data file, as safely as we can. The basic algorithm is:
Step 1 is implemented in the constructor. Step 2 you do, by calling getWriter or getOutputStream (not both). Step 3, 4 and 5 are done in finish().
Normal usage is thus:
try {
FileSaver saver = new FileSaver(file);
final Writer writer = saver.getWriter();
PrintWriter out = new PrintWriter(writer);
myWriteOutputFile(out);
out.close();
saver.finish();
System.out.println("Saved OK");
} catch (IOException e) {
System.out.println("Save FAILED");
}
Objects of this class may be re-used sequentially (for the same file) but are not thread-safe and should not be shared among different threads.
| Constructor Summary | |
|---|---|
FileSaver(java.io.File input)
|
|
| Method Summary | |
|---|---|
void |
finish()
Close the output file and rename the temp file to the original name. |
java.io.File |
getFile()
Return a reference to the contained File object, to promote re-use (File objects are immutable so this is at least moderately safe). |
java.io.OutputStream |
getOutputStream()
Return an output file that the client should use to write the client's data to. |
java.io.Writer |
getWriter()
Return an output file that the client should use to write the client's data to. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public FileSaver(java.io.File input)
throws java.io.IOException
java.io.IOException| Method Detail |
|---|
public java.io.File getFile()
if (fileSaver == null ||
!(fileSaver.getFile().equals(file))) {
fileSaver = new FileSaver(file);
}
public java.io.OutputStream getOutputStream()
throws java.io.IOException
java.io.IOException - if the temporary file cannot be written
public java.io.Writer getWriter()
throws java.io.IOException
java.io.IOException - if the temporary file cannot be written
public void finish()
throws java.io.IOException
java.io.IOException - If anything goes wrong
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||