public class FileIOUtils
extends java.lang.Object
Title: Framework Support Library
Description: General file I/O Utilities for use by the framework.
Large streams (over 2GB) will return a bytes copied value of -1
after the copy has
completed since the correct number of bytes cannot be returned as an int. For large streams
use the copyLarge(InputStream, OutputStream)
method or read
method.
Copyright: Copyright (c) 2009
Company: StreamScape Technologies
Modifier and Type | Class and Description |
---|---|
static interface |
FileIOUtils.StreamProcessor |
Constructor and Description |
---|
FileIOUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
closeQuietly(java.io.InputStream input)
Unconditionally close an
InputStream . |
static void |
closeQuietly(java.io.OutputStream output)
Unconditionally close an
OutputStream . |
static void |
closeQuietly(java.io.Reader input)
Unconditionally close an
Reader . |
static void |
closeQuietly(java.io.Writer output)
Unconditionally close a
Writer . |
static void |
closeSafe(java.io.RandomAccessFile raf) |
static long |
copy(java.io.File source,
java.io.File target)
Copies one file to another.
|
static long |
copy(java.io.File input,
java.io.File output,
byte[] copyBuffer)
Copies one file to another using the supplied buffer.
|
static int |
copy(java.io.InputStream input,
java.io.OutputStream output)
Copy bytes from an
InputStream to an OutputStream . |
static long |
copy(java.io.InputStream in,
java.io.OutputStream out,
byte[] copyBuffer)
Copies an
InputStream to an OutputStream using the specified buffer. |
static void |
copy(java.io.InputStream input,
java.io.Writer output)
Copy bytes from an
InputStream to chars on a
Writer using the default character encoding of the platform. |
static void |
copy(java.io.InputStream input,
java.io.Writer output,
java.lang.String encoding)
Copy bytes from an
InputStream to chars on a
Writer using the specified character encoding. |
static void |
copy(java.io.Reader input,
java.io.OutputStream output)
Copy chars from a
Reader to bytes on an
OutputStream using the default character encoding of the platform, and calling flush. |
static void |
copy(java.io.Reader input,
java.io.OutputStream output,
java.lang.String encoding)
Copy chars from a
Reader to bytes on an
OutputStream using the specified character encoding, and calling flush. |
static int |
copy(java.io.Reader input,
java.io.Writer output)
Copy chars from a
Reader to a Writer . |
static void |
copyDirectory(FileSystem fileSystem,
java.lang.String src,
java.lang.String dst) |
static void |
copyFileDir(java.io.File source,
java.io.File target)
Copies one directory or file to another.
|
static long |
copyLarge(java.io.InputStream input,
java.io.OutputStream output)
Copies bytes from a large (over 2GB)
InputStream to the specified OutputStream . |
static long |
copyLarge(java.io.InputStream input,
java.io.OutputStream output,
long sizeLimit)
Copies bytes from a large (over 2GB)
InputStream to the specified OutputStream . |
static long |
copyLarge(java.io.RandomAccessFile input,
java.io.OutputStream output)
Copies bytes from a large (over 2GB)
RandomAccessFile to the specified OutputStream . |
static long |
copyLarge(java.io.RandomAccessFile input,
java.io.OutputStream output,
long sizeLimit)
Copies bytes from a large (over 2GB)
RandomAccessFile to the specified OutputStream . |
static long |
copyLarge(java.io.Reader input,
java.io.Writer output)
Copy chars from a large (over 2GB)
Reader to a Writer . |
static java.io.File |
createTempDir(java.io.File parentDir) |
static boolean |
deleteFile(java.lang.String fileURL) |
static boolean |
deleteFileDir(java.io.File filePath)
Removes a file.
|
static boolean |
deleteFileDir(java.io.File filePath,
boolean withSymbolicLinks)
Removes a file.
|
static boolean |
deleteFileDir(java.io.File filePath,
java.util.Set excludedFiles) |
static boolean |
deleteFileDir(java.io.File filePath,
java.util.Set excludedFiles,
boolean withSymbolicLinks) |
static boolean |
deleteFileDir(java.io.File filePath,
java.util.Set excludedFiles,
boolean withSymbolicLinks,
java.util.function.Function filter) |
static java.io.File[] |
directoryList(java.lang.String dirPath,
java.lang.String fileFilter)
A variation of the method that returns the list of files (only visible files) as an array.
|
static java.util.List |
directoryList(java.lang.String dirPath,
java.lang.String fileFilter,
boolean absolute)
Returns a list of files that are the contents of the specified
directory.
|
static void |
directoryListRecursive(java.io.File dirFile,
java.util.function.Consumer onFile,
java.util.function.Function onDirectory) |
static java.util.List |
directorySublist(java.lang.String dirPath,
boolean absolute)
Returns a list of sub-directories under the specified directory path.
|
static boolean |
equals(java.lang.String filename1,
java.lang.String filename2) |
static boolean |
equalsByContent(java.io.File file1,
java.io.File file2)
Checks if the two specified file have the same content.
|
static boolean |
equalsByContent(java.io.InputStream stream1,
java.io.InputStream stream2)
Checks if the two specified input streams have the same content.
|
static boolean |
equalsByContent(java.net.URLConnection stream1,
java.io.InputStream stream2)
Checks if the two specified input streams have the same content.
|
static boolean |
fileAvailable(java.lang.String fileURL)
Checks if a file exists (is available).
|
static boolean |
fileAvailable(java.net.URI uri) |
static boolean |
fileReadable(java.lang.String fileURL) |
static boolean |
fileWritable(java.lang.String fileURL) |
static long |
getCreationTime(java.io.File file) |
static java.lang.String |
getExtension(java.lang.String fileNameOrPath)
Returns file extension
|
static byte[] |
getFile(java.lang.String workingDir,
java.lang.String filename)
Obtains the contents of a file as a
byte[] buffer. |
static byte[] |
getFileContent(java.io.File file)
Returns a content of the specified file.
|
static java.lang.String |
getRelativePath(java.io.File base,
java.io.File child)
Returns
child path relative to base or null
if child is not descendant of base . |
static java.lang.String |
getRelativePathForCanonicalBase(java.io.File base,
java.io.File child)
Returns
child path relative to base or null
if child is not descendant of base |
static byte[] |
getStreamContent(java.io.InputStream stream,
long sizeLimit)
Reads and returns a content of the specified stream.
|
static boolean |
isDescendant(java.io.File base,
java.io.File child) |
static boolean |
isDescendantForCanonicalBase(java.io.File base,
java.io.File child) |
static boolean |
isFile(java.lang.String location,
java.lang.String ext,
java.lang.String absPath)
Returns whether
absPath denotes first level under location with
ext extension |
static boolean |
isFirstLevelFile(java.lang.String location,
java.lang.String ext,
java.lang.String absPath)
Returns whether
absPath denotes first level file under location with
ext extension |
static java.nio.channels.FileLock |
lock(java.nio.channels.FileChannel channel,
java.lang.String filename) |
static void |
move(java.io.File source,
java.io.File target)
Moves one directory or file to another.
|
static java.io.File |
moveToDirWithUniqueName(java.io.File source,
java.io.File destDir)
Moves
source file to destDir directory. |
static java.io.File |
newFile(java.lang.String pathName)
Create a file from path name.
|
static java.io.File |
newFile(java.net.URI uri)
Create a file based on a URI.
|
static java.io.File |
newFileDir(java.lang.String pathName)
Create a file or directory.
|
static java.io.File |
openFile(java.lang.String pathName)
Opens a file if it exists.
|
static java.io.File |
openFile(java.net.URI uri)
Opens the file that may already exist.
|
static long |
processLargeStream(java.io.InputStream input,
long sizeLimit,
FileIOUtils.StreamProcessor processor) |
static long |
processStream(java.io.InputStream input,
long sizeLimit,
FileIOUtils.StreamProcessor processor) |
static void |
putFile(java.lang.String workingDir,
java.lang.String filename,
byte[] data)
Writes the specified data to the specified file at the specified location.
|
static void |
putFile(java.lang.String workingDir,
java.lang.String filename,
byte[] data,
long timeout)
Writes the specified data to the specified file at the specified location.
|
static void |
removeIfNotLocked(java.io.File file) |
static void |
renameDirectory(FileSystem fileSystem,
java.lang.String src,
java.lang.String dst) |
static boolean |
renameFile(java.lang.String oldURL,
java.lang.String newURL)
Renames a file qualified by a full path name from the old name to the new.
|
static void |
setCreationTime(java.io.File file,
long time) |
static java.nio.channels.FileLock |
tryLock(java.nio.channels.FileChannel channel,
long timeout) |
static void |
unmap(java.nio.MappedByteBuffer buffer) |
public static boolean fileAvailable(java.lang.String fileURL)
true
if string "abc.txt" is checked against file
"AbC.txt" for existence.fileURL
- Stringtrue
if the file exists.public static boolean fileAvailable(java.net.URI uri) throws java.io.IOException
java.io.IOException
public static boolean fileReadable(java.lang.String fileURL)
public static boolean fileWritable(java.lang.String fileURL)
public static boolean renameFile(java.lang.String oldURL, java.lang.String newURL)
FileTransaction
be used if transacted behavior is desired.oldURL
- StringnewURL
- Stringpublic static boolean deleteFile(java.lang.String fileURL)
public static boolean deleteFileDir(java.io.File filePath)
false
.
Files under symbolic links removed as well.filePath
- file to be removedpublic static boolean deleteFileDir(java.io.File filePath, java.util.Set excludedFiles)
public static boolean deleteFileDir(java.io.File filePath, boolean withSymbolicLinks)
false
.filePath
- file to be removedwithSymbolicLinks
- if set to false files under symbolic links will be removedpublic static boolean deleteFileDir(java.io.File filePath, java.util.Set excludedFiles, boolean withSymbolicLinks)
public static boolean deleteFileDir(java.io.File filePath, java.util.Set excludedFiles, boolean withSymbolicLinks, java.util.function.Function filter)
public static void move(java.io.File source, java.io.File target) throws UtilitiesException
source
- the file to move from.target
- the file to move to.UtilitiesException
- if an I/O error occurs (may result in partially done work).public static void copyFileDir(java.io.File source, java.io.File target) throws UtilitiesException
source
- directory or file to copy fromtarget
- directory or file to copy toUtilitiesException
- if an I/O error occurs (may result in partially done work)public static java.util.List directoryList(java.lang.String dirPath, java.lang.String fileFilter, boolean absolute) throws UtilitiesException
dirPath
- String Directory NamefileFilter
- String Filter expression.absolute
- boolean If true
returns the absolute file name prefixed by the path.UtilitiesException
public static java.io.File[] directoryList(java.lang.String dirPath, java.lang.String fileFilter) throws UtilitiesException
dirPath
- StringfileFilter
- StringUtilitiesException
public static java.util.List directorySublist(java.lang.String dirPath, boolean absolute) throws UtilitiesException
dirPath
- Stringabsolute
- booleanUtilitiesException
public static void directoryListRecursive(java.io.File dirFile, java.util.function.Consumer onFile, java.util.function.Function onDirectory) throws java.io.IOException
java.io.IOException
public static java.io.File newFileDir(java.lang.String pathName) throws UtilitiesException
pathName
- Stringjava.io.IOException
UtilitiesException
public static java.io.File newFile(java.lang.String pathName) throws UtilitiesException
pathName
- StringUtilitiesException
public static java.io.File openFile(java.lang.String pathName) throws UtilitiesException
pathName
- StringUtilitiesException
- Thrown when the file does not exist.public static java.io.File newFile(java.net.URI uri) throws UtilitiesException
uri
- URIUtilitiesException
public static java.io.File openFile(java.net.URI uri) throws UtilitiesException
uri
- URIUtilitiesException
public static void closeQuietly(java.io.Reader input)
Reader
.
Equivalent to Reader.close()
, except any exceptions will be ignored.
This is typically used in finally blocks.
input
- the Reader to close, may be null or already closedpublic static void closeQuietly(java.io.Writer output)
Writer
.output
- the Writer to close, may be null or already closedpublic static void closeQuietly(java.io.InputStream input)
InputStream
.input
- the InputStream to close, may be null or already closedpublic static void closeQuietly(java.io.OutputStream output)
OutputStream
.output
- the OutputStream to close, may be null or already closedpublic static byte[] getFileContent(java.io.File file) throws UtilitiesException
file
- FileUtilitiesException
public static byte[] getStreamContent(java.io.InputStream stream, long sizeLimit) throws UtilitiesException
stream
- the stream to be read.sizeLimit
- the maximum number of bytes can be read from the specified stream.UtilitiesException
- if some error occurs during a reading.public static int copy(java.io.InputStream input, java.io.OutputStream output) throws UtilitiesException
InputStream
to an OutputStream
.
This method buffers the input internally, so there is no need to use a BufferedInputStream
.
input
- the InputStream
to read fromoutput
- the OutputStream
to write toUtilitiesException
public static long processStream(java.io.InputStream input, long sizeLimit, FileIOUtils.StreamProcessor processor) throws UtilitiesException
UtilitiesException
public static long processLargeStream(java.io.InputStream input, long sizeLimit, FileIOUtils.StreamProcessor processor) throws UtilitiesException
UtilitiesException
public static long copyLarge(java.io.InputStream input, java.io.OutputStream output) throws UtilitiesException
InputStream
to the specified OutputStream
.
This method buffers the input internally, so there is no need to use a BufferedInputStream
.input
- the InputStream
to read from.output
- the OutputStream
to write to.UtilitiesException
public static long copyLarge(java.io.InputStream input, java.io.OutputStream output, long sizeLimit) throws UtilitiesException
InputStream
to the specified OutputStream
.
This method buffers the input internally, so there is no need to use a BufferedInputStream
.input
- the InputStream
to read from.output
- the OutputStream
to write to.sizeLimit
- the number of bytes to be copied.UtilitiesException
public static long copyLarge(java.io.RandomAccessFile input, java.io.OutputStream output) throws UtilitiesException
RandomAccessFile
to the specified OutputStream
.
This method buffers the input internally, so there is no need to use a BufferedInputStream
.input
- the RandomAccessFile
to read from.output
- the OutputStream
to write to.UtilitiesException
public static long copyLarge(java.io.RandomAccessFile input, java.io.OutputStream output, long sizeLimit) throws UtilitiesException
RandomAccessFile
to the specified OutputStream
.
This method buffers the input internally, so there is no need to use a BufferedInputStream
.input
- the RandomAccessFile
to read from.output
- the OutputStream
to write to.sizeLimit
- the number of bytes to be copied.UtilitiesException
public static long copy(java.io.File source, java.io.File target) throws UtilitiesException
source
- the file to move from.target
- the file to move to.UtilitiesException
- if an I/O error occurs (may result in partially done work).public static long copy(java.io.File input, java.io.File output, byte[] copyBuffer) throws UtilitiesException
input
- source fileoutput
- destination filecopyBuffer
- buffer used for copyingUtilitiesException
- if an I/O error occurs (may result in partially done work)public static long copy(java.io.InputStream in, java.io.OutputStream out, byte[] copyBuffer) throws UtilitiesException
InputStream
to an OutputStream
using the specified buffer.in
- stream to copy fromout
- stream to copy tocopyBuffer
- buffer used for copyingUtilitiesException
- if an I/O error occurs (may result in partially done work)public static void copy(java.io.InputStream input, java.io.Writer output) throws java.io.IOException
InputStream
to chars on a
Writer
using the default character encoding of the platform.
This method buffers the input internally, so there is no need to use a
BufferedInputStream
.
This method uses InputStreamReader
.
input
- the InputStream
to read fromoutput
- the Writer
to write tojava.io.IOException
public static void copy(java.io.InputStream input, java.io.Writer output, java.lang.String encoding) throws java.io.IOException
InputStream
to chars on a
Writer
using the specified character encoding.
This method buffers the input internally, so there is no need to use a BufferedInputStream
.
This method uses InputStreamReader
.
input
- the InputStream
to read fromoutput
- the Writer
to write toencoding
- the encoding to use, null means platform defaultjava.io.IOException
public static int copy(java.io.Reader input, java.io.Writer output) throws java.io.IOException
Reader
to a Writer
.
This method buffers the input internally, so there is no need to use a
BufferedReader
.
input
- the Reader
to read fromoutput
- the Writer
to write tojava.io.IOException
public static long copyLarge(java.io.Reader input, java.io.Writer output) throws java.io.IOException
Reader
to a Writer
.
This method buffers the input internally, so there is no need to use a BufferedReader
.
input
- the Reader
to read fromoutput
- the Writer
to write tojava.io.IOException
public static void copy(java.io.Reader input, java.io.OutputStream output) throws java.io.IOException
Reader
to bytes on an
OutputStream
using the default character encoding of the platform, and calling flush.
This method buffers the input internally, so there is no need to use a BufferedReader
.
Due to the implementation of OutputStreamWriter, this method performs a flush.
This method uses OutputStreamWriter
.
input
- the Reader
to read fromoutput
- the OutputStream
to write tojava.io.IOException
public static void copy(java.io.Reader input, java.io.OutputStream output, java.lang.String encoding) throws java.io.IOException
Reader
to bytes on an
OutputStream
using the specified character encoding, and calling flush.
This method buffers the input internally, so there is no need to use a
BufferedReader
.
This method uses OutputStreamWriter
.
input
- the Reader
to read fromoutput
- the OutputStream
to write toencoding
- the encoding to use, null means platform defaultjava.io.IOException
public static byte[] getFile(java.lang.String workingDir, java.lang.String filename) throws UtilitiesException
byte[]
buffer.
This method locks the file while it is reading it ensuring that if
other applications have an exclusive lock on the file the operation
will fail.workingDir
- Stringfilename
- StringUtilitiesException
public static void putFile(java.lang.String workingDir, java.lang.String filename, byte[] data) throws UtilitiesException
1
second before writing.
If the file cannot be locked (i.e. other process locked the file before) the method throws an exception.
workingDir
- the file location.filename
- the file name.data
- the data to be written.UtilitiesException
- if some error occurs.public static void putFile(java.lang.String workingDir, java.lang.String filename, byte[] data, long timeout) throws UtilitiesException
workingDir
- the file location.filename
- the file name.data
- the data to be written.timeout
- the timeout (in milliseconds) for file locking.UtilitiesException
- if some error occurs.public static java.io.File moveToDirWithUniqueName(java.io.File source, java.io.File destDir) throws UtilitiesException
source
file to destDir
directory.source
- source filedestDir
- destination directoryUtilitiesException
public static java.lang.String getExtension(java.lang.String fileNameOrPath)
fileNameOrPath
- file name or file path (relative or absolute)public static boolean isFile(java.lang.String location, java.lang.String ext, java.lang.String absPath)
absPath
denotes first level under location
with
ext
extensionlocation
- location absolute pathext
- file extension (with '.' included)absPath
- file absolute pathpublic static boolean isFirstLevelFile(java.lang.String location, java.lang.String ext, java.lang.String absPath)
absPath
denotes first level file under location
with
ext
extensionlocation
- location absolute pathext
- file extension (with '.' included)absPath
- file absolute pathpublic static void closeSafe(java.io.RandomAccessFile raf)
public static boolean isDescendantForCanonicalBase(java.io.File base, java.io.File child) throws java.io.IOException
java.io.IOException
public static boolean isDescendant(java.io.File base, java.io.File child) throws java.io.IOException
java.io.IOException
public static java.lang.String getRelativePathForCanonicalBase(java.io.File base, java.io.File child) throws java.io.IOException
child
path relative to base
or null
if child
is not descendant of base
base
- SHOULD BE CANONICAL FILEchild
- java.io.IOException
public static java.lang.String getRelativePath(java.io.File base, java.io.File child) throws java.io.IOException
child
path relative to base
or null
if child
is not descendant of base
.base
- child
- java.io.IOException
public static void unmap(java.nio.MappedByteBuffer buffer)
public static java.nio.channels.FileLock lock(java.nio.channels.FileChannel channel, java.lang.String filename) throws java.io.IOException
java.io.IOException
public static java.nio.channels.FileLock tryLock(java.nio.channels.FileChannel channel, long timeout) throws java.io.IOException
java.io.IOException
public static void removeIfNotLocked(java.io.File file)
public static boolean equals(java.lang.String filename1, java.lang.String filename2)
public static boolean equalsByContent(java.io.File file1, java.io.File file2)
file1
- the first file.file2
- the second file.true
if the specified files have the same content, false
otherwise.public static boolean equalsByContent(java.io.InputStream stream1, java.io.InputStream stream2)
stream1
- the first stream.stream2
- the second stream.true
if the specified streams have the same content, false
otherwise.public static boolean equalsByContent(java.net.URLConnection stream1, java.io.InputStream stream2)
stream1
- the first stream.stream2
- the second stream.true
if the specified streams have the same content, false
otherwise.public static void renameDirectory(FileSystem fileSystem, java.lang.String src, java.lang.String dst) throws java.io.IOException
java.io.IOException
public static void copyDirectory(FileSystem fileSystem, java.lang.String src, java.lang.String dst) throws java.io.IOException
java.io.IOException
public static long getCreationTime(java.io.File file)
public static void setCreationTime(java.io.File file, long time)
public static java.io.File createTempDir(java.io.File parentDir) throws UtilitiesException
UtilitiesException
Copyright © 2015-2024 StreamScape Technologies. All rights reserved.