|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectfc.io.IOUtil
public class IOUtil
Misc. IO utilities
| Field Summary | |
|---|---|
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 Summary | |
|---|---|
IOUtil()
|
|
| Method Summary | |
|---|---|
static CharBuffer |
arrayToCharBuffer(byte[] array)
Convenience method that delegates to arrayToCharBuffer(byte[],
String) using the platform's default encoding. |
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 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 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 the default platform encoding). |
static void |
main(String[] args)
Usage: java IOUtil args where args are: -fileToString full-path-to-file [to convert to String (for fileToString() method)] |
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 void |
sortByFileName(List c)
Alphabetizes the specified list by the filename. |
static String |
stackTrace()
Convenience method that returns the current 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)
|
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final String FILE_SEP
public static final String PATH_SEP
public static final 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
| Constructor Detail |
|---|
public IOUtil()
| Method Detail |
|---|
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
IOExceptioncopyFile(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
IOExceptioncopyFile(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 OutputStream
IOExceptionpublic 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 removed
public static long dirSize(File dir)
dir - the target dir
public static BufferedInputStream bufferStream(InputStream in)
in - the input stream to be buffered
public static BufferedOutputStream bufferStream(OutputStream out)
out - the output stream to be buffered
public 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 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 fallaback measure, if the
default encoding cannot be determined.
filename - 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 read
IOException
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 read
IOException
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 read
IOException
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 read
IOException
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 the
default encoding.
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 encoding to use to convert bytes into charspublic static CharBuffer arrayToCharBuffer(byte[] array)
arrayToCharBuffer(byte[],
String) using the platform's default encoding.
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 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)
throws IOException
IOExceptionpublic 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 KB
filesize - 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 void main(String[] args)
throws Exception
Exception
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||