public class SafeFile extends Object
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(File.getNewFile("\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.
Modifier and Type | Field and Description |
---|---|
protected File |
file |
protected String |
fileName |
protected boolean |
keepBackup |
Constructor and Description |
---|
SafeFile(File file)
Create a new SafeFile.
|
SafeFile(File file,
boolean keepBackup)
Create a new SafeFile.
|
Modifier and Type | Method and Description |
---|---|
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.
|
byte[] |
readAll() |
void |
replace(byte[] data) |
void |
replace(byte[] data,
int offset,
int length) |
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:
|
protected File file
protected String fileName
protected boolean keepBackup
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.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.IOException
protected String getNewFileName()
protected File getBackupFile()
protected String getBackupFileName()
public boolean backupCurrent() throws IOException
IOException
protected boolean swapToNew() throws IOException
IOException
public void replace(byte[] data) throws IOException
IOException
public void replace(byte[] data, int offset, int length) throws IOException
IOException
public byte[] readAll() throws IOException
IOException