Class FileUtils
- java.lang.Object
-
- org.apache.directory.api.util.FileUtils
-
public final class FileUtils extends Object
This code comes from Apache commons.io library. Origin of code: Excalibur, Alexandria, Tomcat, Commons-Utils.- Author:
- Apache Directory Project
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidcleanDirectory(File directory)Deletes a directory recursively.static voiddeleteDirectory(File directory)Deletes a directory recursively.static booleandeleteQuietly(File file)Deletes a file, never throwing an exception.static voidforceDelete(File file)Deletes a file.static FilegetTempDirectory()Returns aFilerepresenting the system temporary directory.static StringgetTempDirectoryPath()Returns the path to the system temporary directory.static booleanisSymlink(File file)Determines whether the specified file is a Symbolic Link rather than an actual file.static InputStreamopenInputStream(File file)Opens aFileInputStreamfor the specified file, providing better error messages than simply callingnew FileInputStream(file).static OutputStreamopenOutputStream(File file)Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.static OutputStreamopenOutputStream(File file, boolean append)Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.static byte[]readFileToByteArray(File file)Reads the contents of a file into a byte array.static StringreadFileToString(File file)Deprecated.2.5 usereadFileToString(File, Charset)insteadstatic StringreadFileToString(File file, String encoding)Reads the contents of a file into a String.static StringreadFileToString(File file, Charset encoding)Reads the contents of a file into a String.static List<String>readLines(File file)Deprecated.2.5 usereadLines(File, Charset)insteadstatic List<String>readLines(File file, Charset encoding)Reads the contents of a file line by line to a List of Strings.static voidwriteByteArrayToFile(File file, byte[] data)Writes a byte array to a file creating the file if it does not exist.static voidwriteByteArrayToFile(File file, byte[] data, boolean append)Writes a byte array to a file creating the file if it does not exist.static voidwriteByteArrayToFile(File file, byte[] data, int off, int len, boolean append)Writeslenbytes from the specified byte array starting at offsetoffto a file, creating the file if it does not exist.static voidwriteStringToFile(File file, String data)Deprecated.2.5 usewriteStringToFile(File, String, Charset, boolean)insteadstatic voidwriteStringToFile(File file, String data, String encoding)Writes a String to a file creating the file if it does not exist.static voidwriteStringToFile(File file, String data, Charset encoding, boolean append)Writes a String to a file creating the file if it does not exist.
-
-
-
Field Detail
-
ONE_KB
public static final long ONE_KB
The number of bytes in a kilobyte.- See Also:
- Constant Field Values
-
ONE_MB
public static final long ONE_MB
The number of bytes in a megabyte.- See Also:
- Constant Field Values
-
-
Method Detail
-
deleteDirectory
public static void deleteDirectory(File directory) throws IOException
Deletes a directory recursively.- Parameters:
directory- directory to delete- Throws:
IOException- in case deletion is unsuccessful
-
isSymlink
public static boolean isSymlink(File file) throws IOException
Determines whether the specified file is a Symbolic Link rather than an actual file.Will not return true if there is a Symbolic Link anywhere in the path, only if the specific file is.
Note: the current implementation always returns
falseif the system is detected as WindowsFor code that runs on Java 1.7 or later, use the following method instead:
boolean java.nio.file.Files.isSymbolicLink(Path path)- Parameters:
file- the file to check- Returns:
- true if the file is a Symbolic Link
- Throws:
IOException- if an IO error occurs while checking the file- Since:
- 2.0
-
cleanDirectory
public static void cleanDirectory(File directory) throws IOException
Deletes a directory recursively.- Parameters:
directory- directory to delete- Throws:
IOException- in case deletion is unsuccessful
-
forceDelete
public static void forceDelete(File file) throws IOException
Deletes a file. If file is a directory, delete it and all sub-directories.The difference between File.delete() and this method are:
- A directory to be deleted does not have to be empty.
- You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)
- Parameters:
file- file or directory to delete, must not benull- Throws:
NullPointerException- if the directory isnullFileNotFoundException- if the file was not foundIOException- in case deletion is unsuccessful
-
getTempDirectoryPath
public static String getTempDirectoryPath()
Returns the path to the system temporary directory.- Returns:
- the path to the system temporary directory.
- Since:
- 2.0
-
readFileToString
@Deprecated public static String readFileToString(File file) throws IOException
Deprecated.2.5 usereadFileToString(File, Charset)insteadReads the contents of a file into a String using the default encoding for the VM. The file is always closed.- Parameters:
file- the file to read, must not benull- Returns:
- the file contents, never
null - Throws:
IOException- in case of an I/O error- Since:
- 1.3.1
-
readFileToString
public static String readFileToString(File file, Charset encoding) throws IOException
Reads the contents of a file into a String. The file is always closed.- Parameters:
file- the file to read, must not benullencoding- the encoding to use,nullmeans platform default- Returns:
- the file contents, never
null - Throws:
IOException- in case of an I/O error- Since:
- 2.3
-
readFileToString
public static String readFileToString(File file, String encoding) throws IOException
Reads the contents of a file into a String. The file is always closed.- Parameters:
file- the file to read, must not benullencoding- the encoding to use,nullmeans platform default- Returns:
- the file contents, never
null - Throws:
IOException- in case of an I/O error- Since:
- 2.3
-
openInputStream
public static InputStream openInputStream(File file) throws IOException
Opens aFileInputStreamfor the specified file, providing better error messages than simply callingnew FileInputStream(file).At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.
- Parameters:
file- the file to open for input, must not benull- Returns:
- a new
InputStreamfor the specified file - Throws:
FileNotFoundException- if the file does not existIOException- if the file object is a directoryIOException- if the file cannot be read- Since:
- 1.3
-
writeStringToFile
@Deprecated public static void writeStringToFile(File file, String data) throws IOException
Deprecated.2.5 usewriteStringToFile(File, String, Charset, boolean)insteadWrites a String to a file creating the file if it does not exist using the default encoding for the VM.- Parameters:
file- the file to writedata- the content to write to the file- Throws:
IOException- in case of an I/O error
-
writeStringToFile
public static void writeStringToFile(File file, String data, String encoding) throws IOException
Writes a String to a file creating the file if it does not exist. NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.- Parameters:
file- the file to writedata- the content to write to the fileencoding- the encoding to use,nullmeans platform default- Throws:
IOException- in case of an I/O errorUnsupportedEncodingException- if the encoding is not supported by the VM
-
writeStringToFile
public static void writeStringToFile(File file, String data, Charset encoding, boolean append) throws IOException
Writes a String to a file creating the file if it does not exist.- Parameters:
file- the file to writedata- the content to write to the fileencoding- the encoding to use,nullmeans platform defaultappend- iftrue, then the String will be added to the end of the file rather than overwriting- Throws:
IOException- in case of an I/O error- Since:
- 2.3
-
openOutputStream
public static OutputStream openOutputStream(File file, boolean append) throws IOException
Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.
- Parameters:
file- the file to open for output, must not benullappend- iftrue, then bytes will be added to the end of the file rather than overwriting- Returns:
- a new
OutputStreamfor the specified file - Throws:
IOException- if the file object is a directoryIOException- if the file cannot be written toIOException- if a parent directory needs creating but that fails- Since:
- 2.1
-
getTempDirectory
public static File getTempDirectory()
Returns aFilerepresenting the system temporary directory.- Returns:
- the system temporary directory.
- Since:
- 2.0
-
deleteQuietly
public static boolean deleteQuietly(File file)
Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.The difference between File.delete() and this method are:
- A directory to be deleted does not have to be empty.
- No exceptions are thrown when a file or directory cannot be deleted.
- Parameters:
file- file or directory to delete, can benull- Returns:
trueif the file or directory was deleted, otherwisefalse- Since:
- 1.4
-
writeByteArrayToFile
public static void writeByteArrayToFile(File file, byte[] data) throws IOException
Writes a byte array to a file creating the file if it does not exist.NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
- Parameters:
file- the file to write todata- the content to write to the file- Throws:
IOException- in case of an I/O erroe- Since:
- 1.1
-
writeByteArrayToFile
public static void writeByteArrayToFile(File file, byte[] data, boolean append) throws IOException
Writes a byte array to a file creating the file if it does not exist.- Parameters:
file- the file to write todata- the content to write to the fileappend- iftrue, then bytes will be added to the end of the file rather than overwriting- Throws:
IOException- in case of an I/O error- Since:
- 2.1
-
writeByteArrayToFile
public static void writeByteArrayToFile(File file, byte[] data, int off, int len, boolean append) throws IOException
Writeslenbytes from the specified byte array starting at offsetoffto a file, creating the file if it does not exist.- Parameters:
file- the file to write todata- the content to write to the fileoff- the start offset in the datalen- the number of bytes to writeappend- iftrue, then bytes will be added to the end of the file rather than overwriting- Throws:
IOException- in case of an I/O error- Since:
- 2.5
-
readFileToByteArray
public static byte[] readFileToByteArray(File file) throws IOException
Reads the contents of a file into a byte array. The file is always closed.- Parameters:
file- the file to read, must not benull- Returns:
- the file contents, never
null - Throws:
IOException- in case of an I/O error- Since:
- 1.1
-
openOutputStream
public static OutputStream openOutputStream(File file) throws IOException
Opens aFileOutputStreamfor the specified file, checking and creating the parent directory if it does not exist.At the end of the method either the stream will be successfully opened, or an exception will have been thrown.
The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.
- Parameters:
file- the file to open for output, must not benull- Returns:
- a new
OutputStreamfor the specified file - Throws:
IOException- if the file object is a directoryIOException- if the file cannot be written toIOException- if a parent directory needs creating but that fails- Since:
- 1.3
-
readLines
@Deprecated public static List<String> readLines(File file) throws IOException
Deprecated.2.5 usereadLines(File, Charset)insteadReads the contents of a file line by line to a List of Strings using the default encoding for the VM. The file is always closed.- Parameters:
file- the file to read, must not benull- Returns:
- the list of Strings representing each line in the file, never
null - Throws:
IOException- in case of an I/O error- Since:
- 1.3
-
readLines
public static List<String> readLines(File file, Charset encoding) throws IOException
Reads the contents of a file line by line to a List of Strings. The file is always closed.- Parameters:
file- the file to read, must not benullencoding- the encoding to use,nullmeans platform default- Returns:
- the list of Strings representing each line in the file, never
null - Throws:
IOException- in case of an I/O error- Since:
- 2.3
-
-