public final class IOUtils extends Object
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(InputStream input)
Unconditionally close an
InputStream . |
static void |
closeQuietly(OutputStream output)
Unconditionally close an
OutputStream . |
static void |
closeQuietly(Reader reader)
Unconditionally close a
Reader . |
static void |
closeQuietly(Writer writer)
Unconditionally close a
Writer . |
static void |
copy(File input,
File output)
Copy bytes from one
File to another File . |
static void |
copy(InputStream input,
OutputStream output)
Copy bytes from an
InputStream to an OutputStream . |
static void |
copy(Reader reader,
Writer writer)
Copy characters from a
Reader to a Writer . |
static URI |
createURI(String uri)
Parses an URI string.
|
static void |
deleteDirectory(File directory)
Deletes a directory.
|
static void |
deleteDirectoryContent(File directory)
Recursively deletes all files contained in the specified directory, but does not delete the directory itself.
|
static void |
deleteFile(File file)
Deletes a file.
|
static File |
getAbsoluteFile(File relativeFile,
File directory)
Returns the absolute form of the specified relative file,
using the specified directory to compute this absolute path.
|
static String |
getFileExtension(File file)
Returns the extension of the specified
File . |
static String |
getFileExtension(String filename)
Returns the extension of the specified file name.
|
static boolean |
isFileExtension(File file,
String extension)
Determines whether the file has the specified file extension.
|
static byte[] |
toByteArray(File file)
Get the contents of a
File as a byte array. |
static byte[] |
toByteArray(InputStream input)
Get the contents of an
InputStream as a byte array. |
static String |
toString(File file,
String charsetName)
Get the contents of a
File as a String using the specified character encoding. |
public static String getFileExtension(File file)
File
.
The file extension portion is defined as the string following the last period (".") character in the name. If there is no period in the name, the path has no file extension portion. If the name ends in a period, the file extension portion is the empty string.
file
- the file for which the extension is to determine.null
if the file has no extension.public static String getFileExtension(String filename)
The file extension portion is defined as the string following the last period (".") character in the name. If there is no period in the name, the path has no file extension portion. If the name ends in a period, the file extension portion is the empty string.
filename
- the file for which the extension is to determine.null
if the file has no extension.public static File getAbsoluteFile(File relativeFile, File directory)
This method does the same job as
, but uses the specified directory to compute the absolute
file instead of the current user directory.File.getAbsoluteFile()
relativeFile
- the relative file.directory
- the reference directory (to use in place of the current user directory).public static String toString(File file, String charsetName) throws IOException
File
as a String
using the specified character encoding.file
- the file to read from.charsetName
- the encoding to use, null
means platform default.String
.IOException
- if an I/O error occurs.public static byte[] toByteArray(File file) throws IOException
File
as a byte
array.file
- the file to read from.byte
array.IOException
- if an I/O error occurs.public static byte[] toByteArray(InputStream input) throws IOException
InputStream
as a byte
array.input
- the stream to read from.byte
array.IOException
- if an I/O error occurs.public static void copy(InputStream input, OutputStream output) throws IOException
InputStream
to an OutputStream
.
This method buffers the input internally, so there is no need to use a BufferedInputStream
.
input
- the input stream to read from.output
- the output stream where to copy the bytes.IOException
- if an I/O error occurspublic static void copy(File input, File output) throws IOException
File
to another File
.input
- the source file.output
- the target file.IOException
- if an I/O error occursIllegalArgumentException
- if input or output is a directorypublic static void copy(Reader reader, Writer writer) throws IOException
Reader
to a Writer
.
This method buffers the input internally, so there is no need to use a BufferedReader
.
reader
- the source reader where to read the bytes.writer
- the target writer where to copy the bytes.IOException
- if an I/O error occurspublic static void closeQuietly(InputStream input)
InputStream
.
Equivalent to InputStream.close()
, except any exceptions will be ignored. This is typically used in finally blocks.
input
- the InputStream
to close, may be null
or already closedpublic static void closeQuietly(OutputStream output)
OutputStream
.
Equivalent to OutputStream.close()
, except any exceptions will be ignored. This is typically used in finally blocks.
output
- the OutputStream
to close, may be null
or already closedpublic static void closeQuietly(Reader reader)
Reader
.
Equivalent to Reader.close()
, except any exceptions will be ignored. This is typically used in finally blocks.
reader
- the Reader
to close, may be null
or already closedpublic static void closeQuietly(Writer writer)
Writer
.
Equivalent to Writer.close()
, except any exceptions will be ignored. This is typically used in finally blocks.
writer
- the Writer
to close, may be null
or already closedpublic static URI createURI(String uri)
File.exists()
).
Otherwise URI.createURI(String)
is called.uri
- the uri to parse.URI.createURI(String)
public static void deleteFile(File file)
file
- the file to delete (may be null
)deleteDirectory(File)
public static void deleteDirectory(File directory)
directory
- the directory to delete (may be null
)deleteFile(File)
public static void deleteDirectoryContent(File directory)
directory
- the directory whose content is to be deleted (may be null
)deleteFile(File)
public static boolean isFileExtension(File file, String extension)
file
- the file whose extension is to verifyextension
- the file extension (without the '.'
character)