com.glaivestone.javax.io
Class FileSystemServices

java.lang.Object
  |
  +--com.glaivestone.javax.io.FileSystemServices

public class FileSystemServices
extends java.lang.Object

Convenience services for working with files and directories in a file system.

The standard service provider for the host file system is accessed by referencing FileSystemServices.getCurrent().

Author:
Deb Lewis
, Gary Gregory

Field Summary
static FileSystemServices current
           
static int DEFAULT_COPY_BUFFER_SIZE
          Default size in bytes of the transfer buffer used by the copyFile operation.
 
Constructor Summary
FileSystemServices(int copyBufferSize)
          Create a new FileSystemServices instance which will use a copy buffer of the specified size in bytes for invocations of the copyFile operation.
 
Method Summary
 void clearCopyBuffer()
          Clear the transfer buffer used by the copyFile operation.
 void copyFile(java.io.File sourceFile, java.io.File destination)
          Copy the contents of the source file to the destination file system element.
 FileSystemOperationResult deleteAllFiles(java.io.File aDirectory)
          Delete all files in the specified directory and its subdirectories.
 FileSystemOperationResult deleteDirectory(java.io.File aDirectory)
          Delete the contents of the specified directory and all its subdirectories.
 FileSystemOperationResult deleteEmptyDirectories(java.io.File aDirectory)
          Delete all empty subdirectories under the given directory, along with the root directory itself if empty.
 FileSystemOperationResult deleteEmptySubdirectories(java.io.File aDirectory)
          Delete all empty subdirectories under the given directory.
 FileSystemOperationResult deleteFiles(java.io.File aDirectory)
          Delete all files in the specified directory.
static FileSystemServices getCurrent()
           
 int getDirectorySize(java.io.File aDirectory)
          Answer the number of file system elements (files and directories) which are directly contained in the given directory.
 java.lang.String getFileContents(java.io.File aFile)
          Answer a string containing the contents of the specified file.
 java.lang.String getFileContents(java.lang.String pathname)
          Answer a string containing the contents of the specified file.
 char[] getFileContentsAsCharArray(java.io.File aFile)
          Answer a character array containing the contents of the specified file.
 boolean isRealDirectory(java.io.File fileSystemElement)
          Answer whether the given file system element is a directory which actually exists.
 java.io.File realizeDirectory(java.io.File aDirectory)
          Ensure that the specified directory exists.
 java.io.File realizeDirectory(java.io.File aDirectory, java.lang.String relativePathName)
          Ensure that the specified directory exists.
 void setCopyBufferSize(int nBytes)
          Specify the size in bytes of the transfer buffer used by the copyFile operation.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_COPY_BUFFER_SIZE

public static final int DEFAULT_COPY_BUFFER_SIZE
Default size in bytes of the transfer buffer used by the copyFile operation.

See Also:
Constant Field Values

current

public static FileSystemServices current
Constructor Detail

FileSystemServices

public FileSystemServices(int copyBufferSize)
Create a new FileSystemServices instance which will use a copy buffer of the specified size in bytes for invocations of the copyFile operation.

Method Detail

getCurrent

public static FileSystemServices getCurrent()

clearCopyBuffer

public void clearCopyBuffer()
Clear the transfer buffer used by the copyFile operation.


copyFile

public void copyFile(java.io.File sourceFile,
                     java.io.File destination)
              throws java.io.FileNotFoundException,
                     java.io.IOException
Copy the contents of the source file to the destination file system element. If the destination is a directory, create a file of the same name as the source file in the destination directory.

It's very discouraging that this fundamental service is not exposed in the base system File class.

Throws:
java.io.FileNotFoundException - thrown if the source file cannot be accessed
java.io.IOException - thrown if the destination file cannot be created or copying fails

deleteAllFiles

public FileSystemOperationResult deleteAllFiles(java.io.File aDirectory)
Delete all files in the specified directory and its subdirectories. Answer a status value which specifies the number of files that were removed and the exceptions from any problems which were encountered.


deleteDirectory

public FileSystemOperationResult deleteDirectory(java.io.File aDirectory)
Delete the contents of the specified directory and all its subdirectories. Answer a status value which specifies the number of files and directories that were removed and the exceptions from any problems which were encountered.


deleteEmptyDirectories

public FileSystemOperationResult deleteEmptyDirectories(java.io.File aDirectory)
Delete all empty subdirectories under the given directory, along with the root directory itself if empty. Answer a status value which specifies the number of files and directories that were removed and the exceptions from any problems which were encountered.


deleteEmptySubdirectories

public FileSystemOperationResult deleteEmptySubdirectories(java.io.File aDirectory)
Delete all empty subdirectories under the given directory. Answer a status value which specifies the number of files and directories that were removed and the exceptions from any problems which were encountered.


deleteFiles

public FileSystemOperationResult deleteFiles(java.io.File aDirectory)
Delete all files in the specified directory. Answer a status value which specifies the number of files that were removed and the exceptions from any problems which were encountered.


getDirectorySize

public int getDirectorySize(java.io.File aDirectory)
Answer the number of file system elements (files and directories) which are directly contained in the given directory.


getFileContents

public java.lang.String getFileContents(java.io.File aFile)
                                 throws java.io.FileNotFoundException,
                                        java.io.IOException
Answer a string containing the contents of the specified file.

java.io.FileNotFoundException
java.io.IOException

getFileContents

public java.lang.String getFileContents(java.lang.String pathname)
                                 throws java.io.FileNotFoundException,
                                        java.io.IOException
Answer a string containing the contents of the specified file.

java.io.FileNotFoundException
java.io.IOException

getFileContentsAsCharArray

public char[] getFileContentsAsCharArray(java.io.File aFile)
                                  throws java.io.FileNotFoundException,
                                         java.io.IOException
Answer a character array containing the contents of the specified file.

java.io.FileNotFoundException
java.io.IOException

isRealDirectory

public boolean isRealDirectory(java.io.File fileSystemElement)
Answer whether the given file system element is a directory which actually exists.

Returns false if the path refers to a directory which has not yet been realized in the file system or if the path is a reference to a file.


realizeDirectory

public java.io.File realizeDirectory(java.io.File aDirectory,
                                     java.lang.String relativePathName)
                              throws java.io.IOException
Ensure that the specified directory exists. Answer the referenced directory.

Creates the directory and any containing directory structure if necessary.

Throws:
java.io.IOException - If the named directory and all necessary parent directories could not be created

realizeDirectory

public java.io.File realizeDirectory(java.io.File aDirectory)
                              throws java.io.IOException
Ensure that the specified directory exists. Answer the referenced directory.

Creates the directory and any containing directory structure if necessary.

Throws:
java.io.IOException - If the named directory and all necessary parent directories could not be created

setCopyBufferSize

public void setCopyBufferSize(int nBytes)
Specify the size in bytes of the transfer buffer used by the copyFile operation.


GFL 1.1 API