= FileUtils
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Utilities related to the _Java_ _File_ type.

== API

[source,java]
.FileUtils.java
----
class FileUtils {
  Try<T> tryReadAndApply(File file, ThrowingFunction<InputStream, T> inputStreamMapper)     // <.>
  Try<Void> tryReadAndAccept(File file, ThrowingConsumer<InputStream> inputStreamConsumer)     // <.>
  Set<File> searchFiles(File dir, Predicate<File> dirFilter, Predicate<File> fileFilter)     // <.>
  void searchFiles(File dir, Predicate<File> dirFilter, Predicate<File> fileFilter, Consumer<File> onFileFound)     // <.>
  Optional<String> canonicalPath(File file)     // <.>
  String toRelativePath(String commonPath, String absolutePath)     // <.>
  void deleteFile(File file)     // <.>
  boolean deleteDirectory(File directoryToBeDeleted)
  File tempDir(String name)     // <.>
  String realtiveFileName(File root, File file)
  Function<File, String> realtiveFileName(File root)
  File makeDir(File directory)     // <.>
  Optional<File> existingFile(File file)     // <.>
  File existingFileElseFail(File file)     // <.>
  Optional<File> existingDirectory(File file)     // <.>
  File existingDirectoryElseFail(File file)     // <.>
  void copy(File from, File to)     // <.>
  void copyLines(File from, File to, Charset charset, UnaryOperator<String> lineProcessor, OpenOption... openOptions)     // <.>
}
----

<.> xref:#tryReadAndApply_File_ThrowingFunction[tryReadAndApply(File, ThrowingFunction)]
+
--
Opens an _InputStream_ for give _File_ and passes it to given _Function_ for application, then finally closes it.
--
<.> xref:#tryReadAndAccept_File_ThrowingConsumer[tryReadAndAccept(File, ThrowingConsumer)]
+
--
Opens an _InputStream_ for give _File_ and passes it to given _Consumer_ for consumption, then finally closes it.
--
<.> xref:#searchFiles_File_Predicate_Predicate[searchFiles(File, Predicate, Predicate)]
+
--
Recursive file search, starting at _dir_ , going deeper based on predicate _dirFilter_ , collecting files (not directories) based on predicate _fileFilter_ .
--
<.> xref:#searchFiles_File_Predicate_Predicate_Consumer[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_ .
--
<.> xref:#canonicalPath_File[canonicalPath(File)]
<.> xref:#toRelativePath_String_String[toRelativePath(String, String)]
<.> xref:#deleteFile_File[deleteFile(File)]
+
--
Deletes given _File_ . This operation is ignored if the file is a directory or does not exist.
--
<.> xref:#tempDir_String[tempDir(String)]
+
--
Returns a temp directory with delete-on-exit policy.
--
<.> xref:#makeDir_File[makeDir(File)]
+
--
Creates the given directory if it does not already exist. If directory is null acts as a no-op.
--
<.> xref:#existingFile_File[existingFile(File)]
+
--
Optionally given file, based on whether non-null and exists and is a file (not a directory).
--
<.> xref:#existingFileElseFail_File[existingFileElseFail(File)]
+
--
Guard given file against null, non-existence and not representing a file.
--
<.> xref:#existingDirectory_File[existingDirectory(File)]
+
--
Optionally given file, based on whether non-null and exists and is a directory (not a file).
--
<.> xref:#existingDirectoryElseFail_File[existingDirectoryElseFail(File)]
+
--
Guard given file against null, non-existence and not representing a directory (not a file).
--
<.> xref:#copy_File_File[copy(File, File)]
+
--
Copy _from_ file _to_ file, replacing existing.
--
<.> xref:#copyLines_File_File_Charset_UnaryOperator_OpenOption[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]
=== tryReadAndApply(File, ThrowingFunction)

Opens an _InputStream_ for give _File_ and passes it to given _Function_ for application, then finally closes it.

[#tryReadAndAccept_File_ThrowingConsumer]
=== 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]
=== 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]
=== 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]
=== canonicalPath(File)

[#toRelativePath_String_String]
=== toRelativePath(String, String)

[#deleteFile_File]
=== deleteFile(File)

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

[#tempDir_String]
=== tempDir(String)

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

[#makeDir_File]
=== makeDir(File)

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

[#existingFile_File]
=== existingFile(File)

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

[#existingFileElseFail_File]
=== existingFileElseFail(File)

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

[#existingDirectory_File]
=== existingDirectory(File)

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

[#existingDirectoryElseFail_File]
=== existingDirectoryElseFail(File)

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

[#copy_File_File]
=== copy(File, File)

Copy _from_ file _to_ file, replacing existing.

[#copyLines_File_File_Charset_UnaryOperator_OpenOption]
=== copyLines(File, File, Charset, UnaryOperator, OpenOption)

Copy all lines _from_ file _to_ file, using given _Charset_ and processing each line before writing using _lineProcessor_ .
