Package org.apache.uima.util
Class FileUtils
- java.lang.Object
-
- org.apache.uima.util.FileUtils
-
public class FileUtils extends java.lang.Object
Some utilities for handling files.
-
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
copyFile(java.io.File file, java.io.File dir)
Deprecated.use Java 7 for this seeFiles.copy(Path, Path, CopyOption...)
static java.io.File
createTempDir(java.io.File parent, java.lang.String prefix)
Deprecated.use Java 7 methods for this seeFiles.createTempDirectory(Path, String, FileAttribute...)
static java.io.File
createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File tempDir)
Deprecated.use Java 7 methods for this seeFile.createTempFile(String, String, File)
static void
deleteAllFiles(java.io.File directory)
Delete all files in a directory (not recursive).static boolean
deleteRecursive(java.io.File file)
Recursively delete possibly non-empty directory or file.static java.lang.String
file2String(java.io.File file)
Read the contents of a file into a string, using the default platform encoding.static java.lang.String
file2String(java.io.File file, java.lang.String fileEncoding)
Read the contents of a file into a string using a specific character encoding.static java.lang.String
findRelativePath(java.io.File file, java.io.File relativeToDir)
Finds a relative path to a given file, relative to a specified directory.static java.util.ArrayList<java.io.File>
getFiles(java.io.File directory)
Get a list of all files in a directory, ignoring subdirectories.static java.util.ArrayList<java.io.File>
getFiles(java.io.File directory, boolean getRecursive)
Get a list of all files in a directory.static java.lang.String[]
getPathComponents(java.lang.String canonicalPath)
Splits a path into components using the OS file separator character.static java.util.ArrayList<java.io.File>
getSubDirs(java.io.File directory)
Get a list of all subdirectories in a directory, ignoring regular files.static boolean
mkdir(java.io.File directory)
Create a new directory.static java.lang.String
reader2String(java.io.Reader reader)
Read a bufferedReader into a string, using the default platform encoding.static void
saveString2File(java.lang.String fileContents, java.io.File file)
Write a string to a file, using platform encoding.static void
saveString2File(java.lang.String s, java.io.File file, java.lang.String encoding)
Write a string to a file using the specified encoding.static void
writeToFile(java.nio.file.Path path, java.lang.String data)
Efficiently Writes string data as UTF-8 characters to path added for backwards compatibility with v2
-
-
-
Method Detail
-
getFiles
public static final java.util.ArrayList<java.io.File> getFiles(java.io.File directory, boolean getRecursive)
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
public static final java.util.ArrayList<java.io.File> getFiles(java.io.File directory)
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
public static final java.util.ArrayList<java.io.File> getSubDirs(java.io.File directory)
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
public static java.lang.String reader2String(java.io.Reader reader) throws java.io.IOException
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:
java.io.IOException
- Various I/O errors.
-
file2String
public static java.lang.String file2String(java.io.File file) throws java.io.IOException
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:
java.io.IOException
- Various I/O errors.
-
file2String
public static java.lang.String file2String(java.io.File file, java.lang.String fileEncoding) throws java.io.IOException
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:
java.io.IOException
- Various I/O errors.
-
saveString2File
public static void saveString2File(java.lang.String fileContents, java.io.File file) throws java.io.IOException
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:
java.io.IOException
- If for any reason the file can't be written.
-
saveString2File
public static void saveString2File(java.lang.String s, java.io.File file, java.lang.String encoding) throws java.io.IOException
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:
java.io.IOException
- If for any reason the file can't be written.
-
writeToFile
public static void writeToFile(java.nio.file.Path path, java.lang.String data)
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
public static final void deleteAllFiles(java.io.File directory)
Delete all files in a directory (not recursive).- Parameters:
directory
- The directory that contains the files to be deleted.
-
deleteRecursive
public static final boolean deleteRecursive(java.io.File file)
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
public static final boolean mkdir(java.io.File directory)
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 public static final java.io.File createTempDir(java.io.File parent, java.lang.String prefix)
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 java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File tempDir) throws java.io.IOException
Deprecated.use Java 7 methods for this seeFile.createTempFile(String, String, File)
- Parameters:
prefix
- -suffix
- -tempDir
- -- Returns:
- the file
- Throws:
java.io.IOException
- -
-
copyFile
@Deprecated public static final void copyFile(java.io.File file, java.io.File dir) throws java.io.IOException
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:
java.io.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
public static java.lang.String findRelativePath(java.io.File file, java.io.File relativeToDir) throws java.io.IOException
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:
java.io.IOException
- -
-
getPathComponents
public static java.lang.String[] getPathComponents(java.lang.String canonicalPath)
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
-
-