Package nl.saxion.app
Class FileUtils
java.lang.Object
nl.saxion.app.FileUtils
File operations utility class.
This class gives you access to the file system and lets you list directories, copy, move and delete files/directories.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
File information for files or directories.static class
Exception class thrown when an exception occurs, somewhere in this class. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
appendToFile
(String path, String contents) Append contents to an existing file.void
Copy file or directory to new location.void
createDirectories
(String path) Creates all directories in a path recursively.void
createDirectory
(String path) Creates a single directory.void
createFile
(String path) Create an empty file on the file system.void
Delete a single file or an empty directory on the filesystem.void
WARNING: Deletes all directories with all its files and subdirectories.boolean
Check if a certain file or directory does exist.getFileInfo
(String path) Get information about a file or a directory.getParentDirectory
(String path) Get the parent directory of a certain path.Returns the current working directory, as full (absolute) path.boolean
isDirectory
(String path) Check if a path is of type directory.boolean
Check if a path is of type file.list()
Get all files and directories in the current working directory This method does not work recursively.Get all files and directories in a certain directory.Get all subdirectories in a certain directory.Get all files in a certain directory.void
Move file or directory to new location.readFileToString
(String path) Read contents of a file to a String.toAbsolutePath
(String relativePath) Converts an relative path to an absolute path.toRelativePath
(String absolutePath) Converts an absolute path to a relative path.void
writeToFile
(String path, String contents) Overwrites an existing file and replaces the contents.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
list
Get all files and directories in the current working directory This method does not work recursively.- Returns:
- List with filenames
-
list
Get all files and directories in a certain directory. This method does not work recursively.- Parameters:
path
- path on the file system- Returns:
- List with filenames
-
listFiles
Get all files in a certain directory. This method does not work recursively.- Parameters:
path
- path on the file system- Returns:
- List with filenames
-
listDirs
Get all subdirectories in a certain directory. This method does not work recursively.- Parameters:
path
- path on the file system- Returns:
- List with filenames
-
exists
Check if a certain file or directory does exist.- Parameters:
path
- path on the file system- Returns:
- true, if the file/directory exists.
-
isFile
Check if a path is of type file.- Parameters:
path
- path on the file system- Returns:
- true, if path is file.
-
isDirectory
Check if a path is of type directory.- Parameters:
path
- path on the file system- Returns:
- true, if path is directory.
-
getFileInfo
Get information about a file or a directory.- Parameters:
path
- path on the file system- Returns:
- FileInfo object with information about the file
-
readFileToString
Read contents of a file to a String.- Parameters:
path
- path on the file system- Returns:
- Contents of the file.
-
getWorkingDirectory
Returns the current working directory, as full (absolute) path.- Returns:
- current working directory
-
getParentDirectory
Get the parent directory of a certain path. Useful when traversing the filesystem.- Parameters:
path
- path on the filesystem- Returns:
- parent directory
-
toRelativePath
Converts an absolute path to a relative path.- Parameters:
absolutePath
- Absolute path- Returns:
- relative path
-
toAbsolutePath
Converts an relative path to an absolute path.- Parameters:
relativePath
- Relative path- Returns:
- absolute path
-
copy
Copy file or directory to new location.- Parameters:
fromPath
- fromtoPath
- to
-
move
Move file or directory to new location.- Parameters:
fromPath
- fromtoPath
- to
-
delete
Delete a single file or an empty directory on the filesystem. This method does not work recursively.- Parameters:
path
- path on the file system.
-
deleteDirectoryAndContents
WARNING: Deletes all directories with all its files and subdirectories. Be careful with this operation!!!- Parameters:
path
- path on the file system.
-
createFile
Create an empty file on the file system.- Parameters:
path
- path on the filesystem
-
writeToFile
Overwrites an existing file and replaces the contents.- Parameters:
path
- path on the file systemcontents
- contents to write to the file
-
appendToFile
Append contents to an existing file.- Parameters:
path
- path on the file systemcontents
- contents to write to the file
-
createDirectory
Creates a single directory. This operation will NOT work recursively and only create a single directory. In case you are looking for recursive directory creation, see @createDirectories(String path)- Parameters:
path
- path on the file system
-
createDirectories
Creates all directories in a path recursively.- Parameters:
path
- path on the file system
-