|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectewe.io.SafeFile
This is used to "safely" modify the contents of a file. This is used when it is critical that the information in a file not be corrupted due to a crash during writing.
To use it to modify a file you would follow the following procedure.
1. Create a SafeFile for the actual data file.
SafeFile sf = new SafeFile(new File("\Program Files\MyApp\Config.dat"));
2. Request a temporary file to write new data to.
File f = sf.getTempFile();
3. Copy or write the data to the temp file.
4. When the file is complete call setNewFile() to cause it to switch over
to the temp file.
sf.setNewFile(f);
This does the following:
- The temporary file is renamed to a ".new" file.
- Any ".bak" (backup) file is deleted.
- The actual data file is renamed to a ".bak" file.
- The ".new" file is renamed to the actual data file.
- If the SafeFile is created with keepBackup as false the ".bak" file is deleted.
This sequence is such that if at any point in the procedure there should be a system
crash, either the original file would be unchanged, or it can be recovered from the ".bak"
(the old version) or ".new" (the new version).
Creating a SafeFile() for an actual file will prompt the system to ensure that the file exists. If it does not it will be recreated (if possible) from the ".bak" or ".new" file. Therefore when accessing that particular file, you should always use a SafeFile() to do so.
| Field Summary | |
protected File |
file
|
protected String |
fileName
|
protected boolean |
keepBackup
|
| Constructor Summary | |
SafeFile(File file)
Create a new SafeFile. |
|
SafeFile(File file,
boolean keepBackup)
Create a new SafeFile. |
|
| Method Summary | |
boolean |
backupCurrent()
NOT IMPLEMENTED YET. |
protected File |
getBackupFile()
|
protected String |
getBackupFileName()
|
File |
getFile()
Get the actual file. |
protected String |
getFullPath()
|
protected File |
getNewFile()
|
protected String |
getNewFileName()
|
File |
getTempFile()
Get a temporary file that will eventually be used to replace the actual file. |
boolean |
setNewFile(File f)
Safely rename the file so that it becomes the orignal file. |
boolean |
setNewFile(File f,
boolean updateTime)
Safely rename the file so that it becomes the orignal file. |
protected boolean |
swapToNew()
This will do the following: |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, toString |
| Field Detail |
protected File file
protected String fileName
protected boolean keepBackup
| Constructor Detail |
public SafeFile(File file,
boolean keepBackup)
throws IOException
file - The actual file to be updated.keepBackup - specifies whether a backup of the previous version is to be kept.
IOException - if an exception occurs.
public SafeFile(File file)
throws IOException
file - The actual file to be updated.
IOException - if an exception occurs.| Method Detail |
public File getFile()
protected String getFullPath()
protected File getNewFile()
public File getTempFile()
public boolean setNewFile(File f)
throws IOException
f - The new file to be renamed.
IOException
public boolean setNewFile(File f,
boolean updateTime)
throws IOException
f - The new file to be renamed.updateTime - true if the new file should have its time updated to the current time.
IOExceptionprotected String getNewFileName()
protected File getBackupFile()
protected String getBackupFileName()
public boolean backupCurrent()
throws IOException
IOException
protected boolean swapToNew()
throws IOException
IOException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||