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 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 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 String |
PATH_SEP
System.getProperty("path.separator") for convenience
|
Constructor and Description |
---|
IOUtil() |
Modifier and Type | Method and Description |
---|---|
static CharBuffer |
arrayToCharBuffer(byte[] array)
Convenience method that delegates to
arrayToCharBuffer(byte[],
String) using UTF-8 encoding by default. |
static CharBuffer |
arrayToCharBuffer(byte[] array,
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 BufferedReader |
bufferReader(Reader in) |
static BufferedInputStream |
bufferStream(InputStream in)
Buffers and returns the specified InputStream, if it is not already buffered.
|
static BufferedOutputStream |
bufferStream(OutputStream out)
Buffers and returns the specified OutputStream, if it is not already buffered.
|
static BufferedWriter |
bufferWriter(Writer out) |
static boolean |
copyDirectory(File source,
File dest)
Calls
copyFile(File, File, int) with the
DIRCOPY_ADD_OR_OVERWRITE flag. |
static boolean |
copyDirectory(File source,
File dest,
int copyflag)
Copies the source directory and all it's contents to the specified
destination directory.
|
static boolean |
copyFile(File source,
File dest)
Copies source file (not directory) to destination.
|
static boolean |
copyFile(File source,
File dest,
int copyflag)
Copies the source file (not directory) to the specified
destination file or directory.
|
static void |
copyStream(InputStream in,
OutputStream out)
Copies all data from the specific input stream to the specified output stream.
|
static boolean |
deepDelete(File f)
This method recursively removes a specified file or recursively
removes a specified directory.
|
static long |
dirSize(File dir)
Gets the total size for a directory and all of it's contents.
|
static String |
fileSizeToString(long filesize)
Converts the specified file size into a human readable description.
|
static String |
fileSizeToString(long filesize,
int length)
Calls
fileSizeToString(long) and truncates the
size to fit in the specified number of digits. |
static byte[] |
fileToByteArray(File file)
Returns the contents of an entire file as a byte[].
|
static byte[] |
fileToByteArray(String filename)
Returns the contents of an entire file as a byte[].
|
static ByteBuffer |
fileToByteBuffer(File file)
Returns the contents of an entire file as a ByteBuffer backed by mapping the
file to memory.
|
static char[] |
fileToCharArray(File file,
String encoding)
Returns the contents of an entire file as a char[].
|
static List |
fileToLines(File file)
Returns the contents of an entire file as a List of individual lines.
|
static List |
fileToLines(File file,
boolean trim,
String comment_chars)
Returns the contents of an entire file as a List of individual lines.
|
static List |
fileToLines(InputStream in)
Returns the contents of an entire file as a List of individual lines.
|
static List |
fileToLines(InputStream instream,
boolean trim,
String comment_chars)
Returns the contents of an entire file as a List of individual lines.
|
static List |
fileToLines(String filename)
Returns the contents of an entire file as a List of individual lines.
|
static String |
fileToString(File file)
Calls
fileToString(String, boolean) with trim being
false (that is, files are not trimmed at their trailing end). |
static String |
fileToString(File file,
boolean trim)
Returns the contents of an entire file as a String.
|
static String |
fileToString(String filename)
Calls
fileToString(String, boolean) with trim being
false (that is, files are not trimmed at their trailing end). |
static String |
fileToString(String filename,
boolean trim)
Returns the contents of an entire file as a String.
|
static InputStream |
getClassResource(Class clazz,
String resource_name)
Get the InputStream for the specified resource (in the same directory as from where the specified class was loaded).
|
static String |
getDefaultEncoding()
Returns the default encoding used by the current platform.
|
static byte[] |
inputStreamToByteArray(InputStream in)
Calls inputStreamToByteArray(in, true)
|
static byte[] |
inputStreamToByteArray(InputStream in,
boolean block)
Reads the entire InputStream and returns all read data as a
byte[].
|
static String |
inputStreamToString(InputStream in)
Calls inputStreamToString(in, true)
|
static String |
inputStreamToString(InputStream in,
boolean block)
Reads the entire InputStream and returns all read data as a String, using UTF-8
encoding.
|
static String |
inputStreamToString(InputStream in,
String encoding)
Reads the entire InputStream and returns all read data as a
String (using the specified platform encoding).
|
static void |
main(String[] args)
Usage:
java IOUtil args where args are:
-file full-path-to-file
[for fileToString and other tests]
|
static String |
propertiesToString(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(Reader reader) |
static char[] |
readerToCharArray(Reader reader,
int buffer_size) |
static String |
sha1hash(byte[] buf)
Returns the SHA-1 hash of the specified byte buffer.
|
static void |
sortByFileName(List c)
Alphabetizes the specified list by the filename.
|
static String |
stackTrace()
Convenience method that returns the current execution stack trace as a String.
|
static long |
stringToFileSize(String str)
Converts a string file size into a number denoting the equivalent bytes.
|
static String |
throwableToString(Throwable e)
Convenience method to print the stack trace of an Exception (or Throwable)
to a String (using the default platform encoding).
|
static PrintStream |
toPrintStream(OutputStream out) |
static PrintWriter |
toPrintWriter(Writer out) |
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(File source, File dest) throws FileNotFoundException, IOException
file
- the source file to be copied, a java.io.Filedest
- the destination file or directory.FileNotFoundException
IOException
copyFile(File, File, int)
public static boolean copyFile(File source, File dest, int copyflag) throws FileNotFoundException, 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
FileNotFoundException
IOException
public static boolean copyDirectory(File source, File dest) throws FileNotFoundException, IOException
copyFile(File, File, int)
with the
DIRCOPY_ADD_OR_OVERWRITE
flag.file
- the source directory to be copieddest
- the destination directory.FileNotFoundException
IOException
copyFile(File, File, int)
public static boolean copyDirectory(File source, File dest, int copyflag) throws 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
IOException
public static void copyStream(InputStream in, OutputStream out) throws IOException
in
- the InputStreamout
- the OutputStreamIOException
public static void sortByFileName(List c)
String.compareTo(String)
implementation
of String.list
- the list to be sortedpublic static boolean deepDelete(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(File dir)
dir
- the target dirpublic static BufferedInputStream bufferStream(InputStream in)
in
- the input stream to be bufferedpublic static BufferedOutputStream bufferStream(OutputStream out)
out
- the output stream to be bufferedpublic static BufferedReader bufferReader(Reader in)
public static PrintStream toPrintStream(OutputStream out)
public static PrintWriter toPrintWriter(Writer out)
public static BufferedWriter bufferWriter(Writer out)
public static String propertiesToString(Properties props)
public static String getDefaultEncoding()
public static List fileToLines(InputStream instream, boolean trim, String comment_chars) throws 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 #|//)IOException
public static List fileToLines(File file, boolean trim, String comment_chars) throws 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 #|//)IOException
public static List fileToLines(File file) throws IOException
file
- the file to be read ("UTF-8" encoding is used)IOException
public static List fileToLines(String filename) throws IOException
filename
- the file to be read ("UTF-8" encoding is used)IOException
public static List fileToLines(InputStream in) throws IOException
in
- the input stream to be readIOException
public static String fileToString(File file, boolean trim) throws 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.IOException
public static String fileToString(String filename, boolean trim) throws 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.IOException
public static String fileToString(String filename) throws 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 readIOException
public static String fileToString(File file) throws IOException
fileToString(String, boolean)
with trim being
false (that is, files are not trimmed at their trailing end).file
- the file to be readIOException
public static byte[] fileToByteArray(String filename) throws 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 readIOException
public static byte[] fileToByteArray(File file) throws 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 readIOException
public static ByteBuffer fileToByteBuffer(File file) throws 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 readIOException
public static char[] fileToCharArray(File file, String encoding) throws 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.IOException
public static char[] readerToCharArray(Reader reader) throws IOException
IOException
public static char[] readerToCharArray(Reader reader, int buffer_size) throws IOException
IOException
public static CharBuffer arrayToCharBuffer(byte[] array, 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 CharBuffer arrayToCharBuffer(byte[] array)
arrayToCharBuffer(byte[],
String)
using UTF-8 encoding by default.public static byte[] inputStreamToByteArray(InputStream in, boolean block) throws 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.NegativeArraySizeException
- if the specified input stream returns
a negative number for available()IOException
public static byte[] inputStreamToByteArray(InputStream in) throws IOException
IOException
public static String inputStreamToString(InputStream in, boolean block) throws 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.NegativeArraySizeException
- if the specified input stream returns
a negative number for available()IOException
public static String inputStreamToString(InputStream in, String encoding) throws 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 charsIOException
public static String inputStreamToString(InputStream in) throws IOException
IOException
public static String throwableToString(Throwable e)
public static String stackTrace()
public static 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 String fileSizeToString(long filesize)
filesize
- the size of the file in bytespublic static long stringToFileSize(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 String sha1hash(byte[] buf) throws 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.
NoSuchAlgorithmException
public static InputStream getClassResource(Class clazz, String resource_name) throws IOException
IOException