Package org.apache.uima.util
Class FileUtils
java.lang.Object
org.apache.uima.util.FileUtils
Some utilities for handling files.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic final void
Deprecated.static final File
createTempDir
(File parent, String prefix) Deprecated.use Java 7 methods for this seeFiles.createTempDirectory(Path, String, FileAttribute...)
static final File
createTempFile
(String prefix, String suffix, File tempDir) Deprecated.use Java 7 methods for this seeFile.createTempFile(String, String, File)
static final void
deleteAllFiles
(File directory) Delete all files in a directory (not recursive).static final boolean
deleteRecursive
(File file) Recursively delete possibly non-empty directory or file.static String
file2String
(File file) Read the contents of a file into a string, using the default platform encoding.static String
file2String
(File file, String fileEncoding) Read the contents of a file into a string using a specific character encoding.static String
findRelativePath
(File file, File relativeToDir) Finds a relative path to a given file, relative to a specified directory.Get a list of all files in a directory, ignoring subdirectories.Get a list of all files in a directory.static String[]
getPathComponents
(String canonicalPath) Splits a path into components using the OS file separator character.getSubDirs
(File directory) Get a list of all subdirectories in a directory, ignoring regular files.static final boolean
Create a new directory.static String
reader2String
(Reader reader) Read a bufferedReader into a string, using the default platform encoding.static void
saveString2File
(String fileContents, File file) Write a string to a file, using platform encoding.static void
saveString2File
(String s, File file, String encoding) Write a string to a file using the specified encoding.static void
writeToFile
(Path path, String data) Efficiently Writes string data as UTF-8 characters to path added for backwards compatibility with v2
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
getFiles
Get a list of all files in a directory. Optionally, get files in subdirectories as well.- Parameters:
directory
- The directory for which to get the files.getRecursive
- Should we get files from subdirectories?- Returns:
- ArrayList A list of
File
objects.null
ifdirectory
does not exist, or is not a directory.
-
getFiles
Get a list of all files in a directory, ignoring subdirectories.- Parameters:
directory
- The directory for which to get the files.- Returns:
- ArrayList A list of
File
objects.null
ifdirectory
does not exist, or is not a directory.
-
getSubDirs
Get a list of all subdirectories in a directory, ignoring regular files.- Parameters:
directory
- The directory for which to get the subdirectories.- Returns:
- ArrayList A list of
File
objects.null
ifdirectory
does not exist, or is not a directory.
-
reader2String
Read a bufferedReader into a string, using the default platform encoding.- Parameters:
reader
- to be read in- Returns:
- String The contents of the stream.
- Throws:
IOException
- Various I/O errors.
-
file2String
Read the contents of a file into a string, using the default platform encoding.- Parameters:
file
- The file to be read in.- Returns:
- String The contents of the file.
- Throws:
IOException
- Various I/O errors.
-
file2String
Read the contents of a file into a string using a specific character encoding.- Parameters:
file
- The input file.fileEncoding
- The character encoding of the file (see your Java documentation for supported encodings).- Returns:
- String The contents of the file.
- Throws:
IOException
- Various I/O errors.
-
saveString2File
Write a string to a file, using platform encoding. If the file exists, it is overwritten.- Parameters:
fileContents
- The file contents.file
- The file to save to.- Throws:
IOException
- If for any reason the file can't be written.
-
saveString2File
Write a string to a file using the specified encoding. If the file exists, it is overwritten.- Parameters:
s
- The file contents.file
- The file to save to.encoding
- The character encoding to use.- Throws:
IOException
- If for any reason the file can't be written.
-
writeToFile
Efficiently Writes string data as UTF-8 characters to path added for backwards compatibility with v2- Parameters:
path
- where to write to, creating a new file if it doesn't already existdata
- the data to write
-
deleteAllFiles
Delete all files in a directory (not recursive).- Parameters:
directory
- The directory that contains the files to be deleted.
-
deleteRecursive
Recursively delete possibly non-empty directory or file.- Parameters:
file
- The file or directory to be deleted.- Returns:
true
iff the file/directory could be deleted.
-
mkdir
Create a new directory. The parent directory must exist.- Parameters:
directory
- The directory to be created.- Returns:
- boolean Will fail if directory already exists, or File.mkdir() returns
false
.
-
createTempDir
Deprecated.use Java 7 methods for this seeFiles.createTempDirectory(Path, String, FileAttribute...)
Create a temporary directory using random numbers as name suffixes.- Parameters:
parent
- Where the directory is created.prefix
- Prefix of the directory names to be created.- Returns:
- A file object corresponding to the newly created dir, or
null
if none could be created for some reason (e.g., if the parent is not writable).
-
createTempFile
@Deprecated public static final File createTempFile(String prefix, String suffix, File tempDir) throws IOException Deprecated.use Java 7 methods for this seeFile.createTempFile(String, String, File)
- Parameters:
prefix
- -suffix
- -tempDir
- -- Returns:
- the file
- Throws:
IOException
- -
-
copyFile
Deprecated.use Java 7 for this seeFiles.copy(Path, Path, CopyOption...)
Copy filefile
to locationdir
. This method will not fail silently. Anything that prevents the copy from happening will be treated as an error condition. If the method does not throw an exception, the copy was successful.Note: this is a completely brain-dead implementation of file copy. The complete file is read into memory before it is copied. If you need something more sophisticated for copying large files, feel free to add your improvements.
- Parameters:
file
- The file to copy.dir
- The destination directory.- Throws:
IOException
- For various reason: iffile
does not exist or is not readable, if the destination directory does not exist or isn't a directory, or if the file can't be copied for any reason.
-
findRelativePath
Finds a relative path to a given file, relative to a specified directory.- Parameters:
file
- file that the relative path should resolve torelativeToDir
- directory that the path should be relative to- Returns:
- a relative path. This always uses / as the separator character.
- Throws:
IOException
- -
-
getPathComponents
Splits a path into components using the OS file separator character. This can be used on the results of File.getCanonicalPath().- Parameters:
canonicalPath
- a file path that uses the OS file separator character- Returns:
- an array of strings, one for each component of the path
-
Files.copy(Path, Path, CopyOption...)