public final class IOUtil extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
IOUtil.IFileVisitor
A visitor which does something with
File s. |
static class |
IOUtil.LineInformation
A line information returned from
computeLineInformation(File) and
computeLineInformation(InputStream) . |
Modifier and Type | Field and Description |
---|---|
static int |
BUFFER_SIZE
The size of used buffers.
|
static Charset |
DEFAULT_CHARSET
The default charset to use.
|
Modifier | Constructor and Description |
---|---|
private |
IOUtil()
Forbid instances by this private constructor.
|
Modifier and Type | Method and Description |
---|---|
static IOUtil.LineInformation[] |
computeLineInformation(File file)
Computes line information for each text line in the given
File . |
static IOUtil.LineInformation[] |
computeLineInformation(InputStream in)
Computes line information for each text line in the given
InputStream . |
static String |
computeMD5(File file)
Computes the MD5 checksum of the given
File . |
static String |
computeMD5(InputStream in)
Computes the MD5 checksum of the given
InputStream and closes it. |
static boolean |
contains(File parent,
File child)
|
static boolean |
contains(Iterable<File> parents,
File child)
|
static boolean |
copy(InputStream source,
OutputStream target)
Copies the content from the
InputStream to the OutputStream
and closes both streams. |
static File |
createTempDirectory(String prefix,
String suffix)
Creates a temporary directory with help of
File.createTempFile(String, String) . |
static void |
delete(File file)
Deletes the given file/folder with all contained sub files/folders.
|
static boolean |
exists(File file)
Checks if the given
File exists. |
static File |
getClassLocation(Class<?> classInstance) |
static URL |
getClassLocationURL(Class<?> classInstance) |
static File |
getCurrentDirectory()
Returns the current directory.
|
static String |
getFileExtension(File file)
Returns the file extension of the given
File if available. |
static String |
getFileNameWithoutExtension(String fileName)
Returns the file name without file extension for the given file name with extension.
|
static File |
getHomeDirectory()
Returns the home directory.
|
static File |
getProjectRoot(Class<?> classInstance) |
static File |
getTempDirectory()
Returns the temporary directory.
|
static String |
readFrom(File file)
Reads the complete content from the
File . |
static String |
readFrom(InputStream in)
Reads the complete content from the
InputStream and closes it. |
static String |
readFrom(URL url)
Reads the complete content from the
URL . |
static List<File> |
search(File file,
IFilter<File> filter)
|
static File |
toFile(URL url) |
static String |
toFileString(URL url) |
static URI |
toURI(URL url) |
static InputStream |
unifyLineBreaks(InputStream in)
Replaces all line breaks (
\r , \r\n ) in the given InputStream with \n . |
static String |
validateOSIndependentFileName(String name)
Ensures that the segment is a valid OS independent path segment meaning
that it is a valid file/folder name.
|
static void |
visit(File file,
IOUtil.IFileVisitor visitor)
Visits recursive all files and folders.
|
static void |
writeTo(OutputStream out,
String content)
Writes the given content into the given
OutputStream and closes it. |
static void |
writeTo(OutputStream out,
String content,
Charset encoding)
Writes the given content into the given
OutputStream and closes it. |
static void |
writeTo(OutputStream out,
String content,
String encoding)
Writes the given content into the given
OutputStream and closes it. |
public static final int BUFFER_SIZE
public static final Charset DEFAULT_CHARSET
public static String computeMD5(File file) throws IOException
File
.file
- The File
to compute its MD5 checksum.IOException
- Occurred Exception.public static String computeMD5(InputStream in) throws IOException
InputStream
and closes it.in
- The InputStream
which provides the content to compute its MD5 checksum. The InputStream
will be closed.IOException
- Occurred Exception.public static File getHomeDirectory()
public static String getFileExtension(File file)
File
if available.file
- The file to extract it extension.null
if not available.public static String getFileNameWithoutExtension(String fileName)
file
- The file name with extension for that the file name without extension is needed.null
if it was not possible to compute it.public static void delete(File file)
file
- The file/folder to delete.public static String readFrom(URL url) throws IOException
URL
.file
- The URL
to read from.null
if the URL
is null
.IOException
- Occurred Exception.public static String readFrom(File file) throws IOException
File
.file
- The File
to read from.null
if the File
is null
or not an existing file.IOException
- Occurred Exception.public static String readFrom(InputStream in) throws IOException
InputStream
and closes it.in
- The InputStream
to read from and to close.null
if the InputStream
is null
.IOException
- Occurred Exception.public static void writeTo(OutputStream out, String content) throws IOException
OutputStream
and closes it.
Nothing will be written if the content is null
, but the stream will be closed.out
- The OutputStream
to write to.content
- The content to write.IOException
- Occurred Exception.public static void writeTo(OutputStream out, String content, Charset encoding) throws IOException
OutputStream
and closes it.
Nothing will be written if the content is null
, but the stream will be closed.out
- The OutputStream
to write to.content
- The content to write.IOException
- Occurred Exception.public static void writeTo(OutputStream out, String content, String encoding) throws IOException
OutputStream
and closes it.
Nothing will be written if the content is null
, but the stream will be closed.out
- The OutputStream
to write to.content
- The content to write.encoding
- The encoding to use.IOException
- Occurred Exception.public static IOUtil.LineInformation[] computeLineInformation(File file) throws IOException
Computes line information for each text line in the given File
.
A IOUtil.LineInformation
consists of the offset from the beginning of the
file for each line and the indices of tabs '\t'
in each line.
Example content, line break is '\n':
Line 1 Line 2: With some text Line 4Computed line start indices:
result[0] = new LineInformation(0, new int[0]);
result[1] = new LineInformation(7, new int[] {7});
result[2] = new LineInformation(30, new int[0]);
result[3] = new LineInformation(31, new int[0]);
file
- The given File
.IOException
- Occurred Exception.public static IOUtil.LineInformation[] computeLineInformation(InputStream in) throws IOException
Computes line information for each text line in the given InputStream
.
A IOUtil.LineInformation
consists of the offset from the beginning of the
file for each line and the indices of tabs '\t'
in each line.
Example content, line break is '\n':
Line 1 Line 2:\tWith some text Line 4Computed line start indices:
result[0] = new LineInformation(0, new int[0]);
result[1] = new LineInformation(7, new int[] {7});
result[2] = new LineInformation(30, new int[0]);
result[3] = new LineInformation(31, new int[0]);
file
- The given File
.IOException
- Occurred Exception.public static File createTempDirectory(String prefix, String suffix) throws IOException
File.createTempFile(String, String)
.prefix
- The prefix string to be used in generating the file's name; must be at least three characters long.suffix
- The suffix string to be used in generating the file's name; may be null, in which case the suffix ".tmp" will be used.IOException
- Occurred Exception.public static List<File> search(File file, IFilter<File> filter) throws IOException
file
- The File
to start search in.filter
- An optional IFilter
used to accept files. Without a filter all File
s are accepted.File
s.IOException
- Occurred Exceptionpublic static void visit(File file, IOUtil.IFileVisitor visitor) throws IOException
file
- The File
to start in.visitor
- The IOUtil.IFileVisitor
which does something with the visited filesIOException
- Occurred Exceptionpublic static InputStream unifyLineBreaks(InputStream in) throws IOException
\r
, \r\n
) in the given InputStream with \n
.in
- The InputStream
to replace line breaks in.InputStream
with with the replaced line breaks.IOException
- Occurred Exception.public static boolean copy(InputStream source, OutputStream target) throws IOException
InputStream
to the OutputStream
and closes both streams.source
- The InputStream
to read from.target
- The OutputStream
to write to.true
if copy was performed and false
if not performed.IOException
- Occurred Exception.public static File getCurrentDirectory()
public static File getTempDirectory()
public static String validateOSIndependentFileName(String name)
'_'
.segment
- The segment to validate.