ewe.io
Class SafeFile

java.lang.Object
  extended byewe.io.SafeFile

public class SafeFile
extends Object

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: Remove the old backup file (if it exists).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

file

protected File file

fileName

protected String fileName

keepBackup

protected boolean keepBackup
Constructor Detail

SafeFile

public SafeFile(File file,
                boolean keepBackup)
         throws IOException
Create a new SafeFile. The creation of the SafeFile will automatically cause the system to check to see if the file exists and if it does not, but a ".bak" or ".new" does exist, then the ".bak" or ".new" will be renamed to the actual file.

Parameters:
file - The actual file to be updated.
keepBackup - specifies whether a backup of the previous version is to be kept.
Throws:
IOException - if an exception occurs.

SafeFile

public SafeFile(File file)
         throws IOException
Create a new SafeFile. The creation of the SafeFile will automatically cause the system to check to see if the file exists and if it does not, but a ".bak" or ".new" does exist, then the ".bak" or ".new" will be renamed to the actual file.

Parameters:
file - The actual file to be updated.
Throws:
IOException - if an exception occurs.
Method Detail

getFile

public File getFile()
Get the actual file.

Returns:
The actual file.

getFullPath

protected String getFullPath()

getNewFile

protected File getNewFile()

getTempFile

public File getTempFile()
Get a temporary file that will eventually be used to replace the actual file. This file is always the same name as the actual file with a ".tmp" added to it.

Returns:
A new ".tmp" file.

setNewFile

public boolean setNewFile(File f)
                   throws IOException
Safely rename the file so that it becomes the orignal file.

Parameters:
f - The new file to be renamed.
Returns:
true if successful.
Throws:
IOException

setNewFile

public boolean setNewFile(File f,
                          boolean updateTime)
                   throws IOException
Safely rename the file so that it becomes the orignal file.

Parameters:
f - The new file to be renamed.
updateTime - true if the new file should have its time updated to the current time.
Returns:
true if successful.
Throws:
IOException

getNewFileName

protected String getNewFileName()

getBackupFile

protected File getBackupFile()

getBackupFileName

protected String getBackupFileName()

backupCurrent

public boolean backupCurrent()
                      throws IOException
NOT IMPLEMENTED YET. This will overwrite the old backup file (if it exists) with the current file. It leaves the current file as it is.

Throws:
IOException

swapToNew

protected boolean swapToNew()
                     throws IOException
This will do the following:
  • Remove the old backup file (if it exists).
  • Rename the current file to be the backup (".bak") file.
  • Rename the new (".new") file to be the current file.
  • If keepBackup is false, the backup will be deleted.

    Throws:
    IOException