public final class IOUtil
extends java.lang.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 java.nio.charset.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(java.io.File file)
Computes line information for each text line in the given
File . |
static IOUtil.LineInformation[] |
computeLineInformation(java.io.InputStream in)
Computes line information for each text line in the given
InputStream . |
static java.lang.String |
computeMD5(java.io.File file)
Computes the MD5 checksum of the given
File . |
static java.lang.String |
computeMD5(java.io.InputStream in)
Computes the MD5 checksum of the given
InputStream and closes it. |
static boolean |
contains(java.io.File parent,
java.io.File child)
Checks if the given parent
File contains (recursive) the child File . |
static boolean |
contains(java.lang.Iterable<java.io.File> parents,
java.io.File child)
Checks if at least one given parent
File contains (recursive) the child File . |
static boolean |
copy(java.io.InputStream source,
java.io.OutputStream target)
Copies the content from the
InputStream to the OutputStream
and closes both streams. |
static java.io.File |
createTempDirectory(java.lang.String prefix,
java.lang.String suffix)
Creates a temporary directory with help of
File.createTempFile(String, String) . |
static void |
delete(java.io.File file)
Deletes the given file/folder with all contained sub files/folders.
|
static boolean |
exists(java.io.File file)
Checks if the given
File exists. |
static java.io.File |
getClassLocation(java.lang.Class<?> classInstance) |
static java.net.URL |
getClassLocationURL(java.lang.Class<?> classInstance) |
static java.io.File |
getCurrentDirectory()
Returns the current directory.
|
static java.lang.String |
getFileExtension(java.io.File file)
Returns the file extension of the given
File if available. |
static java.lang.String |
getFileNameWithoutExtension(java.lang.String fileName)
Returns the file name without file extension for the given file name with extension.
|
static java.io.File |
getHomeDirectory()
Returns the home directory.
|
static java.io.File |
getProjectRoot(java.lang.Class<?> classInstance) |
static java.io.File |
getTempDirectory()
Returns the temporary directory.
|
static java.lang.String |
readFrom(java.io.File file)
Reads the complete content from the
File . |
static java.lang.String |
readFrom(java.io.InputStream in)
Reads the complete content from the
InputStream and closes it. |
static java.lang.String |
readFrom(java.net.URL url)
Reads the complete content from the
URL . |
static java.util.List<java.io.File> |
search(java.io.File file,
IFilter<java.io.File> filter)
|
static java.io.File |
toFile(java.net.URL url) |
static java.lang.String |
toFileString(java.net.URL url) |
static java.net.URI |
toURI(java.net.URL url) |
static java.io.InputStream |
unifyLineBreaks(java.io.InputStream in)
Replaces all line breaks (
\r , \r\n ) in the given InputStream with \n . |
static java.lang.String |
validateOSIndependentFileName(java.lang.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(java.io.File file,
IOUtil.IFileVisitor visitor)
Visits recursive all files and folders.
|
static void |
writeTo(java.io.OutputStream out,
java.lang.String content)
Writes the given content into the given
OutputStream and closes it. |
static void |
writeTo(java.io.OutputStream out,
java.lang.String content,
java.nio.charset.Charset encoding)
Writes the given content into the given
OutputStream and closes it. |
static void |
writeTo(java.io.OutputStream out,
java.lang.String content,
java.lang.String encoding)
Writes the given content into the given
OutputStream and closes it. |
public static final int BUFFER_SIZE
public static final java.nio.charset.Charset DEFAULT_CHARSET
public static java.lang.String computeMD5(java.io.File file) throws java.io.IOException
File
.file
- The File
to compute its MD5 checksum.java.io.IOException
- Occurred Exception.public static java.lang.String computeMD5(java.io.InputStream in) throws java.io.IOException
InputStream
and closes it.in
- The InputStream
which provides the content to compute its MD5 checksum. The InputStream
will be closed.java.io.IOException
- Occurred Exception.public static java.io.File getHomeDirectory()
public static java.lang.String getFileExtension(java.io.File file)
File
if available.file
- The file to extract it extension.null
if not available.public static java.lang.String getFileNameWithoutExtension(java.lang.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(java.io.File file)
file
- The file/folder to delete.public static java.lang.String readFrom(java.net.URL url) throws java.io.IOException
URL
.file
- The URL
to read from.null
if the URL
is null
.java.io.IOException
- Occurred Exception.public static java.lang.String readFrom(java.io.File file) throws java.io.IOException
File
.file
- The File
to read from.null
if the File
is null
or not an existing file.java.io.IOException
- Occurred Exception.public static java.lang.String readFrom(java.io.InputStream in) throws java.io.IOException
InputStream
and closes it.in
- The InputStream
to read from and to close.null
if the InputStream
is null
.java.io.IOException
- Occurred Exception.public static void writeTo(java.io.OutputStream out, java.lang.String content) throws java.io.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.java.io.IOException
- Occurred Exception.public static void writeTo(java.io.OutputStream out, java.lang.String content, java.nio.charset.Charset encoding) throws java.io.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.java.io.IOException
- Occurred Exception.public static void writeTo(java.io.OutputStream out, java.lang.String content, java.lang.String encoding) throws java.io.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.java.io.IOException
- Occurred Exception.public static IOUtil.LineInformation[] computeLineInformation(java.io.File file) throws java.io.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
.java.io.IOException
- Occurred Exception.public static IOUtil.LineInformation[] computeLineInformation(java.io.InputStream in) throws java.io.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
.java.io.IOException
- Occurred Exception.public static java.io.File createTempDirectory(java.lang.String prefix, java.lang.String suffix) throws java.io.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.java.io.IOException
- Occurred Exception.public static java.util.List<java.io.File> search(java.io.File file, IFilter<java.io.File> filter) throws java.io.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.java.io.IOException
- Occurred Exceptionpublic static void visit(java.io.File file, IOUtil.IFileVisitor visitor) throws java.io.IOException
file
- The File
to start in.visitor
- The IOUtil.IFileVisitor
which does something with the visited filesjava.io.IOException
- Occurred Exceptionpublic static java.io.InputStream unifyLineBreaks(java.io.InputStream in) throws java.io.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.java.io.IOException
- Occurred Exception.public static boolean contains(java.lang.Iterable<java.io.File> parents, java.io.File child)
File
contains (recursive) the child File
.parents
- The parent File
.child
- The child File
to check for containment in parents.true
child is contained (recursive) in at least one parent, false
child is not contained in any parent.public static boolean contains(java.io.File parent, java.io.File child)
File
contains (recursive) the child File
.parent
- The parent File
.child
- The child File
to check for containment in parent.true
child is contained (recursive) in parent, false
child is not contained in parent.public static boolean copy(java.io.InputStream source, java.io.OutputStream target) throws java.io.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.java.io.IOException
- Occurred Exception.public static boolean exists(java.io.File file)
File
exists.file
- The File
to check.true
File
is not null
and exists, false
otherwise.public static final java.net.URL getClassLocationURL(java.lang.Class<?> classInstance)
public static final java.io.File getClassLocation(java.lang.Class<?> classInstance)
public static final java.io.File getProjectRoot(java.lang.Class<?> classInstance)
public static java.io.File toFile(java.net.URL url)
public static java.lang.String toFileString(java.net.URL url)
public static java.net.URI toURI(java.net.URL url)
public static java.io.File getCurrentDirectory()
public static java.io.File getTempDirectory()
public static java.lang.String validateOSIndependentFileName(java.lang.String name)
'_'
.segment
- The segment to validate.