All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class sdsu.io.SimpleFile

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

public class SimpleFile
extends 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)

Constructor Index

 o SimpleFile(File, String)
Creates a File instance that represents the file with the specified name in the specified directory.
 o SimpleFile(String)
Creates a File instance that represents the file whose pathname is the given path argument.
 o SimpleFile(String, String)
Creates a File instance whose pathname is the pathname of the specified directory, followed by the separator character, followed by the name argument.

Method Index

 o append(String)
Appends the string tailContents to the end of the file.
 o backup()
Create a backup copy of the current file.
 o backup(String)
Create a backup copy of the current file.
 o getContents()
Returns the contents of the file.
 o setContents(String)
Set the contents of the file to be newContents to the end of the file.

Constructors

 o SimpleFile
 public SimpleFile(String path)
Creates a File instance that represents the file whose pathname is the given path argument.

Parameters:
path - the file pathname.
Throws: NullPointerException
if the file path is equal to null.
See Also:
getPath
 o SimpleFile
 public SimpleFile(String path,
                   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:
getPath, separator
 o SimpleFile
 public SimpleFile(File dir,
                   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:
getPath, separator

Methods

 o getContents
 public String getContents() throws FileNotFoundException, 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: FileNotFoundException
thrown if file name given constuctor does not exist.
Throws: IOException
Thrown if there is an IOException when reading the file.
 o append
 public void append(String tailContents) throws 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: IOException
Thrown if there is an IOException when writing to the file.
 o setContents
 public void setContents(String newContents) throws 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: IOException
Thrown if there is an IOException when writing to the file.
 o backup
 public void backup() throws 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: IOException
Thrown if there is an IOException when creating the backup file.
 o backup
 public void backup(String fileExtension) throws 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: IOException
Thrown if there is an IOException when creating the backup file.

All Packages  Class Hierarchy  This Package  Previous  Next  Index