- All Known Implementing Classes:
FileUtils
-
Method Summary
Modifier and TypeMethodDescriptionvoidcheckFileIsWithinDirectory(String path, String directoryPath) This method is to provide assurance that the file specified by the path parameter is within the directory specified by directoryPath.voidA wrapper aroundFiles.delete(Path)voidDeletes a directory, deleting everything inside it recursively afterwards.booleandeleteIfExists(Path path) A wrapper aroundFiles.deleteIfExists(Path)booleanexists(Path path, LinkOption... options) A wrapper aroundFiles.exists(Path, LinkOption...)booleanisRegularFile(Path path, LinkOption... options) A wrapper aroundFiles.isRegularFile(Path, LinkOption...)A wrapper aroundFiles.lines(Path, Charset)A wrapper aroundFiles.list(Path)voidmakeDirectory(Path directory) Creates a directory if it doesn't already exist.voidmove(Path source, Path target, CopyOption... options) A wrapper aroundFiles.move(Path, Path, CopyOption...)newBufferedReader(Path path, Charset cs) A wrapper aroundFiles.newBufferedReader(Path, Charset)newBufferedWriter(Path path, Charset cs, OpenOption... options) A wrapper aroundFiles.newBufferedWriter(Path, Charset, OpenOption...)readAllLines(Path path) A wrapper aroundFiles.readAllLines(Path)byte[]readBinaryFile(String path) Read a binary file, return as a byte arrayreadString(Path path) A wrapper aroundFiles.readString(Path)readTextFile(String path) Read a text file from the given path, return as a string.safeResolve(String parentDirectory, String path) This helper method will ensure that the requested path is within the parent directory and using safe characterslongA wrapper aroundFiles.size(Path)walk(Path start, FileVisitOption... options) A wrapper aroundFiles.walk(Path, FileVisitOption...)write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options) A wrapper aroundFiles.write(Path, Iterable, Charset, OpenOption...)voidwriteString(Path path, String content, OpenOption... options) A wrapper aroundFiles.writeString(Path, CharSequence, OpenOption...)
-
Method Details
-
writeString
A wrapper aroundFiles.writeString(Path, CharSequence, OpenOption...)
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Throws:
UtilsException- if an empty path is providedIOException
-
write
Path write(Path path, Iterable<? extends CharSequence> lines, Charset cs, OpenOption... options) throws IOException A wrapper aroundFiles.write(Path, Iterable, Charset, OpenOption...)- Throws:
IOException
-
readString
A wrapper aroundFiles.readString(Path)- Returns:
- the value of the file at the path parameter, as a string, presuming UTF-8 encoding
- Throws:
UtilsException- if an empty path is providedIOException
-
deleteDirectoryRecursivelyIfExists
Deletes a directory, deleting everything inside it recursively afterwards. A more dangerous method than many others, take care.
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Throws:
IOException
-
makeDirectory
Creates a directory if it doesn't already exist.
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Throws:
IOException
-
readBinaryFile
Read a binary file, return as a byte array
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Throws:
IOException
-
readAllLines
A wrapper aroundFiles.readAllLines(Path)- Throws:
IOException
-
readTextFile
Read a text file from the given path, return as a string.
Note: This does *not* protect against untrusted data on its own. Call
safeResolve(String, String)first against the path to ensure it uses valid characters and prevent it escaping the expected directory.- Throws:
IOException
-
checkFileIsWithinDirectory
This method is to provide assurance that the file specified by the path parameter is within the directory specified by directoryPath. Use this for any code that reads from files where the user provides untrusted input.- Throws:
ForbiddenUseException- if the file is not within the directoryIOException
-
safeResolve
This helper method will ensure that the requested path is within the parent directory and using safe characters- Throws:
IOException
-
delete
A wrapper aroundFiles.delete(Path)- Throws:
IOException
-
move
A wrapper aroundFiles.move(Path, Path, CopyOption...)- Throws:
IOException
-
exists
A wrapper aroundFiles.exists(Path, LinkOption...) -
newBufferedWriter
A wrapper aroundFiles.newBufferedWriter(Path, Charset, OpenOption...)- Throws:
IOException
-
newBufferedReader
A wrapper aroundFiles.newBufferedReader(Path, Charset)- Throws:
IOException
-
walk
A wrapper aroundFiles.walk(Path, FileVisitOption...)- Throws:
IOException
-
isRegularFile
A wrapper aroundFiles.isRegularFile(Path, LinkOption...) -
lines
A wrapper aroundFiles.lines(Path, Charset)- Throws:
IOException
-
deleteIfExists
A wrapper aroundFiles.deleteIfExists(Path)- Throws:
IOException
-
size
A wrapper aroundFiles.size(Path)- Throws:
IOException
-
list
A wrapper aroundFiles.list(Path)- Throws:
IOException
-