FileUtils

Utilities related to the Java File type.

API

FileUtils.java
class FileUtils {
  Try<T> tryReadAndApply(File file, ThrowingFunction<InputStream, T> inputStreamMapper)     (1)
  Try<Void> tryReadAndAccept(File file, ThrowingConsumer<InputStream> inputStreamConsumer)     (2)
  Set<File> searchFiles(File dir, Predicate<File> dirFilter, Predicate<File> fileFilter)     (3)
  void searchFiles(File dir, Predicate<File> dirFilter, Predicate<File> fileFilter, Consumer<File> onFileFound)     (4)
  Optional<String> canonicalPath(File file)     (5)
  String toRelativePath(String commonPath, String absolutePath)     (6)
  void deleteFile(File file)     (7)
  boolean deleteDirectory(File directoryToBeDeleted)
  File tempDir(String name)     (8)
  String realtiveFileName(File root, File file)
  Function<File, String> realtiveFileName(File root)
  File makeDir(File directory)     (9)
  Optional<File> existingFile(File file)     (10)
  File existingFileElseFail(File file)     (11)
  Optional<File> existingDirectory(File file)     (12)
  File existingDirectoryElseFail(File file)     (13)
  void copy(File from, File to)     (14)
  void copyLines(File from, File to, Charset charset, UnaryOperator<String> lineProcessor, OpenOption... openOptions)     (15)
}
1 tryReadAndApply(File, ThrowingFunction)

Opens an InputStream for give File and passes it to given Function for application, then finally closes it.

2 tryReadAndAccept(File, ThrowingConsumer)

Opens an InputStream for give File and passes it to given Consumer for consumption, then finally closes it.

3 searchFiles(File, Predicate, Predicate)

Recursive file search, starting at dir , going deeper based on predicate dirFilter , collecting files (not directories) based on predicate fileFilter .

4 searchFiles(File, Predicate, Predicate, Consumer)

Recursive file search, starting at dir , going deeper based on predicate dirFilter , consuming files (not directories) based on predicate fileFilter .

5 canonicalPath(File)
6 toRelativePath(String, String)
7 deleteFile(File)

Deletes given File . This operation is ignored if the file is a directory or does not exist.

8 tempDir(String)

Returns a temp directory with delete-on-exit policy.

9 makeDir(File)

Creates the given directory if it does not already exist. If directory is null acts as a no-op.

10 existingFile(File)

Optionally given file, based on whether non-null and exists and is a file (not a directory).

11 existingFileElseFail(File)

Guard given file against null, non-existence and not representing a file.

12 existingDirectory(File)

Optionally given file, based on whether non-null and exists and is a directory (not a file).

13 existingDirectoryElseFail(File)

Guard given file against null, non-existence and not representing a directory (not a file).

14 copy(File, File)

Copy from file to file, replacing existing.

15 copyLines(File, File, Charset, UnaryOperator, OpenOption)

Copy all lines from file to file, using given Charset and processing each line before writing using lineProcessor .

Members

tryReadAndApply(File, ThrowingFunction)

Opens an InputStream for give File and passes it to given Function for application, then finally closes it.

tryReadAndAccept(File, ThrowingConsumer)

Opens an InputStream for give File and passes it to given Consumer for consumption, then finally closes it.

searchFiles(File, Predicate, Predicate)

Recursive file search, starting at dir , going deeper based on predicate dirFilter , collecting files (not directories) based on predicate fileFilter .

searchFiles(File, Predicate, Predicate, Consumer)

Recursive file search, starting at dir , going deeper based on predicate dirFilter , consuming files (not directories) based on predicate fileFilter .

canonicalPath(File)

toRelativePath(String, String)

deleteFile(File)

Deletes given File . This operation is ignored if the file is a directory or does not exist.

tempDir(String)

Returns a temp directory with delete-on-exit policy.

makeDir(File)

Creates the given directory if it does not already exist. If directory is null acts as a no-op.

existingFile(File)

Optionally given file, based on whether non-null and exists and is a file (not a directory).

existingFileElseFail(File)

Guard given file against null, non-existence and not representing a file.

existingDirectory(File)

Optionally given file, based on whether non-null and exists and is a directory (not a file).

existingDirectoryElseFail(File)

Guard given file against null, non-existence and not representing a directory (not a file).

copy(File, File)

Copy from file to file, replacing existing.

copyLines(File, File, Charset, UnaryOperator, OpenOption)

Copy all lines from file to file, using given Charset and processing each line before writing using lineProcessor .