public final class FileUtils extends java.lang.Object
Modified by: Joseph A. Huwaldt
Modifier and Type | Method and Description |
---|---|
static void |
buffer2File(byte[] buffer,
java.io.File file)
Write the entire contents of a byte array to the specified file.
|
static void |
byteBuffer2File(java.nio.ByteBuffer buffer,
java.io.File file)
Write the contents of a ByteBuffer, from the beginning up to the current position,
to the specified file.
|
static java.lang.String |
cleanFileName(java.lang.String name)
Replace any potentially illegal characters from a file name with '_'.
|
static void |
copy(java.io.File src,
java.io.File dst)
Copy a file from the source to the destination locations.
|
static void |
copy(java.io.InputStream in,
java.io.OutputStream out)
Copy the input stream to the output stream.
|
static void |
copyAndGzip(java.io.File src,
java.io.File destDir)
Copy a file to the specified destination directory while GZIP compressing the file.
|
static void |
copyAndUngzip(java.io.File src,
java.io.File destDir)
Copy a file to the specified destination directory while decompressing the GZIP
file.
|
static void |
copyDirectory(java.io.File source,
java.io.File destination)
Recursively copy the contents of an entire directory tree from source to
destination.
|
static java.io.File |
createTempDirectory(java.lang.String prefix)
Create a temporary directory using the specified prefix.
|
static boolean |
deleteDirectory(java.io.File path)
Recursively deletes the directory tree indicated by the specified path.
|
static byte[] |
file2Buffer(java.io.File file)
Returns a buffer that contains the contents of the specified file.
|
static java.nio.ByteBuffer |
file2ByteBuffer(java.io.File file)
Returns a ByteBuffer that contains the contents of the specified file.
|
static java.lang.String |
getExtension(java.io.File file)
Return the extension portion of the file's name.
|
static java.lang.String |
getExtension(java.lang.String name)
Return the extension portion of the file's name.
|
static java.lang.String |
getFileNameWithoutExtension(java.io.File file)
Return the file name of the specified file without the extension.
|
static java.lang.String |
getFileNameWithoutExtension(java.lang.String name)
Return the file name of the specified file without the extension.
|
static void |
gzip(java.io.File src,
java.io.File dst)
GZIP compress the src file writing to the destination file.
|
static void |
gzip(java.io.InputStream in,
java.io.OutputStream out)
GZIP compress the input stream and write it to the output stream.
|
static boolean |
hasIllegalChars(java.lang.String name)
Returns true if the supplied file name contains characters that are illegal on some
file systems.
|
static boolean |
isGZIPCompressed(java.io.BufferedInputStream in)
Returns
true if the specified input stream is pointing at a GZIP
compressed data set. |
static boolean |
isGZIPCompressed(byte[] bytes)
Returns
true if the specified array of bytes represent a GZIP
compressed data set. |
static boolean |
isGZIPCompressed(java.io.File file)
Returns
true if the specified input file is pointing at a GZIP
compressed data set. |
static double[][] |
loadtxt(java.io.File txtFile)
Read in an optionally GZIP compressed, delimited text file that contains a regular
array of Double values.
|
static double[][] |
loadtxt(java.io.File txtFile,
int skiprows)
Read in an optionally GZIP compressed, delimited text file that contains a regular
array of Double values.
|
static double[][] |
loadtxt(java.io.File txtFile,
java.lang.String delimiter,
int skiprows)
Read in an optionally GZIP compressed, delimited text file that contains a regular
array of Double values.
|
static double[][] |
loadtxt(java.lang.String filePath)
Read in an optionally GZIP compressed, delimited text file that contains a regular
array of Double values.
|
static double[][] |
loadtxt(java.lang.String filePath,
int skiprows)
Read in an optionally GZIP compressed, delimited text file that contains a regular
array of Double values.
|
static double[][] |
loadtxt(java.lang.String filePath,
java.lang.String delimiter,
int skiprows)
Read in an optionally GZIP compressed, delimited text file that contains a regular
array of Double values.
|
static java.util.List<java.lang.String> |
readlines(java.io.File file)
Returns a list of String objects each of which represents a line in the specified
file.
|
static java.util.List<java.lang.String> |
readlines(java.io.InputStream instream)
Returns a list of String objects each of which represents a line in the specified
input stream.
|
static void |
rename(java.io.File src,
java.io.File dst)
Attempt to rename a file from the source File location to the destination File
location.
|
static boolean |
sameFile(java.io.File f1,
java.io.File f2)
Returns
true if and only if the two File objects refer to the same
file in the file system. |
static void |
ungzip(java.io.File src,
java.io.File dst)
Un-GZIP the compressed src file writing the uncompressed data to the destination
file.
|
static void |
ungzip(java.io.InputStream in,
java.io.OutputStream out)
Un-GZIP the compressed input stream and write the uncompressed data to the
specified output stream.
|
static void |
writelines(java.io.File outFile,
java.util.List<java.lang.String> lines)
Write out a list of String objects which represent a line to the specified output
stream.
|
static void |
writelines(java.io.OutputStream outstream,
java.util.List<java.lang.String> lines)
Write out a list of String objects which represent a line to the specified output
stream.
|
public static java.lang.String cleanFileName(java.lang.String name)
name
- The file name to be cleaned of potentially illegal characters. May not
be null.public static boolean hasIllegalChars(java.lang.String name)
name
- The file name to be checked for potentially illegal characters. May not
be null.public static java.lang.String getFileNameWithoutExtension(java.io.File file)
file
- The file to have the name without extension returned. May not be null.public static java.lang.String getFileNameWithoutExtension(java.lang.String name)
name
- The file name to have the name without extension returned. May not be
null.public static java.lang.String getExtension(java.io.File file)
file
- The file for which the extension is to be returned. May not be null.getExtension(java.lang.String)
public static java.lang.String getExtension(java.lang.String name)
name
- The name of the file, including the extension. May not be null.getExtension(java.io.File)
public static void copy(java.io.File src, java.io.File dst) throws java.io.IOException
src
- The source file. May not be null.dst
- The destination file to copy the source file to. May not be null.java.io.IOException
- if there is any problem reading from or writing to the
files.public static void copy(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
in
- The source input stream. May not be null.out
- The destination output stream. May not be null.java.io.IOException
- if there is any problem reading from or writing to the
streams.public static void copyDirectory(java.io.File source, java.io.File destination) throws java.io.IOException
source
- The source directory or file to copy. May not be null.destination
- The destination directory or file. May not be null.java.io.IOException
- If there is any problem copying the directories or
files.public static void rename(java.io.File src, java.io.File dst) throws java.io.IOException
src
- The source file to be renamed. Upon exit, this object MAY point to the
same location as "dst" or it may be left unchanged. Either way, barring
an exception, the file will no longer exist at the input src path
location. May not be null.dst
- The destination path to rename the source file to. May not be null.java.io.IOException
- if there is any problem renaming the file.public static java.util.List<java.lang.String> readlines(java.io.File file) throws java.io.IOException
file
- The possibly GZIP compressed file to be read in. May not be null.java.io.IOException
- if there is any problem reading from the file.public static java.util.List<java.lang.String> readlines(java.io.InputStream instream) throws java.io.IOException
instream
- The input stream to be read in. May optionally be GZIP compressed.
May not be null.java.io.IOException
- if there is any problem reading from the stream.public static void writelines(java.io.File outFile, java.util.List<java.lang.String> lines) throws java.io.IOException
outFile
- The output file to be written to. May not be null.lines
- The list of String objects to be written out. May not be null.java.io.IOException
- if there is any problem writing to the output file.public static void writelines(java.io.OutputStream outstream, java.util.List<java.lang.String> lines) throws java.io.IOException
outstream
- The output stream to be written to. May not be null.lines
- The list of String objects to be written out. May not be null.java.io.IOException
- if there is any problem writing to the output stream.public static byte[] file2Buffer(java.io.File file) throws java.io.IOException
file
- The file to be read into a new ByteBuffer. May not be null.java.io.IOException
- if there is any problem reading from the file.public static void buffer2File(byte[] buffer, java.io.File file) throws java.io.IOException
buffer
- The buffer to be written to the file. May not be null.file
- The file to write the buffer to (it's existing contents will be
overwritten). May not be null.java.io.IOException
- if there is any problem writing to the file.public static java.nio.ByteBuffer file2ByteBuffer(java.io.File file) throws java.io.IOException
file
- The file to be read into a new ByteBuffer. May not be null.java.io.IOException
- if there is any problem reading from the file.public static void byteBuffer2File(java.nio.ByteBuffer buffer, java.io.File file) throws java.io.IOException
buffer
- The buffer to be written to the file. May not be null.file
- The file to write the buffer to (it's existing contents will be
overwritten). May not be null.java.io.IOException
- if there is any problem writing the byte buffer to the
file.public static double[][] loadtxt(java.lang.String filePath) throws java.io.IOException
filePath
- Path to/Name of the possibly GZIP compressed table file being read
in.java.io.IOException
- if there is any problem reading from the file or parsing the
results.public static double[][] loadtxt(java.lang.String filePath, int skiprows) throws java.io.IOException
filePath
- Path to/Name of the possibly GZIP compressed table file being read
in.skiprows
- Indicates the number of rows to skip at the top of the file (to
skip over a header for instance).java.io.IOException
- if there is any problem reading from the file or parsing the
results.public static double[][] loadtxt(java.lang.String filePath, java.lang.String delimiter, int skiprows) throws java.io.IOException
filePath
- Path to/Name of the possibly GZIP compressed table file being read
in.delimiter
- The delimiter to use to separate columns (such as "," for commas,
or "\\s+" for any whitespace.skiprows
- Indicates the number of rows to skip at the top of the file (to
skip over a header for instance).java.io.IOException
- if there is any problem reading from the file or parsing the
results.public static double[][] loadtxt(java.io.File txtFile) throws java.io.IOException
txtFile
- The possibly GZIP compressed table file being read in.java.io.IOException
- if there is any problem reading from the file or parsing the
results.public static double[][] loadtxt(java.io.File txtFile, int skiprows) throws java.io.IOException
txtFile
- The possibly GZIP compressed table file being read in.skiprows
- Indicates the number of rows to skip at the top of the file (to
skip over a header for instance).java.io.IOException
- if there is any problem reading from the file or parsing the
results.public static double[][] loadtxt(java.io.File txtFile, java.lang.String delimiter, int skiprows) throws java.io.IOException
txtFile
- The possibly GZIP compressed table file being read in.delimiter
- The delimiter to use to separate columns (such as "," for commas,
or "\\s+" for any whitespace.skiprows
- Indicates the number of rows to skip at the top of the file (to
skip over a header for instance).java.io.IOException
- if there is any problem reading from the file or parsing the
results.public static java.io.File createTempDirectory(java.lang.String prefix) throws java.io.IOException
prefix
- The prefix string to be used in generating the file's name; must be
at least three characters long. May not be null.java.io.IOException
- if there is any problem creating the temporary
directory.public static boolean deleteDirectory(java.io.File path)
path
- The directory to be deleted. If a plain file is passed in rather than a
directory, it is simply deleted. May not be null.public static boolean sameFile(java.io.File f1, java.io.File f2) throws java.io.IOException
true
if and only if the two File objects refer to the same
file in the file system.f1
- The first file to check. May not be null.f2
- The 2nd file to check with the 1st one. May not be null.java.io.IOException
- If an I/O error occurs, which is possible because the
construction of the canonical pathname may require file system queries.public static void gzip(java.io.File src, java.io.File dst) throws java.io.IOException
src
- The source file to be compressed. May not be null.dst
- The destination file to compress the source file to. This may be
identical to the source location if the change is to be made in place.
May not be null.java.io.IOException
- if there is any problem reading from or writing to the
files.public static void copyAndGzip(java.io.File src, java.io.File destDir) throws java.io.IOException
src
- The source file to be compressed. May not be null.destDir
- The directory to copy the compressed file into. May not be null.java.io.IOException
- if there is any problem reading from or writing to the
files.public static void ungzip(java.io.File src, java.io.File dst) throws java.io.IOException
src
- The GZIP compressed source file to be de-compressed. May not be null.dst
- The destination file to uncompress the source file to. This may be
identical to the source location if the change is to be made in place.
May not be null.java.io.IOException
- if there is any problem reading from or writing to the
files.public static void copyAndUngzip(java.io.File src, java.io.File destDir) throws java.io.IOException
src
- The source file GZIP file. May not be null.destDir
- The directory to copy the uncompressed file into. May not be null.java.io.IOException
- if there is any problem reading from or writing to the
files.public static void gzip(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
in
- The source input stream. May not be null.out
- The destination output stream where GZIP compressed data is to be
written. May not be null.java.io.IOException
- if there is any problem reading from or writing to the
streams.public static void ungzip(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
in
- The source input stream pointing to GZIP compressed data. May not be
null.out
- The destination output stream where uncompressed data is to be written.
May not be null.java.io.IOException
- if there is any problem reading from or writing to the
streams.public static boolean isGZIPCompressed(java.io.File file) throws java.io.IOException
true
if the specified input file is pointing at a GZIP
compressed data set.file
- The input file to be tested. May not be null.java.io.IOException
- if there is a problem reading from the specified file.public static boolean isGZIPCompressed(java.io.BufferedInputStream in) throws java.io.IOException
true
if the specified input stream is pointing at a GZIP
compressed data set.in
- The input stream to be tested. May not be null.java.io.IOException
- if there is a problem reading from the input stream.public static boolean isGZIPCompressed(byte[] bytes)
true
if the specified array of bytes represent a GZIP
compressed data set.bytes
- the array of bytes to be tested. May not be null.