org.assertj.core.util
Class Files

java.lang.Object
  extended by org.assertj.core.util.Files

public class Files
extends Object

Utility methods related to files.

Author:
Yvonne Wang, Alex Ruiz

Method Summary
static String contentOf(File file, Charset charset)
          Loads the text content of a file into a character string.
static String contentOf(File file, String charsetName)
          Loads the text content of a file into a character string.
static File currentFolder()
          Returns the current directory.
static void delete(File file)
          Deletes the given file or directory.
static List<String> fileNamesIn(String dirName, boolean recurse)
          Returns the names of the files inside the specified directory.
static void flushAndClose(OutputStream out)
          Flushes and closes the given OutputStream.
static void flushAndClose(Writer writer)
          Flushes and closes the given Writer.
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.
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.
static File newFile(String path)
          Creates a new file using the given path.
static File newFolder(String path)
          Creates a new directory using the given path.
static File newTemporaryFile()
          Creates a new file in the system's temporary directory.
static File newTemporaryFolder()
          Creates a new directory in the system's temporary directory.
static File temporaryFolder()
          Returns the system's temporary directory.
static String temporaryFolderPath()
          Returns the path of the system's temporary directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fileNamesIn

public static List<String> fileNamesIn(String dirName,
                                       boolean recurse)
Returns the names of the files inside the specified directory.

Parameters:
dirName - the name of the directory to start the search from.
recurse - if true, we will look in subdirectories.
Returns:
the names of the files inside the specified directory.
Throws:
IllegalArgumentException - if the given directory name does not point to an existing directory.

temporaryFolder

public static File temporaryFolder()
Returns the system's temporary directory.

Returns:
the system's temporary directory.
Throws:
FilesException - if this method cannot find or create the system's temporary directory.

temporaryFolderPath

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:
the path of the system's temporary directory.

newTemporaryFile

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(String.valueOf(System.currentTimeMillis()), ".txt");
 

Returns:
the created file.

newTemporaryFolder

public static File newTemporaryFolder()
Creates a new directory in the system's temporary directory. The name of the directory will be the result of:

 System.currentTimeMillis();
 

Returns:
the created file.

newFile

public static File newFile(String path)
Creates a new file using the given path.

Parameters:
path - the path of the new file.
Returns:
the new created file.
Throws:
FilesException - if the path belongs to an existing non-empty directory.
FilesException - if the path belongs to an existing file.
FilesException - if any I/O error is thrown when creating the new file.

newFolder

public static File newFolder(String path)
Creates a new directory using the given path.

Parameters:
path - the path of the new directory.
Returns:
the new created directory.
Throws:
FilesException - if the path belongs to an existing non-empty directory.
FilesException - if the path belongs to an existing file.
FilesException - if any I/O error is thrown when creating the new directory.

flushAndClose

public static void flushAndClose(Writer writer)
Flushes and closes the given Writer. Any I/O errors catched by this method are ignored and not rethrown.

Parameters:
writer - the writer to flush and close.

flushAndClose

public static void flushAndClose(OutputStream out)
Flushes and closes the given OutputStream. Any I/O errors catched by this method are ignored and not rethrown.

Parameters:
out - the output stream to flush and close.

currentFolder

public static File currentFolder()
Returns the current directory.

Returns:
the current directory.
Throws:
FilesException - if the current directory cannot be obtained.

delete

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

Parameters:
file - the file or directory to delete.

contentOf

public static String contentOf(File file,
                               String charsetName)
Loads the text content of a file into a character string.

Parameters:
file - the file.
charsetName - the name of the character set to use.
Returns:
the content of the file.
Throws:
IllegalArgumentException - if the given character set is not supported on this platform.
FilesException - if an I/O exception occurs.

contentOf

public static String contentOf(File file,
                               Charset charset)
Loads the text content of a file into a character string.

Parameters:
file - the file.
charset - the character set to use.
Returns:
the content of the file.
Throws:
NullPointerException - if the given charset is null.
FilesException - if an I/O exception occurs.

linesOf

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 - the file.
charset - the character set to use.
Returns:
the content of the file.
Throws:
NullPointerException - if the given charset is null.
FilesException - if an I/O exception occurs.

linesOf

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 - the file.
charsetName - the name of the character set to use.
Returns:
the content of the file.
Throws:
NullPointerException - if the given charset is null.
FilesException - if an I/O exception occurs.


Copyright © 2013–2015 AssertJ. All rights reserved.