public class FileHelper extends Object
Constructor and Description |
---|
FileHelper() |
Modifier and Type | Method and Description |
---|---|
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.
|
public static boolean copyFile(String srcFile, String destFile, boolean append)
srcFile
- - the full pathname of the source filedestFile
- - the full pathname of the destination fileappend
- - should destination file be appended with source filepublic static boolean editPropFile(String fileName, Properties prop, String backupFileName)
fileName
- The full pathname of the fileprop
- Property names and their new valuesbackupFileName
- The file to save a copy before edit, if anypublic static boolean tokenReplace(String fileName, String token, String replacement, String backupFileName)
fileName
- The full pathname of the filetoken
- Token to findreplacement
- The replacement stringbackupFileName
- If needed pass a backup file namepublic static boolean isInFile(String fileName, String token)
fileName
- The full pathname of the filetoken
- Token to search forpublic static boolean recursiveDelete(File file)
file
- The file or directory to deletepublic static boolean delete(File dir, FileFilter filter)
dir
- The directory to look for files to deletefilter
- The file name filterpublic static boolean recursiveDelete(File parentDir, String name)
parentDir
- The file object corresponding to the parent directoryname
- Name of the directory to be deletedpublic static boolean recursiveCopy(File src, File dest)
src
- The source filedest
- The dest file, must not exist before calling methodpublic static void jar(String dir, String[] fileNames, String jarPath) throws IOException
dir
- The base directory to jar (not included in output)fileNames
- The file names to jar, can be multiplejarPath
- The pathname of the jar fileIOException
- There is a problem jarring uppublic static void unjar(String jarPath, String outputDir) throws IOException
jarPath
- The path to the jar fileoutputDir
- The output directoryIOException
- If there is an error running unjarpublic static File unjarTmp(File tmpJarFile) throws IOException
tmpJarFile
- The temporary jar fileIOException
- If there is an error unjaringpublic static void writeStringToFile(String string, File file) throws IOException
string
- The string to be writtenfile
- The target fileIOException
- If the write failspublic static String readStringFromFile(File file) throws IOException
file
- The file to be readIOException
- If the read failspublic static void writeContentToFile(String string, File file) throws IOException
string
- file
- IOException
public static String readContentFromFile(File file) throws IOException
file
- IOException
public static String[] readArrayContentFromFile(File file) throws IOException
file
- IOException
@Deprecated public static boolean xferFile(String inFile, String outFile, boolean move)
inFile
- The input file name on the current hostoutFile
- The output file name on the Faban mastermove
- Whether to remove the original file or notpublic static byte[] getContent(String file) throws IOException
file
- The file nameIOException
- If the file cannot be read.public static byte[] getContent(File file) throws IOException
file
- The file nameIOException
- If the file cannot be read.public static boolean hasString(File file, String string) throws IOException
file
- The filestring
- The stringIOException
- Problem reading the filepublic static void main(String[] args)
args
- The command line arguments