com.sun.faban.harness.util
Class FileHelper

java.lang.Object
  extended by com.sun.faban.harness.util.FileHelper

public class FileHelper
extends Object

File utilities. A collection of static methods to deal with file operations.


Constructor Summary
FileHelper()
           
 
Method Summary
static boolean copyFile(String srcFile, String destFile, boolean append)
          Copies a file.
static boolean delete(File dir, FileFilter filter)
          Deletes all files matched by the filter in a certain directory.
static boolean editPropFile(String fileName, Properties prop, String backupFileName)
          This method opens, traverses through the file and finds the properties and replaces the values This updates only the first occurrence of the prop in the file to eliminate cases where it changes props defined like PROP=$PROP:MYPROP We expect only one prop per line in the file.
static byte[] getContent(File file)
          Obtains the whole content of a local file in a byte array.
static byte[] getContent(String file)
          Obtains the whole content of a local file in a byte array.
static boolean hasString(File file, String string)
          Checks a file whether it contains the given string.
static boolean isInFile(String fileName, String token)
          This method opens, traverses through the file and finds the token, it will avoid comments when searching.
static void jar(String dir, String[] fileNames, String jarPath)
          Jars up a directory to a given Jar file.
static void main(String[] args)
          Unit test the functionality.
static String[] readArrayContentFromFile(File file)
          Obtains the content of a file as a strin array.
static String readContentFromFile(File file)
          Reads a whole file and obtains the contents as a formatted string with "\n" seperated.
static String readStringFromFile(File file)
          Reads a whole file and obtains the contents as a string.
static boolean recursiveCopy(File src, File dest)
          Copies a file from source to dest.
static boolean recursiveDelete(File file)
          This method is used to delete a directory and recursively delete files and subdirectories within it.
static boolean recursiveDelete(File parentDir, String name)
          This method is used to delete a directory and recursively delete files and subdirectories within it.
static boolean tokenReplace(String fileName, String token, String replacement, String backupFileName)
          This method opens, traverses through the file and finds the token and replaces it with new value This method updates only the first occurrence of the token in the file to eliminate cases where it changes props defined like PROP=$PROP:MYPROP.
static void unjar(String jarPath, String outputDir)
          Unjars a jar file into an output directory.
static File unjarTmp(File tmpJarFile)
          Unjars a temporary jar file xxxx.jar under the directory xxxx in the same path.
static void writeContentToFile(String string, File file)
          Writes the entire content to file.
static void writeStringToFile(String string, File file)
          Writes a string to a file.
static boolean xferFile(String inFile, String outFile, boolean move)
          Deprecated.  
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileHelper

public FileHelper()
Method Detail

copyFile

public static boolean copyFile(String srcFile,
                               String destFile,
                               boolean append)
Copies a file.

Parameters:
srcFile - - the full pathname of the source file
destFile - - the full pathname of the destination file
append - - should destination file be appended with source file
Returns:
Whether the copy succeeded

editPropFile

public static boolean editPropFile(String fileName,
                                   Properties prop,
                                   String backupFileName)
This method opens, traverses through the file and finds the properties and replaces the values This updates only the first occurrence of the prop in the file to eliminate cases where it changes props defined like PROP=$PROP:MYPROP We expect only one prop per line in the file.

Parameters:
fileName - The full pathname of the file
prop - Property names and their new values
backupFileName - The file to save a copy before edit, if any
Returns:
Whether the edit succeeded

tokenReplace

public static boolean tokenReplace(String fileName,
                                   String token,
                                   String replacement,
                                   String backupFileName)
This method opens, traverses through the file and finds the token and replaces it with new value This method updates only the first occurrence of the token in the file to eliminate cases where it changes props defined like PROP=$PROP:MYPROP.

Parameters:
fileName - The full pathname of the file
token - Token to find
replacement - The replacement string
backupFileName - If needed pass a backup file name
Returns:
Whether there is at least one replacement

isInFile

public static boolean isInFile(String fileName,
                               String token)
This method opens, traverses through the file and finds the token, it will avoid comments when searching.

Parameters:
fileName - The full pathname of the file
token - Token to search for
Returns:
true if found, false otherwise

recursiveDelete

public static boolean recursiveDelete(File file)
This method is used to delete a directory and recursively delete files and subdirectories within it.

Parameters:
file - The file or directory to delete
Returns:
Whether the delete succeeded

delete

public static boolean delete(File dir,
                             FileFilter filter)
Deletes all files matched by the filter in a certain directory.

Parameters:
dir - The directory to look for files to delete
filter - The file name filter
Returns:
True if all deletes succeeded, false otherwise

recursiveDelete

public static boolean recursiveDelete(File parentDir,
                                      String name)
This method is used to delete a directory and recursively delete files and subdirectories within it.

Parameters:
parentDir - The file object corresponding to the parent directory
name - Name of the directory to be deleted
Returns:
Whether the delete succeeded

recursiveCopy

public static boolean recursiveCopy(File src,
                                    File dest)
Copies a file from source to dest. If src is a directory, the whole directory tree is copied.

Parameters:
src - The source file
dest - The dest file, must not exist before calling method
Returns:
true if copy succeeded, false afterwise

jar

public static void jar(String dir,
                       String[] fileNames,
                       String jarPath)
                throws IOException
Jars up a directory to a given Jar file.

Parameters:
dir - The base directory to jar (not included in output)
fileNames - The file names to jar, can be multiple
jarPath - The pathname of the jar file
Throws:
IOException - There is a problem jarring up

unjar

public static void unjar(String jarPath,
                         String outputDir)
                  throws IOException
Unjars a jar file into an output directory.

Parameters:
jarPath - The path to the jar file
outputDir - The output directory
Throws:
IOException - If there is an error running unjar

unjarTmp

public static File unjarTmp(File tmpJarFile)
                     throws IOException
Unjars a temporary jar file xxxx.jar under the directory xxxx in the same path.

Parameters:
tmpJarFile - The temporary jar file
Returns:
The file reference to the resulting directory
Throws:
IOException - If there is an error unjaring

writeStringToFile

public static void writeStringToFile(String string,
                                     File file)
                              throws IOException
Writes a string to a file. Replaces the file if it already exists.

Parameters:
string - The string to be written
file - The target file
Throws:
IOException - If the write fails

readStringFromFile

public static String readStringFromFile(File file)
                                 throws IOException
Reads a whole file and obtains the contents as a string.

Parameters:
file - The file to be read
Returns:
The string representing the whole content of the file
Throws:
IOException - If the read fails

writeContentToFile

public static void writeContentToFile(String string,
                                      File file)
                               throws IOException
Writes the entire content to file. Replaces the file if it already exists.

Parameters:
string -
file -
Throws:
IOException

readContentFromFile

public static String readContentFromFile(File file)
                                  throws IOException
Reads a whole file and obtains the contents as a formatted string with "\n" seperated.

Parameters:
file -
Returns:
string
Throws:
IOException

readArrayContentFromFile

public static String[] readArrayContentFromFile(File file)
                                         throws IOException
Obtains the content of a file as a strin array.

Parameters:
file -
Returns:
string array
Throws:
IOException

xferFile

@Deprecated
public static boolean xferFile(String inFile,
                                          String outFile,
                                          boolean move)
Deprecated. 

Transfers a file from the current host to the Faban master.

Parameters:
inFile - The input file name on the current host
outFile - The output file name on the Faban master
move - Whether to remove the original file or not
Returns:
True if the transfer is complete, false otherwise.

getContent

public static byte[] getContent(String file)
                         throws IOException
Obtains the whole content of a local file in a byte array.

Parameters:
file - The file name
Returns:
The byte content of the file
Throws:
IOException - If the file cannot be read.

getContent

public static byte[] getContent(File file)
                         throws IOException
Obtains the whole content of a local file in a byte array.

Parameters:
file - The file name
Returns:
The byte content of the file
Throws:
IOException - If the file cannot be read.

hasString

public static boolean hasString(File file,
                                String string)
                         throws IOException
Checks a file whether it contains the given string.

Parameters:
file - The file
string - The string
Returns:
True if the string is found in the file, false otherwise
Throws:
IOException - Problem reading the file

main

public static void main(String[] args)
Unit test the functionality.

Parameters:
args - The command line arguments