Package org.rapidcontext.util
Class FileUtil
java.lang.Object
org.rapidcontext.util.FileUtil
A set of utility methods for handling files.
- Version:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic File
Attempts to resolve a file to a canonical file.static void
Copies a file or a directory.static void
copy
(InputStream is, File dst) Copies the data from an input stream to a destination file.static void
copy
(InputStream input, OutputStream output) Copies the data from an input stream to an output stream.static void
Deletes a file or a directory.static void
deleteEmptyDirs
(File dir) Deletes all empty directories in a directory, but leaves the directory itself unmodified.static void
deleteFiles
(File dir) Deletes all files in a directory, but leaving the directory otherwise unmodified.static String
Reads a file containing UTF-8 text content.static String
readText
(InputStream is) Reads an input stream containing UTF-8 text content.static File
Finds a child file in a directory by case-insensitive search.static void
setTempDir
(File dir) Sets the temporary directory to use.static File
Creates a new temporary file.
-
Method Details
-
canonical
Attempts to resolve a file to a canonical file.- Parameters:
file
- the file to resolve- Returns:
- the canonical file, or the original file if the resolution failed
-
resolve
Finds a child file in a directory by case-insensitive search. If no file is found, a new file object is created.- Parameters:
dir
- the parent directorychild
- the child file name- Returns:
- the corresponding file object
-
readText
Reads a file containing UTF-8 text content.- Parameters:
file
- the input file to read- Returns:
- the text content of the file
- Throws:
IOException
- if the file couldn't be read
-
readText
Reads an input stream containing UTF-8 text content.- Parameters:
is
- the input stream to read- Returns:
- the text content of the input stream
- Throws:
IOException
- if the input stream couldn't be read
-
copy
Copies a file or a directory. Directories are copied recursively and file modification dates are kept. If the executable bit is set on the source file, that bit will also be set on the destination file.- Parameters:
src
- the source file or directorydst
- the destination file or directory- Throws:
IOException
- if some file couldn't be copied
-
copy
Copies the data from an input stream to a destination file. If the file exists, it will be overwritten. The destination file parent directory must have been created or the copy will fail. The input stream will be closed by this function.- Parameters:
is
- the input stream to readdst
- the destination file- Throws:
IOException
- if the input stream couldn't be read or if the destination file couldn't be written
-
copy
Copies the data from an input stream to an output stream. Both the streams will be closed when this function returns.- Parameters:
input
- the input stream to readoutput
- the output stream to write- Throws:
IOException
- if the input stream couldn't be read or if the output stream couldn't be written
-
delete
Deletes a file or a directory. This function will delete all files and sub-directories inside a directory recursively.- Parameters:
file
- the file or directory to delete- Throws:
IOException
- if some files couldn't be deleted- See Also:
-
deleteFiles
Deletes all files in a directory, but leaving the directory otherwise unmodified. This function will delete any sub-directories recursively.- Parameters:
dir
- the directory to clean- Throws:
IOException
- if some files couldn't be deleted- See Also:
-
deleteEmptyDirs
Deletes all empty directories in a directory, but leaves the directory itself unmodified. This method will remove any empty directories recursively, making it possible to remove a tree of empty directories.- Parameters:
dir
- the directory to clean- Throws:
IOException
- if some files couldn't be deleted- See Also:
-
setTempDir
Sets the temporary directory to use.- Parameters:
dir
- the new temporary directory
-
tempFile
Creates a new temporary file. If a temporary directory has been set, it will be used. Otherwise the default temporary directory will be used. The generated file is guaranteed to keep the same file extension as the desired file name.- Parameters:
name
- the desired file name- Returns:
- a new empty temporary file
- Throws:
IOException
- if the temporary file couldn't be created
-