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 void
cleanDirectory(File directory)
Deletes a directory recursively.static void
deleteDirectory(File directory)
Deletes a directory recursively.static boolean
deleteQuietly(File file)
Deletes a file, never throwing an exception.static void
forceDelete(File file)
Deletes a file.static File
getTempDirectory()
Returns aFile
representing the system temporary directory.static String
getTempDirectoryPath()
Returns the path to the system temporary directory.static boolean
isSymlink(File file)
Determines whether the specified file is a Symbolic Link rather than an actual file.static InputStream
openInputStream(File file)
Opens aFileInputStream
for the specified file, providing better error messages than simply callingnew FileInputStream(file)
.static OutputStream
openOutputStream(File file)
Opens aFileOutputStream
for the specified file, checking and creating the parent directory if it does not exist.static OutputStream
openOutputStream(File file, boolean append)
Opens aFileOutputStream
for 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 String
readFileToString(File file)
Deprecated.2.5 usereadFileToString(File, Charset)
insteadstatic String
readFileToString(File file, String encoding)
Reads the contents of a file into a String.static String
readFileToString(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 void
writeByteArrayToFile(File file, byte[] data)
Writes a byte array to a file creating the file if it does not exist.static void
writeByteArrayToFile(File file, byte[] data, boolean append)
Writes a byte array to a file creating the file if it does not exist.static void
writeByteArrayToFile(File file, byte[] data, int off, int len, boolean append)
Writeslen
bytes from the specified byte array starting at offsetoff
to a file, creating the file if it does not exist.static void
writeStringToFile(File file, String data)
Deprecated.2.5 usewriteStringToFile(File, String, Charset, boolean)
insteadstatic void
writeStringToFile(File file, String data, String encoding)
Writes a String to a file creating the file if it does not exist.static void
writeStringToFile(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
false
if 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 isnull
FileNotFoundException
- 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 benull
encoding
- the encoding to use,null
means 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 benull
encoding
- the encoding to use,null
means 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 aFileInputStream
for 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
InputStream
for 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,null
means 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,null
means 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 aFileOutputStream
for 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
append
- iftrue
, then bytes will be added to the end of the file rather than overwriting- Returns:
- a new
OutputStream
for 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 aFile
representing 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:
true
if 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
Writeslen
bytes from the specified byte array starting at offsetoff
to 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 aFileOutputStream
for 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
OutputStream
for 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 benull
encoding
- the encoding to use,null
means 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
-
-