Top Description Constructors Methods
org.assertj.core.util

public Class Files

extends Object
Class Inheritance
Static Imports
java.io.File.separator, java.util.Objects.requireNonNull, org.assertj.core.util.Arrays.isNullOrEmpty, .Preconditions.checkArgument, .Strings.append, .Strings.concat, .Strings.quote

Utility methods related to files.
Authors
Yvonne Wang, Alex Ruiz

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static UncheckedIOException
private static UncheckedIOException
private static UncheckedIOException
private static void
public static String

Returns:

the content of the file.
contentOf
(File
the file.
file
,
String
the name of the character set to use.
charsetName
)

Loads the text content of a file into a character string.

public static String

Returns:

the content of the file.
contentOf
(File
the file.
file
,
Charset
the character set to use.
charset
)

Loads the text content of a file into a character string.

private static File
public static File

Returns:

the current directory.
currentFolder
()

Returns the current directory.

public static void
delete(File
the file or directory to delete.
file
)

Deprecated use https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#forceDelete-java.io.File- instead
Deletes the given file or directory.
public static List<String>

Returns:

the names of the files inside the specified directory.
fileNamesIn
(String
the name of the directory to start the search from.
dirName
,
boolean
if true, we will look in subdirectories.
recurse
)

Returns the names of the files inside the specified directory.

private static List<String>

Returns:

the names of the files inside the specified directory.
fileNamesIn
(File
the name of the directory to start the search from.
dir
,
boolean
if true, we will look in subdirectories.
recurse
)

Returns the names of the files inside the specified directory.

public static Optional<String>
public static List<String>

Returns:

the content of the file.
linesOf
(File
the file.
file
,
Charset
the character set to use.
charset
)

Loads the text content of a file into a list of strings, each string corresponding to a line.

public static List<String>

Returns:

the content of the file.
linesOf
(File
the file.
file
,
String
the name of the character set to use.
charsetName
)

Loads the text content of a file into a list of strings, each string corresponding to a line.

public static File

Returns:

the new created file.
newFile
(String
the path of the new file.
path
)

Creates a new file using the given path.

public static File

Returns:

the new created directory.
newFolder
(String
the path of the new directory.
path
)

Creates a new directory using the given path.

public static File

Returns:

the created file.
newTemporaryFile
()

Creates a new file in the system's temporary directory.

public static File

Returns:

the created file.
newTemporaryFolder
()

Creates a new directory in the system's temporary directory.

public static File

Returns:

the system's temporary directory.
temporaryFolder
()

Returns the system's temporary directory.
public static String

Returns:

the path of the system's temporary directory.
temporaryFolderPath
()

Returns the path of the system's temporary directory.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Filesback to summary
private Files()

Method Detail

cannotCreateNewFileback to summary
private static UncheckedIOException cannotCreateNewFile(String path, String reason)
cannotCreateNewFileback to summary
private static UncheckedIOException cannotCreateNewFile(String path, Exception cause)
cannotCreateNewFileback to summary
private static UncheckedIOException cannotCreateNewFile(String path, String reason, Exception cause)
checkArgumentCharsetIsSupportedback to summary
private static void checkArgumentCharsetIsSupported(String charsetName)
contentOfback to summary
public static String contentOf(File file, String charsetName)

Loads the text content of a file into a character string.

Parameters
file:File

the file.

charsetName:String

the name of the character set to use.

Returns:String

the content of the file.

Exceptions
IllegalArgumentException:
if the given character set is not supported on this platform.
UncheckedIOException:
if an I/O exception occurs.
contentOfback to summary
public static String contentOf(File file, Charset charset)

Loads the text content of a file into a character string.

Parameters
file:File

the file.

charset:Charset

the character set to use.

Returns:String

the content of the file.

Exceptions
NullPointerException:
if the given charset is null.
UncheckedIOException:
if an I/O exception occurs.
createFileIfPathIsNotANonEmptyDirectoryback to summary
private static File createFileIfPathIsNotANonEmptyDirectory(String path)
currentFolderback to summary
public static File currentFolder()

Returns the current directory.

Returns:File

the current directory.

Exceptions
UncheckedIOException:
if the current directory cannot be obtained.
deleteback to summary
public static void delete(File file)

Deprecated

use https://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html#forceDelete-java.io.File- instead

Deletes the given file or directory.

Parameters
file:File

the file or directory to delete.

Annotations
@Deprecated
fileNamesInback to summary
public static List<String> fileNamesIn(String dirName, boolean recurse)

Returns the names of the files inside the specified directory.

Parameters
dirName:String

the name of the directory to start the search from.

recurse:boolean

if true, we will look in subdirectories.

Returns:List<String>

the names of the files inside the specified directory.

Exceptions
IllegalArgumentException:
if the given directory name does not point to an existing directory.
fileNamesInback to summary
private static List<String> fileNamesIn(File dir, boolean recurse)

Returns the names of the files inside the specified directory.

Parameters
dir:File

the name of the directory to start the search from.

recurse:boolean

if true, we will look in subdirectories.

Returns:List<String>

the names of the files inside the specified directory.

getFileNameExtensionback to summary
public static Optional<String> getFileNameExtension(String fileName)
linesOfback to summary
public static List<String> linesOf(File file, Charset charset)

Loads the text content of a file into a list of strings, each string corresponding to a line. The line endings are either \n, \r or \r\n.

Parameters
file:File

the file.

charset:Charset

the character set to use.

Returns:List<String>

the content of the file.

Exceptions
NullPointerException:
if the given charset is null.
UncheckedIOException:
if an I/O exception occurs.
linesOfback to summary
public static List<String> linesOf(File file, String charsetName)

Loads the text content of a file into a list of strings, each string corresponding to a line. The line endings are either \n, \r or \r\n.

Parameters
file:File

the file.

charsetName:String

the name of the character set to use.

Returns:List<String>

the content of the file.

Exceptions
NullPointerException:
if the given charset is null.
UncheckedIOException:
if an I/O exception occurs.
newFileback to summary
public static File newFile(String path)

Creates a new file using the given path.

Parameters
path:String

the path of the new file.

Returns:File

the new created file.

Exceptions
RuntimeException:
  • if the path belongs to an existing non-empty directory.
  • if the path belongs to an existing file.
UncheckedIOException:
if any I/O error is thrown when creating the new file.
newFolderback to summary
public static File newFolder(String path)

Creates a new directory using the given path.

Parameters
path:String

the path of the new directory.

Returns:File

the new created directory.

Exceptions
RuntimeException:
  • if the path belongs to an existing non-empty directory.
  • if the path belongs to an existing file.
  • if any I/O error is thrown when creating the new directory.
newTemporaryFileback to summary
public static File newTemporaryFile()

Creates a new file in the system's temporary directory. The name of the file will be the result of:

 concat(UUID.randomUUID().toString(), ".txt");
Returns:File

the created file.

newTemporaryFolderback to summary
public static File newTemporaryFolder()

Creates a new directory in the system's temporary directory. The name of the directory will be the result of:

 UUID.randomUUID().toString();
Returns:File

the created file.

temporaryFolderback to summary
public static File temporaryFolder()

Returns the system's temporary directory.

Returns:File

the system's temporary directory.

Annotations
@Deprecated
Exceptions
RuntimeException:
if this method cannot find or create the system's temporary directory.
temporaryFolderPathback to summary
public static String temporaryFolderPath()

Returns the path of the system's temporary directory. This method appends the system's file separator at the end of the path.

Returns:String

the path of the system's temporary directory.