public final class IOUtil extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
DIRCOPY_ADD_OR_OVERWRITE
Copies the existing directory and overwrites any files with
the same name in the destination directory.
|
static int |
DIRCOPY_DELETE_AND_WRITE
Copies the existing directory.
|
static int |
DIRCOPY_NO_OVERWRITE
Ignores a directory copy command if the destination directory already
exists.
|
static java.lang.String |
FILE_SEP
System.getProperty("file.separator") for convenience
|
static int |
FILECOPY_NO_OVERWRITE |
static int |
FILECOPY_OVERWRITE |
static long |
FOUR_GB
number of bytes equal to 2^32
|
static java.lang.String |
LINE_SEP
System.getProperty("line.separator") for convenience
|
static int |
ONE_GB
number of bytes contained in a gigabyte.
|
static int |
ONE_KB
number of bytes contained in a kilobyte
|
static int |
ONE_MB
number of bytes contained in a megabyte.
|
static java.lang.String |
PATH_SEP
System.getProperty("path.separator") for convenience
|
Constructor and Description |
---|
IOUtil() |
Modifier and Type | Method and Description |
---|---|
static java.nio.CharBuffer |
arrayToCharBuffer(byte[] array)
Convenience method that delegates to
arrayToCharBuffer(byte[],
String) using UTF-8 encoding by default. |
static java.nio.CharBuffer |
arrayToCharBuffer(byte[] array,
java.lang.String encoding)
Converts the specified byte array into a CharBuffer using the specified
encoding.
|
static void |
beep()
Beeps by writing the beep control code to System.out
|
static java.io.BufferedReader |
bufferReader(java.io.Reader in) |
static java.io.BufferedInputStream |
bufferStream(java.io.InputStream in)
Buffers and returns the specified InputStream, if it is not already buffered.
|
static java.io.BufferedOutputStream |
bufferStream(java.io.OutputStream out)
Buffers and returns the specified OutputStream, if it is not already buffered.
|
static java.io.BufferedWriter |
bufferWriter(java.io.Writer out) |
static boolean |
copyDirectory(java.io.File source,
java.io.File dest)
Calls
copyFile(File, File, int) with the
DIRCOPY_ADD_OR_OVERWRITE flag. |
static boolean |
copyDirectory(java.io.File source,
java.io.File dest,
int copyflag)
Copies the source directory and all it's contents to the specified
destination directory.
|
static boolean |
copyFile(java.io.File source,
java.io.File dest)
Copies source file (not directory) to destination.
|
static boolean |
copyFile(java.io.File source,
java.io.File dest,
int copyflag)
Copies the source file (not directory) to the specified
destination file or directory.
|
static void |
copyStream(java.io.InputStream in,
java.io.OutputStream out)
Copies all data from the specific input stream to the specified output stream.
|
static boolean |
deepDelete(java.io.File f)
This method recursively removes a specified file or recursively
removes a specified directory.
|
static long |
dirSize(java.io.File dir)
Gets the total size for a directory and all of it's contents.
|
static java.lang.String |
fileSizeToString(long filesize)
Converts the specified file size into a human readable description.
|
static java.lang.String |
fileSizeToString(long filesize,
int length)
Calls
fileSizeToString(long) and truncates the
size to fit in the specified number of digits. |
static byte[] |
fileToByteArray(java.io.File file)
Returns the contents of an entire file as a byte[].
|
static byte[] |
fileToByteArray(java.lang.String filename)
Returns the contents of an entire file as a byte[].
|
static java.nio.ByteBuffer |
fileToByteBuffer(java.io.File file)
Returns the contents of an entire file as a ByteBuffer backed by mapping the
file to memory.
|
static char[] |
fileToCharArray(java.io.File file,
java.lang.String encoding)
Returns the contents of an entire file as a char[].
|
static java.util.List |
fileToLines(java.io.File file)
Returns the contents of an entire file as a List of individual lines.
|
static java.util.List |
fileToLines(java.io.File file,
boolean trim,
java.lang.String comment_chars)
Returns the contents of an entire file as a List of individual lines.
|
static java.util.List |
fileToLines(java.io.InputStream in)
Returns the contents of an entire file as a List of individual lines.
|
static java.util.List |
fileToLines(java.io.InputStream instream,
boolean trim,
java.lang.String comment_chars)
Returns the contents of an entire file as a List of individual lines.
|
static java.util.List |
fileToLines(java.lang.String filename)
Returns the contents of an entire file as a List of individual lines.
|
static java.lang.String |
fileToString(java.io.File file)
Calls
fileToString(String, boolean) with trim being
false (that is, files are not trimmed at their trailing end). |
static java.lang.String |
fileToString(java.io.File file,
boolean trim)
Returns the contents of an entire file as a String.
|
static java.lang.String |
fileToString(java.lang.String filename)
Calls
fileToString(String, boolean) with trim being
false (that is, files are not trimmed at their trailing end). |
static java.lang.String |
fileToString(java.lang.String filename,
boolean trim)
Returns the contents of an entire file as a String.
|
static java.io.InputStream |
getClassResource(java.lang.Class clazz,
java.lang.String resource_name)
Get the InputStream for the specified resource (in the same directory as from where the specified class was loaded).
|
static java.lang.String |
getDefaultEncoding()
Returns the default encoding used by the current platform.
|
static byte[] |
inputStreamToByteArray(java.io.InputStream in)
Calls inputStreamToByteArray(in, true)
|
static byte[] |
inputStreamToByteArray(java.io.InputStream in,
boolean block)
Reads the entire InputStream and returns all read data as a
byte[].
|
static java.lang.String |
inputStreamToString(java.io.InputStream in)
Calls inputStreamToString(in, true)
|
static java.lang.String |
inputStreamToString(java.io.InputStream in,
boolean block)
Reads the entire InputStream and returns all read data as a
String (using the default platform encoding).
|
static java.lang.String |
inputStreamToString(java.io.InputStream in,
java.lang.String encoding)
Reads the entire InputStream and returns all read data as a
String (using the specified platform encoding).
|
static void |
main(java.lang.String[] args)
Usage:
java IOUtil args where args are:
-file full-path-to-file
[for fileToString and other tests]
|
static java.lang.String |
propertiesToString(java.util.Properties props)
Convenience method to print the contents of a java.util.Property object
to a String (using the default platform encoding).
|
static char[] |
readerToCharArray(java.io.Reader reader) |
static java.lang.String |
sha1hash(byte[] buf)
Returns the SHA-1 hash of the specified byte buffer.
|
static void |
sortByFileName(java.util.List c)
Alphabetizes the specified list by the filename.
|
static java.lang.String |
stackTrace()
Convenience method that returns the current execution stack trace as a String.
|
static long |
stringToFileSize(java.lang.String str)
Converts a string file size into a number denoting the equivalent bytes.
|
static java.lang.String |
throwableToString(java.lang.Throwable e)
Convenience method to print the stack trace of an Exception (or Throwable)
to a String (using the default platform encoding).
|
static java.io.PrintStream |
toPrintStream(java.io.OutputStream out) |
static java.io.PrintWriter |
toPrintWriter(java.io.Writer out) |
public static final java.lang.String FILE_SEP
public static final java.lang.String PATH_SEP
public static final java.lang.String LINE_SEP
public static final int FILECOPY_OVERWRITE
public static final int FILECOPY_NO_OVERWRITE
public static final int DIRCOPY_NO_OVERWRITE
public static final int DIRCOPY_ADD_OR_OVERWRITE
public static final int DIRCOPY_DELETE_AND_WRITE
public static final int ONE_KB
public static final int ONE_MB
public static final int ONE_GB
public static final long FOUR_GB
public IOUtil()
public static void beep()
public static boolean copyFile(java.io.File source, java.io.File dest) throws java.io.FileNotFoundException, java.io.IOException
file
- the source file to be copied, a java.io.Filedest
- the destination file or directory.java.io.FileNotFoundException
java.io.IOException
copyFile(File, File, int)
public static boolean copyFile(java.io.File source, java.io.File dest, int copyflag) throws java.io.FileNotFoundException, java.io.IOException
FILECOPY_OVERWRITE
or FILECOPY_NO_OVERWRITE
flags.
If a file is specified as the destination, then the source file
is copied to that file. If the specified file exists already
then specify the appropriate overwrite flag.
Try to use absolute path names for files and directories. Relative path names can be relative either to user.dir or where the jvm was invoked or some platform/jvm dependent place, so don't rely on relative paths. Copying, moving or working with files is tricky in java. Similar behavior is not defined for all platforms. For example, in WindowsNT/java1.2, aliases cannot be opened/resolved and hence cannot be copied via FileInput/Output streams. This sort of thing is best left to JNI calls native code.
This method returns true if the directory was copied successfully, false otherwise. (for example, false will be returned when the copy mode is not to overwrite but the target file already exists).
source
- the source file (not directory) to be copied, a java.io.Filedest
- the destination file or directory.copyflag
- the file copy mode. FILECOPY_OVERWRITE
, FILECOPY_NO_OVERWRITE
java.io.FileNotFoundException
java.io.IOException
public static boolean copyDirectory(java.io.File source, java.io.File dest) throws java.io.FileNotFoundException, java.io.IOException
copyFile(File, File, int)
with the
DIRCOPY_ADD_OR_OVERWRITE
flag.file
- the source directory to be copieddest
- the destination directory.java.io.FileNotFoundException
java.io.IOException
copyFile(File, File, int)
public static boolean copyDirectory(java.io.File source, java.io.File dest, int copyflag) throws java.io.IOException
To handle cases where the destination directory already exists
use the appropriate DIRCOPY_NO_OVERWRITE
DIRCOPY_DELETE_AND_WRITE
and
DIRCOPY_ADD_OR_OVERWRITE
flags.
Try to use absolute path names for files and directories. Relative path names can be relative either to user.dir or where the jvm was invoked or some platform/jvm dependent place, so don't rely on relative paths. Copying, moving or working with files is tricky in java. Similar behavior is not defined for all platforms. For example, in WindowsNT/java1.2, aliases cannot be opened/resolved and hence cannot be copied via FileInput/Output streams. This sort of thing is best left to JNI calls to POSIX or to platform specific code.
This method returns true if the directory was copied successfully, false otherwise. (for example, false will be returned when the copy mode is not to overwrite but the target directory already exists).
source
- the source directory (not file) to be copied, a java.io.Filedest
- the destination file or directory.copyflag
- the dir copy mode. DIRCOPY_NO_OVERWRITE
, DIRCOPY_ADD_OR_OVERWRITE
java.io.IOException
public static void copyStream(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
in
- the InputStreamout
- the OutputStreamjava.io.IOException
public static void sortByFileName(java.util.List c)
String.compareTo(String)
implementation
of String.list
- the list to be sortedpublic static boolean deepDelete(java.io.File f)
File.delete()
lacks the ability to delete a directory if the
directory is not empty.
Internally, this method delegates to File.delete()
, so if
File.delete()
follows sym links, then so will this method.
Be careful !
file
- the file or directory to be removedpublic static long dirSize(java.io.File dir)
dir
- the target dirpublic static java.io.BufferedInputStream bufferStream(java.io.InputStream in)
in
- the input stream to be bufferedpublic static java.io.BufferedOutputStream bufferStream(java.io.OutputStream out)
out
- the output stream to be bufferedpublic static java.io.BufferedReader bufferReader(java.io.Reader in)
public static java.io.PrintStream toPrintStream(java.io.OutputStream out)
public static java.io.PrintWriter toPrintWriter(java.io.Writer out)
public static java.io.BufferedWriter bufferWriter(java.io.Writer out)
public static java.lang.String propertiesToString(java.util.Properties props)
public static java.lang.String getDefaultEncoding()
public static java.util.List fileToLines(java.io.InputStream instream, boolean trim, java.lang.String comment_chars) throws java.io.IOException
instream
- the stream to be readtrim
- if true, any leading or trailing blank lines are trimmed are ignored.comment_chars
- Regex of comment chars. Any lines that start with this (or have leading spaces
and then start with this) are ignored. (example: # or #|//)java.io.IOException
public static java.util.List fileToLines(java.io.File file, boolean trim, java.lang.String comment_chars) throws java.io.IOException
File
- the file to be read ("UTF-8" encoding is used)trim
- if true, any leading or trailing blank lines are trimmed are ignored.comment_chars
- Regex of comment chars. Any lines that start with this (or have leading spaces
and then start with this) are ignored. (example: # or #|//)java.io.IOException
public static java.util.List fileToLines(java.io.File file) throws java.io.IOException
file
- the file to be read ("UTF-8" encoding is used)java.io.IOException
public static java.util.List fileToLines(java.lang.String filename) throws java.io.IOException
filename
- the file to be read ("UTF-8" encoding is used)java.io.IOException
public static java.util.List fileToLines(java.io.InputStream in) throws java.io.IOException
in
- the input stream to be readjava.io.IOException
public static java.lang.String fileToString(java.io.File file, boolean trim) throws java.io.IOException
Note 1: Due to jdk1.4 brain damage, this method is limited to files
less than 2^32 bytes. If the specified file is greater than 2^32 bytes,
an IOException will be thrown.
Note 2: The files is converted into a String using an encoding
that is determined programmatically (from the filesystem). This may
not be totally reliable but there is no way around this because JDK 1.4
provides no way to easily get the default platform encoding.
Uses the ISO_8859_1 encoding as a fallback measure, if the
default encoding cannot be determined.
filename
- the file to be readtrim
- if true, any trailing whitespace is trimmed from the file's end.java.io.IOException
public static java.lang.String fileToString(java.lang.String filename, boolean trim) throws java.io.IOException
Note 1: Due to jdk1.4 brain damage, this method is limited to files
less than 2^32 bytes. If the specified file is greater than 2^32 bytes,
an IOException will be thrown.
Note 2: The files is converted into a String using an encoding
that is determined programmatically (from the filesystem). This may
not be totally reliable but there is no way around this because JDK 1.4
provides no way to easily get the default platform encoding.
Uses the ISO_8859_1 encoding as a fallback measure, if the
default encoding cannot be determined.
file
- the absolute path to the file name to be readtrim
- if true, any trailing whitespace is trimmed from the file's end.java.io.IOException
public static java.lang.String fileToString(java.lang.String filename) throws java.io.IOException
fileToString(String, boolean)
with trim being
false (that is, files are not trimmed at their trailing end).filename
- the absolute path to the file name to be readjava.io.IOException
public static java.lang.String fileToString(java.io.File file) throws java.io.IOException
fileToString(String, boolean)
with trim being
false (that is, files are not trimmed at their trailing end).file
- the file to be readjava.io.IOException
public static byte[] fileToByteArray(java.lang.String filename) throws java.io.IOException
Note 1: Since java arrays cannot be greater than 2^32 elements, this method is limited to files less than or equal to 2^32 bytes. If the specified file is greater than 2^32 bytes, an IOException will be thrown.
filename
- the absolute path to the file name to be readjava.io.IOException
public static byte[] fileToByteArray(java.io.File file) throws java.io.IOException
Note 1: Since java arrays cannot be greater than 2^32 elements, this method is limited to files less than or equal to 2^32 bytes. If the specified file is greater than 2^32 bytes, an IOException will be thrown.
file
- the file to be readjava.io.IOException
public static java.nio.ByteBuffer fileToByteBuffer(java.io.File file) throws java.io.IOException
MappedByteBuffer
documentation about concurrent modification or
deletion of files that are mapped into memory.
The ByteBuffer returned by this method will have Buffer.rewind()
called on it before it is returned.
Note 1: This method is limited to files less than 2^32 bytes, since ByteBuffers cannot be greater than this size. If the specified file is greater than 2^32 bytes, an IOException will be thrown.
file
- the file to be readjava.io.IOException
public static char[] fileToCharArray(java.io.File file, java.lang.String encoding) throws java.io.IOException
Note 1: Since java arrays cannot be greater than 2^32 elements, this method is limited to files less than or equal to 2^32 bytes. If the specified file is greater than 2^32 bytes, an IOException will be thrown.
file
- the file to be readencoding
- the name of the character encoding to use.
Specify null to use UTF-8 encoding.java.io.IOException
public static char[] readerToCharArray(java.io.Reader reader) throws java.io.IOException
java.io.IOException
public static java.nio.CharBuffer arrayToCharBuffer(byte[] array, java.lang.String encoding)
This method returns null if the specified array is null or if the specified encoding is null.
array
- the array to convertencoding
- the charset
encoding to use to
convert bytes into charspublic static java.nio.CharBuffer arrayToCharBuffer(byte[] array)
arrayToCharBuffer(byte[],
String)
using UTF-8 encoding by default.public static byte[] inputStreamToByteArray(java.io.InputStream in, boolean block) throws java.io.IOException
in
- the InputStream to readblock
- if true, this method will block until all
available data from the specified input stream
has been read. If false, this method will
read and return as much data as currently is available
is read without blocking. The available amount is
that returned by the available() method of the specified
input stream.java.lang.NegativeArraySizeException
- if the specified input stream returns
a negative number for available()java.io.IOException
public static byte[] inputStreamToByteArray(java.io.InputStream in) throws java.io.IOException
java.io.IOException
public static java.lang.String inputStreamToString(java.io.InputStream in, boolean block) throws java.io.IOException
in
- the InputStream to readblock
- if true, this method will block until all
available data from the specified input stream
has been read. If false, this method will
read and return as much data as currently is available
is read without blocking. The available amount is
that returned by the specified input stream.java.lang.NegativeArraySizeException
- if the specified input stream returns
a negative number for available()java.io.IOException
public static java.lang.String inputStreamToString(java.io.InputStream in, java.lang.String encoding) throws java.io.IOException
This method will block until all available data from the specified input stream has been read.
in
- the input stream to readencoding
- the Charset
encoding name to use to
convert bytes into charsjava.io.IOException
public static java.lang.String inputStreamToString(java.io.InputStream in) throws java.io.IOException
java.io.IOException
public static java.lang.String throwableToString(java.lang.Throwable e)
public static java.lang.String stackTrace()
public static java.lang.String fileSizeToString(long filesize, int length)
fileSizeToString(long)
and truncates the
size to fit in the specified number of digits. For example,
a file size of 4.455 KB and a length of 2 will
return 4.45 KBfilesize
- the size of the file in byteslength
- the max number of digits after
the decimal pointpublic static java.lang.String fileSizeToString(long filesize)
filesize
- the size of the file in bytespublic static long stringToFileSize(java.lang.String str)
34K, 34KB --> 34 bytes 34M, 34megabytes --> 34 * 1024 bytesAllows numbers to end with k..., m..., g...., b.... or no suffix at all. Suffixes are case insensitive.
public static final java.lang.String sha1hash(byte[] buf) throws java.security.NoSuchAlgorithmException
This method is not thread safe (as far as I can tell, since it uses
MessageDigest.getInstance
, but if that is thread safe, then
this method is thread safe). The invoker should invoke it in a thread safe way.
java.security.NoSuchAlgorithmException
public static java.io.InputStream getClassResource(java.lang.Class clazz, java.lang.String resource_name) throws java.io.IOException
java.io.IOException
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception