Package jahuwaldt.io
Class FileUtils
- java.lang.Object
-
- jahuwaldt.io.FileUtils
-
public final class FileUtils extends java.lang.Object
This is a utility class of static methods for working with files.Modified by: Joseph A. Huwaldt
- Version:
- July 7, 2018
- Author:
- Joseph A. Huwaldt, Date: November 27, 2009
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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(byte[] bytes)
Returnstrue
if the specified array of bytes represent a GZIP compressed data set.static boolean
isGZIPCompressed(java.io.BufferedInputStream in)
Returnstrue
if the specified input stream is pointing at a GZIP compressed data set.static boolean
isGZIPCompressed(java.io.File file)
Returnstrue
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)
Returnstrue
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.static void
writelines(java.lang.String outFile, java.util.List<java.lang.String> lines)
Write out a list of String objects which represent a line to the specified output stream.
-
-
-
Method Detail
-
cleanFileName
public static java.lang.String cleanFileName(java.lang.String name)
Replace any potentially illegal characters from a file name with '_'.- Parameters:
name
- The file name to be cleaned of potentially illegal characters. May not be null.- Returns:
- The input file name with potentially illegal characters replaced with "_".
-
hasIllegalChars
public static boolean hasIllegalChars(java.lang.String name)
Returns true if the supplied file name contains characters that are illegal on some file systems.- Parameters:
name
- The file name to be checked for potentially illegal characters. May not be null.- Returns:
- true if the file name contains potentially illegal characters, false if it is safe.
-
getFileNameWithoutExtension
public static java.lang.String getFileNameWithoutExtension(java.io.File file)
Return the file name of the specified file without the extension.- Parameters:
file
- The file to have the name without extension returned. May not be null.- Returns:
- The name of the specified file without the extension (if there is one).
-
getFileNameWithoutExtension
public static java.lang.String getFileNameWithoutExtension(java.lang.String name)
Return the file name of the specified file without the extension.- Parameters:
name
- The file name to have the name without extension returned. May not be null.- Returns:
- The name of the specified file without the extension (if there is one).
-
getExtension
public static java.lang.String getExtension(java.io.File file)
Return the extension portion of the file's name. The extension will always be returned in lower case and without the ".".- Parameters:
file
- The file for which the extension is to be returned. May not be null.- Returns:
- The extension portion of the file's name without the "." or "" if there is no extension.
- See Also:
getExtension(java.lang.String)
-
getExtension
public static java.lang.String getExtension(java.lang.String name)
Return the extension portion of the file's name. The extension will always be returned in lower case and without the ".".- Parameters:
name
- The name of the file, including the extension. May not be null.- Returns:
- The extension portion of the file's name without the "." or "" if there is no extension.
- See Also:
getExtension(java.io.File)
-
copy
public static void copy(java.io.File src, java.io.File dst) throws java.io.IOException
Copy a file from the source to the destination locations.- Parameters:
src
- The source file. May not be null.dst
- The destination file to copy the source file to. May not be null.- Throws:
java.io.IOException
- if there is any problem reading from or writing to the files.
-
copy
public static void copy(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
Copy the input stream to the output stream.- Parameters:
in
- The source input stream. May not be null.out
- The destination output stream. May not be null.- Throws:
java.io.IOException
- if there is any problem reading from or writing to the streams.
-
copyDirectory
public static void copyDirectory(java.io.File source, java.io.File destination) throws java.io.IOException
Recursively copy the contents of an entire directory tree from source to destination. This also works if a single file is passed as the source and destination.- Parameters:
source
- The source directory or file to copy. May not be null.destination
- The destination directory or file. May not be null.- Throws:
java.io.IOException
- If there is any problem copying the directories or files.
-
rename
public static void rename(java.io.File src, java.io.File dst) throws java.io.IOException
Attempt to rename a file from the source File location to the destination File location. If the standard atomic rename fails, this method falls back on copying the file which is dangerous as it is not atomic. The fall back will not work if the source or destination is a directory and an exception is thrown in that case.- Parameters:
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.- Throws:
java.io.IOException
- if there is any problem renaming the file.
-
readlines
public static java.util.List<java.lang.String> readlines(java.io.File file) throws java.io.IOException
Returns a list of String objects each of which represents a line in the specified file. The file may optionally be GZIP compressed.- Parameters:
file
- The possibly GZIP compressed file to be read in. May not be null.- Returns:
- A list of String objects, one for each line in the specified file.
- Throws:
java.io.IOException
- if there is any problem reading from the file.
-
readlines
public static java.util.List<java.lang.String> readlines(java.io.InputStream instream) throws java.io.IOException
Returns a list of String objects each of which represents a line in the specified input stream. The input stream may optionally be GZIP compressed.- Parameters:
instream
- The input stream to be read in. May optionally be GZIP compressed. May not be null.- Returns:
- A list of String objects, one for each line in the specified input stream.
- Throws:
java.io.IOException
- if there is any problem reading from the stream.
-
writelines
public static void writelines(java.lang.String outFile, java.util.List<java.lang.String> lines) throws java.io.IOException
Write out a list of String objects which represent a line to the specified output stream. A line ending character is appended to each line.- Parameters:
outFile
- The path to 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.- Throws:
java.io.IOException
- if there is any problem writing to the output file.
-
writelines
public static void writelines(java.io.File outFile, java.util.List<java.lang.String> lines) throws java.io.IOException
Write out a list of String objects which represent a line to the specified output stream. A line ending character is appended to each line.- Parameters:
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.- Throws:
java.io.IOException
- if there is any problem writing to the output file.
-
writelines
public static void writelines(java.io.OutputStream outstream, java.util.List<java.lang.String> lines) throws java.io.IOException
Write out a list of String objects which represent a line to the specified output stream. A line ending character is appended to each line.- Parameters:
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.- Throws:
java.io.IOException
- if there is any problem writing to the output stream.
-
file2Buffer
public static byte[] file2Buffer(java.io.File file) throws java.io.IOException
Returns a buffer that contains the contents of the specified file.- Parameters:
file
- The file to be read into a new ByteBuffer. May not be null.- Returns:
- The buffer containing the contents of the specified file.
- Throws:
java.io.IOException
- if there is any problem reading from the file.
-
buffer2File
public static void buffer2File(byte[] buffer, java.io.File file) throws java.io.IOException
Write the entire contents of a byte array to the specified file.- Parameters:
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.- Throws:
java.io.IOException
- if there is any problem writing to the file.
-
file2ByteBuffer
public static java.nio.ByteBuffer file2ByteBuffer(java.io.File file) throws java.io.IOException
Returns a ByteBuffer that contains the contents of the specified file.- Parameters:
file
- The file to be read into a new ByteBuffer. May not be null.- Returns:
- The ByteBuffer containing the contents of the specified file.
- Throws:
java.io.IOException
- if there is any problem reading from the file.
-
byteBuffer2File
public static void byteBuffer2File(java.nio.ByteBuffer buffer, java.io.File file) throws java.io.IOException
Write the contents of a ByteBuffer, from the beginning up to the current position, to the specified file.- Parameters:
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.- Throws:
java.io.IOException
- if there is any problem writing the byte buffer to the file.
-
loadtxt
public static double[][] loadtxt(java.lang.String filePath) throws java.io.IOException
Read in an optionally GZIP compressed, delimited text file that contains a regular array of Double values. The delimiter may be any whitespace.- Parameters:
filePath
- Path to/Name of the possibly GZIP compressed table file being read in.- Returns:
- A 2D double array of the values in the table file.
- Throws:
java.io.IOException
- if there is any problem reading from the file or parsing the results.
-
loadtxt
public static double[][] loadtxt(java.lang.String filePath, int skiprows) throws java.io.IOException
Read in an optionally GZIP compressed, delimited text file that contains a regular array of Double values. The delimiter may be any whitespace.- Parameters:
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).- Returns:
- A 2D double array of the values in the table file.
- Throws:
java.io.IOException
- if there is any problem reading from the file or parsing the results.
-
loadtxt
public static double[][] loadtxt(java.lang.String filePath, java.lang.String delimiter, int skiprows) throws java.io.IOException
Read in an optionally GZIP compressed, delimited text file that contains a regular array of Double values. The delimiter may be any whitespace.- Parameters:
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).- Returns:
- A 2D double array of the values in the table file.
- Throws:
java.io.IOException
- if there is any problem reading from the file or parsing the results.
-
loadtxt
public static double[][] loadtxt(java.io.File txtFile) throws java.io.IOException
Read in an optionally GZIP compressed, delimited text file that contains a regular array of Double values. The delimiter may be any whitespace.- Parameters:
txtFile
- The possibly GZIP compressed table file being read in.- Returns:
- A 2D double array of the values in the table file.
- Throws:
java.io.IOException
- if there is any problem reading from the file or parsing the results.
-
loadtxt
public static double[][] loadtxt(java.io.File txtFile, int skiprows) throws java.io.IOException
Read in an optionally GZIP compressed, delimited text file that contains a regular array of Double values. The delimiter may be any whitespace.- Parameters:
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).- Returns:
- A 2D double array of the values in the table file.
- Throws:
java.io.IOException
- if there is any problem reading from the file or parsing the results.
-
loadtxt
public static double[][] loadtxt(java.io.File txtFile, java.lang.String delimiter, int skiprows) throws java.io.IOException
Read in an optionally GZIP compressed, delimited text file that contains a regular array of Double values.- Parameters:
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).- Returns:
- A 2D double array of the values in the table file.
- Throws:
java.io.IOException
- if there is any problem reading from the file or parsing the results.
-
createTempDirectory
public static java.io.File createTempDirectory(java.lang.String prefix) throws java.io.IOException
Create a temporary directory using the specified prefix.- Parameters:
prefix
- The prefix string to be used in generating the file's name; must be at least three characters long. May not be null.- Returns:
- a reference to a temporary directory using the specified prefix.
- Throws:
java.io.IOException
- if there is any problem creating the temporary directory.
-
deleteDirectory
public static boolean deleteDirectory(java.io.File path)
Recursively deletes the directory tree indicated by the specified path. The directory and all of it's contents are deleted.- Parameters:
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.- Returns:
- true if the directory and it's contents were successfully deleted.
-
sameFile
public static boolean sameFile(java.io.File f1, java.io.File f2) throws java.io.IOException
Returnstrue
if and only if the two File objects refer to the same file in the file system.- Parameters:
f1
- The first file to check. May not be null.f2
- The 2nd file to check with the 1st one. May not be null.- Returns:
- true if and only if the two File objects refer to the same file in the file system.
- Throws:
java.io.IOException
- If an I/O error occurs, which is possible because the construction of the canonical pathname may require file system queries.
-
gzip
public static void gzip(java.io.File src, java.io.File dst) throws java.io.IOException
GZIP compress the src file writing to the destination file. The source & destination files may be identical.- Parameters:
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.- Throws:
java.io.IOException
- if there is any problem reading from or writing to the files.
-
copyAndGzip
public static void copyAndGzip(java.io.File src, java.io.File destDir) throws java.io.IOException
Copy a file to the specified destination directory while GZIP compressing the file. The original file is not modified. The file in the output directory will have the same name as the source file, but with ".gz" appended.- Parameters:
src
- The source file to be compressed. May not be null.destDir
- The directory to copy the compressed file into. May not be null.- Throws:
java.io.IOException
- if there is any problem reading from or writing to the files.
-
ungzip
public static void ungzip(java.io.File src, java.io.File dst) throws java.io.IOException
Un-GZIP the compressed src file writing the uncompressed data to the destination file. The source & destination files may be identical.- Parameters:
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.- Throws:
java.io.IOException
- if there is any problem reading from or writing to the files.
-
copyAndUngzip
public static void copyAndUngzip(java.io.File src, java.io.File destDir) throws java.io.IOException
Copy a file to the specified destination directory while decompressing the GZIP file. The source file is not modified. A new file is created in the destination directory that has the same name as the source file, but without the ".gz" extension (if there is one).- Parameters:
src
- The source file GZIP file. May not be null.destDir
- The directory to copy the uncompressed file into. May not be null.- Throws:
java.io.IOException
- if there is any problem reading from or writing to the files.
-
gzip
public static void gzip(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
GZIP compress the input stream and write it to the output stream.- Parameters:
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.- Throws:
java.io.IOException
- if there is any problem reading from or writing to the streams.
-
ungzip
public static void ungzip(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
Un-GZIP the compressed input stream and write the uncompressed data to the specified output stream.- Parameters:
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.- Throws:
java.io.IOException
- if there is any problem reading from or writing to the streams.
-
isGZIPCompressed
public static boolean isGZIPCompressed(java.io.File file) throws java.io.IOException
Returnstrue
if the specified input file is pointing at a GZIP compressed data set.- Parameters:
file
- The input file to be tested. May not be null.- Returns:
- true if the specified input file is pointing at a GZIP compressed data set.
- Throws:
java.io.IOException
- if there is a problem reading from the specified file.
-
isGZIPCompressed
public static boolean isGZIPCompressed(java.io.BufferedInputStream in) throws java.io.IOException
Returnstrue
if the specified input stream is pointing at a GZIP compressed data set.- Parameters:
in
- The input stream to be tested. May not be null.- Returns:
- true if the specified input stream is pointing at a GZIP compressed data set.
- Throws:
java.io.IOException
- if there is a problem reading from the input stream.
-
isGZIPCompressed
public static boolean isGZIPCompressed(byte[] bytes)
Returnstrue
if the specified array of bytes represent a GZIP compressed data set.- Parameters:
bytes
- the array of bytes to be tested. May not be null.- Returns:
- true if the specified array of bytes represent a GZIP compressed data set.
-
-