Package org.jppf.utils
Class FileUtils
java.lang.Object
org.jppf.utils.FileUtils
This class provides a set of utility methods for reading, writing and manipulating files.
- Author:
- Laurent Cohen
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleandeletePath(File path) Delete the specified path, recursively if this is a directory.static booleandeletePath(File path, boolean childrenOnly) Delete the specified path, recursively if this is a directory.static BufferedReadergetBufferedReader(String path) Get aReaderfor the specified file path, looked up first in the file system then in the classpath.static StringTransform a file path into a URL in non URL-ncoded form.static byte[]getFileAsByte(File file) Get the content of a file as an array of bytes.static StringgetFileExtension(File file) Get the extension of a file.static InputStreamgetFileInputStream(File path) Get an input stream given a file.static InputStreamgetFileInputStream(String path) Get an input stream given a file path.static StringgetFileName(String filePath) Get the name of a file from its full path.getFileNameAndExtension(String imageName) Decompose a file name into name + extension.static OutputStreamgetFileOutputStream(File file) Get an output stream given a file path.static OutputStreamgetFileOutputStream(String path) Get an output stream given a file path.getFilePathList(String fileListPath) Get a list of files whose paths are found in a text file.static ReadergetFileReader(String path) Get aReaderfor the specified file path, looked up first in the file system then in the classpath.static WritergetFileWriter(String path) Get aWriterfor the specified file path.static StringgetFirstExistingFilePath(String... paths) Get the first path in the provided array which exists in the file system.static Filestatic byte[]getPathAsByte(String path) Get the content of a file or resource on the classpath as an array of bytes.static URLgetURLFromFilePath(File path) Transform a file path into a URL.static URLgetURLFromFilePath(String path) Transform a file path into a URL.static voidCreate the folders of the specified path, if they do not all already esist.static StringreadTextFile(File file) Read the content of a specified file into a string.static StringreadTextFile(Reader aReader) Read the content of a specified reader into a string.static StringreadTextFile(String filename) Read the content of a specified file into a string.static voidsplitTextFile(String file, int splitSize) Split a file into multiple files whose size is as close as possible to the specified split size.textFileAsLines(Reader aReader) Read the content of a specified reader into a string.static File[]Convert a set of file names into a set ofFileobjects.static URL[]Convert a set of file paths into a set of URLs.static voidwriteBytesToFile(byte[] data, File path) Write a byte array into an file.static voidwriteTextFile(File file, String content) Write the content of a string into a specified file.static voidwriteTextFile(Writer dest, String content) Write the content of a string into a specified file.static voidwriteTextFile(String filename, String content) Write the content of a string into a specified file.
-
Method Details
-
readTextFile
Read the content of a specified reader into a string.- Parameters:
aReader- the reader to read the content from.- Returns:
- the content of the file as a string.
- Throws:
IOException- if the file can't be found or read.
-
textFileAsLines
Read the content of a specified reader into a string. This method closes the reader upon terminating.- Parameters:
aReader- the reader to read the content from.- Returns:
- the content of the file as a string.
- Throws:
IOException- if the file can't be found or read.
-
readTextFile
Read the content of a specified file into a string.- Parameters:
file- the file to read.- Returns:
- the content of the file as a string.
- Throws:
IOException- if the file can't be found or read.
-
readTextFile
Read the content of a specified file into a string.- Parameters:
filename- the location of the file to read.- Returns:
- the content of the file as a string.
- Throws:
IOException- if the file can't be found or read.
-
writeTextFile
Write the content of a string into a specified file.- Parameters:
filename- the location of the file to write to.content- the content to write into the file.- Throws:
IOException- if the file can't be found or read.
-
writeTextFile
Write the content of a string into a specified file.- Parameters:
file- the location of the file to write to.content- the content to write into the file.- Throws:
IOException- if the file can't be found or read.
-
writeTextFile
Write the content of a string into a specified file.- Parameters:
dest- the file to write to.content- the content to write into the file.- Throws:
IOException- if the file can't be found or read.
-
getFileInputStream
Get an input stream given a file.- Parameters:
path- the file to lookup.- Returns:
- an
InputStreaminstance, or null if the file could not be created. - Throws:
IOException- if an I/O error occurs.
-
getFileInputStream
Get an input stream given a file path. This method first looks up in the file system for the specified path, then in the classpath.- Parameters:
path- the path to the file to lookup.- Returns:
- a
InputStreaminstance, or null if the file could not be found. - Throws:
IOException- if an IO error occurs while looking up the file.
-
getFileOutputStream
Get an output stream given a file path.- Parameters:
path- the path to the file to lookup.- Returns:
- an
OutputStreaminstance, or null if the file could not be created. - Throws:
IOException- if an IO error occurs while looking up the file.
-
getFileOutputStream
Get an output stream given a file path.- Parameters:
file- the file to lookup.- Returns:
- an
OutputStreaminstance, or null if the file could not be created. - Throws:
IOException- if an IO error occurs while looking up the file.
-
getFileReader
Get aReaderfor the specified file path, looked up first in the file system then in the classpath.- Parameters:
path- the path to the file to lookup.- Returns:
- a
Readerinstance, or null if the file could not be found. - Throws:
IOException- if an IO error occurs while looking up the file.
-
getFileWriter
Get aWriterfor the specified file path.- Parameters:
path- the path to the file to create or open.- Returns:
- a
Writerinstance. - Throws:
IOException- if an IO error occurs while creating the file writer.
-
getFilePathList
Get a list of files whose paths are found in a text file.- Parameters:
fileListPath- the path to the file that holds the list of documents to validate.- Returns:
- the file paths as a lst of strings.
- Throws:
IOException- if an error occurs while looking up or reading the file.
-
getFileExtension
Get the extension of a file.- Parameters:
file- the file from which to get the extension.- Returns:
- the file extension, or null if it si not a file or does not have an extension.
-
getFileName
Get the name of a file from its full path.- Parameters:
filePath- the file from which to get the file name.- Returns:
- the file name without path information.
-
splitTextFile
Split a file into multiple files whose size is as close as possible to the specified split size.- Parameters:
file- the text file to split.splitSize- the maximum number of lines of each resulting file.- Throws:
IOException- if an IO error occurs.
-
getPathAsByte
Get the content of a file or resource on the classpath as an array of bytes.- Parameters:
path- the path of the file to read from as a string.- Returns:
- a byte array with the file content.
- Throws:
IOException- if an IO error occurs.
-
getFileAsByte
Get the content of a file as an array of bytes.- Parameters:
file- the abstract path of the file to read from.- Returns:
- a byte array with the file content.
- Throws:
IOException- if an IO error occurs.
-
toFiles
Convert a set of file names into a set ofFileobjects.- Parameters:
dir- the directory in which the files are locatednames- the name part of each file (not the full path)- Returns:
- an array of
Fileobjects.
-
toURLs
Convert a set of file paths into a set of URLs.- Parameters:
files- the files whose path is to be converted to a URL.- Returns:
- an array of
URLobjects.
-
writeBytesToFile
Write a byte array into an file.- Parameters:
data- the byte array to write.path- the path to the file to write to.- Throws:
IOException- if an I/O error occurs.
-
deletePath
Delete the specified path, recursively if this is a directory.- Parameters:
path- the path to delete.- Returns:
- true if the folder and all contained files and subfolders were deleted, false otherwise.
-
deletePath
Delete the specified path, recursively if this is a directory.- Parameters:
path- the path to delete.childrenOnly- whentrueonly delete the children of the specified path, if it is a directory.- Returns:
- true if the folder and all contained files and subfolders were deleted, false otherwise.
-
mkdirs
Create the folders of the specified path, if they do not all already esist.- Parameters:
file- the path for which to create the folders. If it is a file, then folders for its parent path are created.- Throws:
IOException- if the folders could not be created.
-
getURLFromFilePath
Transform a file path into a URL.- Parameters:
path- the path to transform.- Returns:
- the path expressed as a URL.
-
getURLFromFilePath
Transform a file path into a URL.- Parameters:
path- the path to transform.- Returns:
- the path expressed as a URL.
-
getDecodedURLFromFilePath
Transform a file path into a URL in non URL-ncoded form.- Parameters:
path- the path to transform.- Returns:
- the a string representing the decoded URL.
-
getJPPFTempDir
- Returns:
- the root folder for JPPF temporary files.
-
getFileNameAndExtension
Decompose a file name into name + extension.- Parameters:
imageName- the file name to convert.- Returns:
- a Pair of name, extension.
-
getBufferedReader
Get aReaderfor the specified file path, looked up first in the file system then in the classpath.- Parameters:
path- the path to the file to lookup.- Returns:
- a
Readerinstance, or null if the file could not be found. - Throws:
IOException- if an IO error occurs while looking up the file.
-
getFirstExistingFilePath
Get the first path in the provided array which exists in the file system.- Parameters:
paths- the array pf paths to check.- Returns:
- the first existing path, or
nullif none exists.
-