edu.iastate.utils.io
Class FileUtils

java.lang.Object
  extended byedu.iastate.utils.io.FileUtils

public abstract class FileUtils
extends java.lang.Object

Provides some useful methods when working with files.

Version:
$Revision: 1.1 $
Author:
Jose San Leandro Armendáriz

Constructor Summary
private FileUtils()
          Private constructor to avoid accidental instantiation.
 
Method Summary
static boolean appendFile(java.lang.String fullname, java.lang.String text)
           
static void copy(java.io.File original, java.io.File destination)
          Copies the contents of a file to another.
static void copy(java.lang.String filePath, java.lang.String destinationPath)
          Copies one file from its current path to another.
static boolean copyIfPossible(java.io.File original, java.io.File destination)
          Copies the contents of a file to another.
static boolean copyIfPossible(java.lang.String originalPath, java.lang.String destinationPath)
          Copies the contents of a file (referred by given path) to another.
static java.lang.String findExtension(java.lang.String fullname)
           
static java.lang.String findName(java.lang.String fullname)
          find the file name without path or extension
static byte[] getBytesFromFile(java.io.File file)
           
static boolean isFileExists(java.lang.String fullname)
           
static void move(java.io.File originalFile, java.io.File destinationFile)
          Moves a file.
static void move(java.lang.String filePath, java.lang.String destinationPath)
          Moves a file from one path to another, if possible.
static boolean moveIfPossible(java.io.File originalFile, java.io.File destinationFile)
          Moves a file, if possible.
static boolean moveIfPossible(java.lang.String filePath, java.lang.String destinationPath)
          Moves a file from one path to another, if possible.
static java.lang.String readFile(java.io.File file)
          Reads a file and returns its contents.
static java.lang.String readFile(java.lang.String filePath)
          Reads a file pointed by given path, and returns its contents.
static java.lang.String readFileIfPossible(java.io.File file)
          Reads a file and returns its contents, if possible.
static java.lang.String readFileIfPossible(java.lang.String filePath)
          Reads a file by its path and returns its contents, if possible.
static void writeFile(java.io.File file, java.lang.String contents)
          Writes a file with given contents.
static void writeFile(java.lang.String filePath, java.lang.String contents)
          Writes a file referred by given path, with given contents.
static boolean writeFileIfPossible(java.io.File file, java.lang.String contents)
          Saves the contents to a file.
static boolean writeFileIfPossible(java.lang.String filePath, java.lang.String contents)
          Saves the contents to a file.
static java.io.File writeToTempFile(java.lang.String prefix, java.lang.String suffix, java.lang.String contens, boolean deleteOnExit)
          Save to temporary file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtils

private FileUtils()
Private constructor to avoid accidental instantiation.

Method Detail

readFile

public static java.lang.String readFile(java.lang.String filePath)
                                 throws java.io.FileNotFoundException,
                                        java.lang.SecurityException,
                                        java.io.IOException
Reads a file pointed by given path, and returns its contents.

Parameters:
filePath - the path to the file to be read.
Returns:
the contents of the file.
Throws:
java.io.FileNotFoundException - if the file is not found.
java.lang.SecurityException - if the operation is forbidden because of security manager settings.
java.io.IOException - if some I/O exception occurs.

readFile

public static java.lang.String readFile(java.io.File file)
                                 throws java.io.FileNotFoundException,
                                        java.lang.SecurityException,
                                        java.io.IOException
Reads a file and returns its contents.

Parameters:
file - the file to be read.
Returns:
the contents of the file.
Throws:
java.io.FileNotFoundException - if the file is not found.
java.lang.SecurityException - if the operation is forbidden because of security manager settings.
java.io.IOException - if some I/O exception occurs.

readFileIfPossible

public static java.lang.String readFileIfPossible(java.lang.String filePath)
Reads a file by its path and returns its contents, if possible. If some exception occurs, it's ignored, and returns an empty String. This method is used to avoid declaring try/catch blocks in client code.

Parameters:
filePath - the path to the file to be read.
Returns:
the contents of the file, or empty if reading cannot be accomplished.

readFileIfPossible

public static java.lang.String readFileIfPossible(java.io.File file)
Reads a file and returns its contents, if possible. If some exception occurs, it's ignored, and returns an empty String. This method is used to avoid declaring try/catch blocks in client code.

Parameters:
file - the file to be read.
Returns:
the contents of the file, or empty if reading cannot be accomplished.

writeFileIfPossible

public static boolean writeFileIfPossible(java.lang.String filePath,
                                          java.lang.String contents)
Saves the contents to a file.

Parameters:
filePath - the path of the file.
contents - the text to save.
Returns:
true if the process has been successfully accomplished.

writeFileIfPossible

public static boolean writeFileIfPossible(java.io.File file,
                                          java.lang.String contents)
Saves the contents to a file.

Parameters:
file - the file to be overwritten.
contents - the text to save.
Returns:
true if the process has been successfully accomplished.

writeFile

public static void writeFile(java.lang.String filePath,
                             java.lang.String contents)
                      throws java.io.FileNotFoundException,
                             java.lang.SecurityException,
                             java.io.IOException
Writes a file referred by given path, with given contents.

Parameters:
filePath - the path of the file.
contents - the text to write.
Throws:
java.io.FileNotFoundException - if the file is not found.
java.lang.SecurityException - if the security manager forbids this operation.
java.io.IOException - if any other I/O error occurs.

writeFile

public static void writeFile(java.io.File file,
                             java.lang.String contents)
                      throws java.io.FileNotFoundException,
                             java.lang.SecurityException,
                             java.io.IOException
Writes a file with given contents.

Parameters:
file - the file to write.
contents - the text to write.
Throws:
java.io.FileNotFoundException - if the file is not found.
java.lang.SecurityException - if the security manager forbids this operation.
java.io.IOException - if any other I/O error occurs.

copy

public static void copy(java.lang.String filePath,
                        java.lang.String destinationPath)
                 throws java.io.FileNotFoundException,
                        java.lang.SecurityException,
                        java.io.IOException
Copies one file from its current path to another.

Parameters:
filePath - file's path.
destinationPath - the new path of the file.
Throws:
java.io.FileNotFoundException - if the file is not found.
java.lang.SecurityException - if the security manager forbids this operation.
java.io.IOException - if any other I/O error occurs.

copy

public static void copy(java.io.File original,
                        java.io.File destination)
                 throws java.io.FileNotFoundException,
                        java.lang.SecurityException,
                        java.io.IOException
Copies the contents of a file to another.

Parameters:
original - the content to copy.
destination - the file to be overwritten.
Throws:
java.io.FileNotFoundException - if the file is not found.
java.lang.SecurityException - if the security manager forbids this operation.
java.io.IOException - if any other I/O error occurs.

copyIfPossible

public static boolean copyIfPossible(java.lang.String originalPath,
                                     java.lang.String destinationPath)
Copies the contents of a file (referred by given path) to another.

Parameters:
originalPath - the path of the file to copy.
destinationPath - the path of the file to be overwritten.
Returns:
true if the operation ends up successfully.

copyIfPossible

public static boolean copyIfPossible(java.io.File original,
                                     java.io.File destination)
Copies the contents of a file to another.

Parameters:
original - the content to copy.
destination - the file to be overwritten.
Returns:
true if the operation ends up successfully.

move

public static void move(java.io.File originalFile,
                        java.io.File destinationFile)
                 throws java.io.FileNotFoundException,
                        java.lang.SecurityException,
                        java.io.IOException
Moves a file.

Parameters:
originalFile - the file to move.
destinationFile - the new file.
Throws:
java.io.FileNotFoundException - if the file is not found.
java.lang.SecurityException - if the security manager forbids this operation.
java.io.IOException - if any other I/O error occurs.

move

public static void move(java.lang.String filePath,
                        java.lang.String destinationPath)
                 throws java.io.FileNotFoundException,
                        java.lang.SecurityException,
                        java.io.IOException
Moves a file from one path to another, if possible.

Parameters:
filePath - the path of the file to move.
destinationPath - the new file's path.
Throws:
java.io.FileNotFoundException - if the file is not found.
java.lang.SecurityException - if the security manager forbids this operation.
java.io.IOException - if any other I/O error occurs.

moveIfPossible

public static boolean moveIfPossible(java.io.File originalFile,
                                     java.io.File destinationFile)
Moves a file, if possible.

Parameters:
originalFile - the file to move.
destinationFile - the new file.
Returns:
true if the operation ends up successfully.

moveIfPossible

public static boolean moveIfPossible(java.lang.String filePath,
                                     java.lang.String destinationPath)
Moves a file from one path to another, if possible.

Parameters:
filePath - the path of the file to move.
destinationPath - the new file's path.
Returns:
true if the operation ends up successfully.

findExtension

public static java.lang.String findExtension(java.lang.String fullname)
Returns:
String

findName

public static java.lang.String findName(java.lang.String fullname)
find the file name without path or extension

Parameters:
fullname - String
Returns:
String

appendFile

public static boolean appendFile(java.lang.String fullname,
                                 java.lang.String text)
Parameters:
fullname - String
text - String
Returns:
boolean append text to a file

isFileExists

public static boolean isFileExists(java.lang.String fullname)

getBytesFromFile

public static byte[] getBytesFromFile(java.io.File file)
                               throws java.io.IOException
Throws:
java.io.IOException

writeToTempFile

public static java.io.File writeToTempFile(java.lang.String prefix,
                                           java.lang.String suffix,
                                           java.lang.String contens,
                                           boolean deleteOnExit)
Save to temporary file

Parameters:
prefix - String - could be null
suffix - String - could be null
contens - String - to write
deleteOnExit - boolean - if delete the file on exit
Returns:
File
Since:
2005-03-09