public class FileHelper
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.io.File |
HOME_DIR
Stores the current users home directory
|
static java.lang.String |
LS
A Line separator property value
|
static int |
MAX_RETRIES
Number of retries to retry fetching of URL's
|
static long |
RETRY_SLEEP_TIME
Time to sleep between retries
|
Constructor and Description |
---|
FileHelper() |
Modifier and Type | Method and Description |
---|---|
static void |
appendContentsToFile(java.io.File file,
java.lang.StringBuilder contents)
Used to add contents to a file
|
static void |
copy(java.io.File src,
java.io.File dst)
Used to copy a source file or a directory to a destination file or directory.
|
static void |
copy(java.io.InputStream in,
java.io.File dst)
Used to copy the contents of a input stream to a destination file.
|
static void |
copy(java.io.InputStream in,
java.io.File dst,
java.util.Map<java.lang.String,java.lang.String> params)
Used to copy the contents of a input stream to a destination file.
|
static java.lang.String |
copy(java.net.URL url,
java.io.File dest)
This will copy a file from the web to a destination file on the local system
|
static java.io.File |
createTempFile(java.lang.String name,
java.lang.String ext)
Used a temporary file that will be deleted when the JVM exits
|
static java.io.File |
createTmpDir(java.lang.String name)
This will create a temporary directory using the given name.
|
static java.io.File |
createTmpFileWithContents(java.lang.StringBuilder testConfig)
Used to create a temporary file with the give contents
|
static void |
delete(java.io.File file)
Used to delete a file or a directory tree.
|
static void |
displayFile(java.io.File file,
int startLine,
int endLine,
java.io.OutputStream os)
Used to display the contents of a file
|
static void |
displayFile(java.io.File file,
java.io.PrintStream os)
Used to display the contents of a file
|
static java.lang.String |
getExtension(java.io.File file)
Used to get the extension of the file
|
static Stream |
getInputStream(java.net.URL url)
Used to get a stream to a URL.
|
static java.lang.String |
getMD5Checksum(java.io.File file)
Used to generate a MD5 checksum string for a file
|
static java.lang.String |
getName(java.io.File file)
Used to get the name of the file
|
static java.io.File |
getWorkingDirectory()
Used to get the current working directory
|
static java.util.List<java.io.File> |
listDirectories(java.io.File dir)
Used to list all the directories in a directory and it's sub directories.
|
static java.util.List<java.io.File> |
listFiles(java.io.File dir)
Used to list all the files in a directory and it's sub directories.
|
static java.util.List<java.lang.String> |
listFilesAsStrings(java.io.File dir)
Used to list all the files in a directory and it's sub directiories.
|
static void |
move(java.io.File from,
java.io.File to)
Used to move a directory or file from once location to another
|
static java.lang.String |
readFileContents(java.io.File file)
Used to read the contents of a file into a string
|
static java.lang.String |
readFileContents(java.io.InputStream inputStream)
Used to read the contents of a stream into a string
|
static void |
rename(java.io.File oldFile,
java.io.File newFile)
Used to rename a file.
|
static java.io.File |
resolveRelativePaths(java.io.File path)
Used to convert relative paths to absolute paths.
|
static void |
unzip(java.io.InputStream is,
java.io.File destDir)
Used to unzip a file to a directory
|
public static final java.lang.String LS
public static final java.io.File HOME_DIR
public static final long RETRY_SLEEP_TIME
public static final int MAX_RETRIES
public static java.io.File createTmpDir(java.lang.String name) throws java.io.IOException
name
- The name of the directory to createjava.io.IOException
- Thrown if their is a problme creating the directorypublic static void copy(java.io.File src, java.io.File dst) throws java.io.IOException
src
- The source file or directorydst
- The destination file or directoryjava.io.IOException
- Thrown if their is a problem copying the file or directorypublic static void copy(java.io.InputStream in, java.io.File dst) throws java.io.IOException
in
- The input streamdst
- The destination filejava.io.IOException
- Thrown if their is a problem copying the filepublic static void copy(java.io.InputStream in, java.io.File dst, java.util.Map<java.lang.String,java.lang.String> params) throws java.io.IOException
in
- The input streamdst
- The destination fileparams
- Parameters which are replaced with values when the file is copiedjava.io.IOException
- Thrown if their is a problem copying the filepublic static java.lang.String copy(java.net.URL url, java.io.File dest) throws java.io.IOException
url
- The url to read from the file fromdest
- The file to be created on the location systemjava.io.IOException
- Thrown if their is a problem reading or wring the filepublic static java.lang.String getMD5Checksum(java.io.File file) throws java.io.IOException
file
- The filejava.io.IOException
- Thrown if their are any problemspublic static void displayFile(java.io.File file, java.io.PrintStream os) throws java.io.IOException
file
- The file to displayos
- The output stream to display it tojava.io.IOException
- Thrown if their is a problem reading or displaying the filepublic static void displayFile(java.io.File file, int startLine, int endLine, java.io.OutputStream os) throws java.io.IOException
file
- The file to displaystartLine
- The line to start displaying fromendLine
- The line to finish displaying fromos
- The output stream used to print the file tojava.io.IOException
- Thrown if their is a problem reading the filepublic static java.lang.String readFileContents(java.io.File file) throws java.io.IOException
file
- The file to readjava.io.IOException
- Thrown if their is a problem reading the filepublic static java.lang.String readFileContents(java.io.InputStream inputStream) throws java.io.IOException
inputStream
- The input streamjava.io.IOException
- Thrown if their is a problem reading the filepublic static java.util.List<java.io.File> listDirectories(java.io.File dir)
dir
- The directory to list the files ofpublic static java.util.List<java.io.File> listFiles(java.io.File dir)
dir
- The directory to list the files ofpublic static java.util.List<java.lang.String> listFilesAsStrings(java.io.File dir)
dir
- The directory to list the files ofpublic static void appendContentsToFile(java.io.File file, java.lang.StringBuilder contents) throws java.io.IOException
file
- The file to add contetns tocontents
- The contentsjava.io.IOException
- Thrown if their is a IO problempublic static void unzip(java.io.InputStream is, java.io.File destDir) throws java.io.IOException
is
- The input stream containing the filedestDir
- The directory to unzip tojava.io.IOException
- Thrown if their are any problemspublic static void move(java.io.File from, java.io.File to) throws java.io.IOException
from
- The old name of the file or directoryto
- The new name of the file or directoryjava.io.IOException
- Thrown if their are any problemspublic static void delete(java.io.File file) throws java.io.IOException
file
- The file or directory to deletejava.io.IOException
- Thrown if their are any problemspublic static java.io.File createTmpFileWithContents(java.lang.StringBuilder testConfig) throws java.io.IOException
testConfig
- The contents to put in the filejava.io.IOException
- Thrown if their are any problemspublic static Stream getInputStream(java.net.URL url) throws java.io.IOException
url
- The URL of the streamjava.io.IOException
- Thrown if their are any problemspublic static java.io.File createTempFile(java.lang.String name, java.lang.String ext) throws java.io.IOException
name
- name of fileext
- extension of the filejava.io.IOException
- Thrown if their is a problem creating the filepublic static java.lang.String getExtension(java.io.File file)
file
- The filepublic static java.lang.String getName(java.io.File file)
file
- The filepublic static java.io.File getWorkingDirectory()
public static java.io.File resolveRelativePaths(java.io.File path)
path
- The relative pathpublic static void rename(java.io.File oldFile, java.io.File newFile) throws java.io.IOException
File.renameTo(File)
fails. It that
occurs it copies the file and deletes the originaloldFile
- The old file namenewFile
- The new file namejava.io.IOException
- Thrown if their are any problems