sdsu.io
Class SimpleFile

java.lang.Object
  |
  +--java.io.File
        |
        +--sdsu.io.SimpleFile

public class SimpleFile
extends java.io.File

This class adds a few useful methods to java.io.File. This class provides methods to backup a file, to read the contents of a file, and write to a file. These methods are very simple, but are done so often it becomes annoying to implement each time they are needed.

Version:
1.0 5 January 1999
Author:
Roger Whitney (whitney@cs.sdsu.edu)
See Also:
Serialized Form

Fields inherited from class java.io.File
pathSeparator, pathSeparatorChar, separator, separatorChar
 
Constructor Summary
SimpleFile(java.io.File dir, java.lang.String name)
          Creates a File instance that represents the file with the specified name in the specified directory.
SimpleFile(java.lang.String path)
          Creates a File instance that represents the file whose pathname is the given path argument.
SimpleFile(java.lang.String path, java.lang.String name)
          Creates a File instance whose pathname is the pathname of the specified directory, followed by the separator character, followed by the name argument.
 
Method Summary
 void append(java.lang.String tailContents)
          Appends the string tailContents to the end of the file.
 void backup()
          Create a backup copy of the current file.
 void backup(java.lang.String fileExtension)
          Create a backup copy of the current file.
 java.lang.String getContents()
          Returns the contents of the file.
 void setContents(java.lang.String newContents)
          Set the contents of the file to be newContents to the end of the file.
 
Methods inherited from class java.io.File
canRead, canWrite, compareTo, compareTo, createNewFile, createTempFile, createTempFile, delete, deleteOnExit, equals, exists, getAbsoluteFile, getAbsolutePath, getCanonicalFile, getCanonicalPath, getName, getParent, getParentFile, getPath, hashCode, isAbsolute, isDirectory, isFile, isHidden, lastModified, length, list, list, listFiles, listFiles, listFiles, listRoots, mkdir, mkdirs, renameTo, setLastModified, setReadOnly, toString, toURL
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SimpleFile

public SimpleFile(java.lang.String path)
Creates a File instance that represents the file whose pathname is the given path argument.
Parameters:
path - the file pathname.
Throws:
java.lang.NullPointerException - if the file path is equal to null.
See Also:
File.getPath()

SimpleFile

public SimpleFile(java.lang.String path,
                  java.lang.String name)
Creates a File instance whose pathname is the pathname of the specified directory, followed by the separator character, followed by the name argument.
Parameters:
path - the directory pathname.
name - the file pathname.
See Also:
File.getPath(), File.separator

SimpleFile

public SimpleFile(java.io.File dir,
                  java.lang.String name)
Creates a File instance that represents the file with the specified name in the specified directory.

If the directory argument is null, the resulting File instance represents a file in the (system-dependent) current directory whose pathname is the name argument. Otherwise, the File instance represents a file whose pathname is the pathname of the directory, followed by the separator character, followed by the name argument.

Parameters:
dir - the directory.
name - the file pathname.
See Also:
File.getPath(), File.separator
Method Detail

getContents

public java.lang.String getContents()
                             throws java.io.FileNotFoundException,
                                    java.io.IOException
Returns the contents of the file. A Buffered Reader is opened on the file, contents of the file is read, and the file Reader is closed before returning the contents.

This is a very simple function, but is done often enough to have done for you. It also allows beginners to read files before having to master Readers/Writers.

Throws:
java.io.FileNotFoundException - thrown if file name given constuctor does not exist.
java.io.IOException - Thrown if there is an IOException when reading the file.

append

public void append(java.lang.String tailContents)
            throws java.io.IOException
Appends the string tailContents to the end of the file. File is created if it does not exist.
Parameters:
tailContents - String added to the end of the file.
Throws:
java.io.IOException - Thrown if there is an IOException when writing to the file.

setContents

public void setContents(java.lang.String newContents)
                 throws java.io.IOException
Set the contents of the file to be newContents to the end of the file. Current contents of the file are erased. File is created if it does not exist.
Parameters:
newContents - String used to replace current contents of the file.
Throws:
java.io.IOException - Thrown if there is an IOException when writing to the file.

backup

public void backup()
            throws java.io.IOException
Create a backup copy of the current file. The name of the backup file is formed by appending .bak to the current file name. If a file by that name already exists, it is deleted.
Throws:
java.io.IOException - Thrown if there is an IOException when creating the backup file.

backup

public void backup(java.lang.String fileExtension)
            throws java.io.IOException
Create a backup copy of the current file. The name of the backup file is formed by appending fileExtension to the current file name. If fileExtension does not begin with a period, a period is added between the file name and the fileExtension. If a file by that name already exists, it is deleted.
Throws:
java.io.IOException - Thrown if there is an IOException when creating the backup file.