java.lang.Object | |
↳ | com.pnfsoftware.jeb.util.io.IO |
File manipulation utility routines.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | MAX_FILENAME_SIZE | Maximum filename size used in JEB (255 allowed for Linux, 259 for Windows file) | |||||||||
int | MAX_PATH_SIZE | Maximum foldername size used in JEB (248 for Windows folder) |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
IO() |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static String |
abs(File file)
Provide an absolute simplified path.
| ||||||||||
static String |
abs(String path)
Provide an absolute simplified path.
| ||||||||||
static void |
addFileToJar(JarOutputStream jar, File file, String entryName)
Add a new entry to a a jar output stream.
| ||||||||||
static void |
addFileToZip(ZipOutputStream zip, File file, String entryName)
Add a new entry to a a zip output stream.
| ||||||||||
static String |
basename(String path)
Return the base name of the provided path.
| ||||||||||
static boolean | checkFileFreshness(File file, long millis, boolean cleanStaleFile) | ||||||||||
static boolean |
compareFiles(File file0, File file1)
Compare the contents of two files for equality.
| ||||||||||
static boolean |
compressFolder(String folderPath, String zipfilePath)
Compress a folder and its contents to a zip archive.
| ||||||||||
static Thread |
copyAsync(InputStream input, OutputStream output)
Copy a continuous stream of data from a source to a destination.
| ||||||||||
static void |
copyFile(File src, File dst, boolean overwrite)
Copy a source file to a destination.
| ||||||||||
static long |
copyStream(InputStream input, OutputStream output, byte[] buffer)
Read bytes from an input stream and dump them to an output stream.@return
| ||||||||||
static long |
copyStream(InputStream input, OutputStream output)
Read bytes from an input stream and dump them to an output stream.@return
| ||||||||||
static boolean |
createDirectory(String path)
Safely create a directory.
| ||||||||||
static boolean |
createDirectory(File f)
Safely create a directory.
| ||||||||||
static boolean |
createFile(File file, boolean createDirs)
Create an empty file.
| ||||||||||
static File |
createFolder(String path)
Create or retrieve a directory.
| ||||||||||
static void |
createFoldersForFile(File file)
Create the directory structure needed to store a file.
| ||||||||||
static File |
createSafeTempFile()
Create an empty file in the safe temporary area.
| ||||||||||
static File |
createTempFile(String exactname)
Create a file (with an exact name) in the default temp folder.
| ||||||||||
static File |
createTempFile()
Create an empty file in the temporary folder.
| ||||||||||
static File |
createTempFile(String basename, String extension)
Create a file in the default temp folder.@return
| ||||||||||
static File |
createTempFileNumbered(String name)
Create a new file with the provided name in the default temp folder.
| ||||||||||
static File |
createTempFolder(String folderName)
Create a directory in the default temporary-file directory.
| ||||||||||
static boolean |
deleteDirectory(File dir)
Delete a directory and its contents recursively.
| ||||||||||
static boolean |
deleteDirectory(String path)
Delete a directory and its contents recursively.
| ||||||||||
static boolean |
deleteDirectoryContents(File dir)
Delete a directory contents recursively (ie, clear the directory).
| ||||||||||
static void |
deleteDirectoryOnExit(File dir)
Delete a folder and all its contents recursively, when the virtual machine terminates.
| ||||||||||
static boolean |
deleteFile(File file)
Delete a file.
| ||||||||||
static String |
dirname(String path)
Return the directory name of the provided path.
| ||||||||||
static String |
escapeFileName(String filename)
Escape filename invalid characters with an underscore.
| ||||||||||
static String |
escapeFileName(String filename, char newChar)
Escape illegal characters in filename by the provided char.
| ||||||||||
static String |
escapeFileNameStrict(String filename, char newChar)
Create a valid filename from any string.
| ||||||||||
static String |
escapeFileNameStrict(String filename)
Create a valid filename with an underscore for non alphanumeric char, '-', '_', '.' nor '''.
| ||||||||||
static String |
expandPath(String path)
Perform expansion of certain shell artifacts used in path names.
| ||||||||||
static void |
extractToFolder(File inputZipFile, File outputFolder)
Extract the contents of a zip archive to a target folder.
| ||||||||||
static String |
getCwd()
Get the path of the current working directory.
| ||||||||||
static String |
getExtension(String path)
Extract the extension of a file path.
| ||||||||||
static String |
getExtension(File file)
Extract the extension of a file path.
| ||||||||||
static byte |
getFirstByte(String path)
Get the first byte of a file.
| ||||||||||
static int |
getFirstIntLE(String path)
Get the first 4-byte of a file, read as a little-endian integer.
| ||||||||||
static short |
getFirstShortLE(String path)
Get the first 2-byte of a file, read as a little-endian short.
| ||||||||||
static File |
getHomeFolder()
Retrieve the current user's home folder.
| ||||||||||
static String |
getOriginalCwd()
Get original working directory, i.e.
| ||||||||||
static File |
getParentFile2(File file)
A safer implementation of
getParentFile() . | ||||||||||
static String |
getRelativePath(File file, File base)
Provide the path to a file relative to a base.
| ||||||||||
static File |
getSafeTempFolder()
This method is deprecated.
use
getSessionTemporaryFolder() instead
| ||||||||||
static File |
getSessionTemporaryFolder()
Retrieve the JEB session-specific temporary folder.
| ||||||||||
static File |
getTempFolder()
Retrieve the temporary folder.
| ||||||||||
static boolean |
inFolder(File file, File folder)
Determine if a hypothetical file would be contained within a hypothetical folder, i.e.
| ||||||||||
static boolean |
isFile(String path)
Determine if a path refers to an existing file.
| ||||||||||
static boolean |
isUrl(String s)
Determine whether the provided input string looks like a URL.
| ||||||||||
static List<File> |
listFiles(String folderpath)
Recursively do a directory listing.
| ||||||||||
static List<File> |
listFiles(File folder)
Recursively do a directory listing.
| ||||||||||
static String | noExtension(String path) | ||||||||||
static File | noExtension(File file) | ||||||||||
static List<String> |
parsePathElements(String s)
Parse the elements of a path.
| ||||||||||
static byte[] |
readFile(String path)
Read the contents of a binary file.@return
| ||||||||||
static byte[] |
readFile(File file, long maxAllowedSize)
Read the contents of a binary file that is at most 2 Gb.
| ||||||||||
static byte[] |
readFile(File file)
Read the contents of a binary file that is at most 2 Gb.
| ||||||||||
static byte[] |
readFileSafe(File file)
Read a binary file safely.
| ||||||||||
static String |
readInputLine()
Read a line from the standard input.@return
| ||||||||||
static String |
readInputLineSafe()
Read a line from the standard input.
| ||||||||||
static byte[] |
readInputStream(InputStream in)
Read and reliably return all bytes of an input stream.
| ||||||||||
static List<String> |
readLines(InputStream input)
Read the contents of a stream as a text buffer of UTF-8 encoded strings.@return
| ||||||||||
static List<String> |
readLines(File file, Charset encoding)
Read the lines of a text file.@return
| ||||||||||
static List<String> |
readLines(File file)
Read the UTF-8 encoded lines of a text file.
| ||||||||||
static List<String> |
readLines(InputStream input, Charset encoding)
Read the contents of a stream as a line of strings.
| ||||||||||
static List<String> |
readLinesSafe(File file)
Read the lines of a UTF8 encoded text file.
| ||||||||||
static List<String> |
readLinesSafe(File file, Charset encoding)
Read the lines of a text file.
| ||||||||||
static boolean |
renameFile(File src, File dst, int mode)
Rename a file with options.
| ||||||||||
static File | replaceExtension(File file, String extension) | ||||||||||
static String[] |
safeList(File f)
A safe version of
list() . | ||||||||||
static String[] |
safeList(File f, FilenameFilter filter)
A safe version of
list(FilenameFilter) . | ||||||||||
static File[] |
safeListFiles(File f)
A safe version of
listFiles() . | ||||||||||
static File[] |
safeListFiles(File f, FileFilter filter)
A safe version of
listFiles(FileFilter) . | ||||||||||
static String |
sanitizePath(String path, boolean isSinglePathElement, boolean replaceBlankCharacters)
Sanitize a path.
| ||||||||||
static String |
sanitizePathUnsafe(String path)
Attempt to sanitize a path.
| ||||||||||
static String |
setCwd(String cwd)
Set the path of the current working directory.
| ||||||||||
static String |
simplifyPath(String path)
Simplify a path.
| ||||||||||
static String |
simplifyPathUnix(String path)
See
simplifyPath(String) . | ||||||||||
static String |
simplifyPathWindows(String path)
See
simplifyPath(String) . | ||||||||||
static String[] |
splitExtension(String path)
Split a file path into its extension-less part and its extension.
| ||||||||||
static String[] |
splitPath(String path)
Split a path into its base part and its filename part.
| ||||||||||
static void |
writeFile(File file, String str)
Write a string to a file using the default character encoding of the current system.
| ||||||||||
static void |
writeFile(File file, byte[] data, boolean createDirs)
Write data to a file whose containing directory structure may not exist.
| ||||||||||
static void |
writeFile(File file, byte[] data, int offset, int size)
Write data to a file.
| ||||||||||
static void |
writeFile(File file, String str, String charsetName)
Write a string to a file using the given character encoding of the current system.
| ||||||||||
static void |
writeFile(File file, byte[] data, int offset, int size, boolean createDirs)
Write data to a file whose containing directory structure may not exist.
| ||||||||||
static void |
writeFile(File file, byte[] data)
Write data to a file.
| ||||||||||
static boolean |
writeFileSafe(File file, byte[] data, int offset, int size, boolean createDirs)
Write a binary file safely.
| ||||||||||
static boolean |
writeFileSafe(File file, byte[] data, boolean createDirs)
Write a binary file safely.
| ||||||||||
static void |
writeLines(File file, List<? extends CharSequence> lines)
Write UTF-8 encoded strings.
| ||||||||||
static void |
writeLines(OutputStream output, List<? extends CharSequence> lines, Charset encoding)
Write strings.
| ||||||||||
static void |
writeLines(File file, List<? extends CharSequence> lines, Charset encoding)
Write strings.
| ||||||||||
static void |
writeLines(OutputStream output, List<? extends CharSequence> lines)
Write UTF-8 encoded strings.
| ||||||||||
static boolean |
writeLinesSafe(OutputStream output, List<? extends CharSequence> lines)
Write UTF-8 encoded strings.
| ||||||||||
static boolean |
writeLinesSafe(File file, List<? extends CharSequence> lines, Charset encoding)
Write strings.
| ||||||||||
static boolean |
writeLinesSafe(OutputStream output, List<? extends CharSequence> lines, Charset encoding)
Write strings.
| ||||||||||
static boolean |
writeLinesSafe(File file, List<? extends CharSequence> lines)
Write UTF-8 encoded strings.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Maximum filename size used in JEB (255 allowed for Linux, 259 for Windows file)
Maximum foldername size used in JEB (248 for Windows folder)
Provide an absolute simplified path. This method is OS-dependent.
Rules:
- .
and ..
uses are resolved.
- Multiple-path separator uses such as ///
or \\
are resolved.
- Redundant trailing separators are removed.
- The empty path is always resolved to .
.
- If the FS is case-insensitive (e.g. NTFS), the case is not standardized
(Windows).
- Symbolic links are not resolved.
file | input file |
---|
Provide an absolute simplified path. This method is OS-dependent.
Rules:
- .
and ..
uses are resolved.
- Multiple-path separator uses such as ///
or \\
are resolved.
- Redundant trailing separators are removed.
- The empty path is always resolved to .
.
- If the FS is case-insensitive (e.g. NTFS), the case is not standardized
(Windows).
- Symbolic links are not resolved.
path | input path |
---|
Add a new entry to a a jar output stream.
IOException |
---|
Add a new entry to a a zip output stream.
IOException |
---|
Return the base name of the provided path. The base name is the part of the path that follows the last file separator character (slash or backslash). No further processing is done. The returned string may be empty or blank. It does not contain slash or backslash.
path | a path |
---|
millis
ms old
Compare the contents of two files for equality.
file0 | first file |
---|---|
file1 | second file |
FileNotFoundException | one of the input file is not found |
---|---|
IOException | other IO exception |
Compress a folder and its contents to a zip archive.
folderPath | input folder |
---|---|
zipfilePath | output zip file path |
Copy a continuous stream of data from a source to a destination. This method is non-blocking.
input | the source input stream |
---|---|
output | the destination output stream |
Copy a source file to a destination.
src | source file (not a directory) |
---|---|
dst | destination file or directory |
overwrite | if true, the destination file may be overwritten if it already exists |
FileNotFoundException | the source is not found |
---|---|
FileAlreadyExistsException | the destination would be overwritten |
IOException | other IO exception |
Read bytes from an input stream and dump them to an output stream.@return
buffer | recommended use: multiple of 0x1000 |
---|
IOException |
---|
Read bytes from an input stream and dump them to an output stream.@return
IOException |
---|
Safely create a directory.
Safely create a directory.
Create an empty file.
createDirs | if true, the directory structure needed to contain the file will also be created |
---|
IOException |
---|
Create or retrieve a directory.
path | directory path |
---|
IOException | on creation error |
---|
Create the directory structure needed to store a file.
file | a file |
---|
IOException |
---|
Create an empty file in the safe temporary area.
IOException | on file creation error |
---|
Create a file (with an exact name) in the default temp folder.
exactname | exact file name |
---|
Create an empty file in the temporary folder.
IOException | on file creation error |
---|
Create a file in the default temp folder.@return
basename | optional base name |
---|---|
extension | optional extension (suffix) |
IOException |
---|
Create a new file with the provided name in the default temp folder. If a file with the
provided name already exists, counter-suffixed names are tried (NAME.1
,
NAME.2
, etc.) until the first non-existent entry is found.
name | desired file name |
---|
IOException |
---|
Create a directory in the default temporary-file directory.
folderName | optional folder name; if null, a randomly named folder will be created |
---|
IOException | if an IO exception happened |
---|
Delete a directory and its contents recursively.
Delete a directory and its contents recursively.
Delete a directory contents recursively (ie, clear the directory). The directory itself is not deleted.
Delete a folder and all its contents recursively, when the virtual machine terminates.
dir | folder to be deleted |
---|
Delete a file.
file | the file to be deleted |
---|
Return the directory name of the provided path. The returned string may be empty. It will is not terminated by slash or backslash.
path | a path |
---|
Escape filename invalid characters with an underscore.
For proper path sanitization, use sanitizePath(String, boolean, boolean)
.
filename | to escape |
---|
Escape illegal characters in filename by the provided char. Note than this is a black-list escaper (whitelist character would be too costly to manage any char) so file name can still be irrelevant on the target Operating System. The list of excluded characters are ':', '\', '/', '*', '"', '?', '|', '<', '>' which covers most of the invalid characters.
For proper path sanitization, use sanitizePath(String, boolean, boolean)
.
filename | to escape |
---|---|
newChar | replacement character |
Create a valid filename from any string. Compared to escapeFileName(String, char)
,
this is a white-list escaper so generated filename will be legal on any non-exotic target
Operating System. The list of allowed characters are any alphanumeric char, '-', '_', '.',
'''.
For proper path sanitization, use sanitizePath(String, boolean, boolean)
.
filename | to escape |
---|---|
newChar | replacement character |
Create a valid filename with an underscore for non alphanumeric char, '-', '_', '.' nor '''.
For proper path sanitization, use sanitizePath(String, boolean, boolean)
.
filename | to escape |
---|
Perform expansion of certain shell artifacts used in path names.
Currently, the expansions performed are:
Extract the contents of a zip archive to a target folder.
inputZipFile | input zip file path |
---|---|
outputFolder | output folder directory |
IOException | if an error occurred; the current state of extraction within the target folder is not erased |
---|
Get the path of the current working directory.
Extract the extension of a file path.
path | a path |
---|
Extract the extension of a file path.
file | a file |
---|
Get the first byte of a file. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.
Get the first 4-byte of a file, read as a little-endian integer. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.
path | file path |
---|
Get the first 2-byte of a file, read as a little-endian short. This method never throws exceptions. If an error occurs, 0 is returned. It is up to the client to handle that special case.
Retrieve the current user's home folder.
Get original working directory, i.e. the value of the current directory set by the JVM when the program was started.
This method is unreliable. It assumes that all updates to the current directory are done
through this class
.
A safer implementation of getParentFile()
. If the input file is a single-level
relative path (eg, "abc"), this method will first retrieve the absolute path (relative to the
current folder) before trying to retrieve the parent folder.
file | file or folder |
---|
Provide the path to a file relative to a base.
file | a file |
---|---|
base | the base to check for |
This method is deprecated.
use getSessionTemporaryFolder()
instead
Retrieve the JEB session-specific temporary folder. That folder is unique per session, and can be used to drop additional temporary files and folders. Its contents is deleted when the session ends (i.e. when the owning JEB instance is terminated).
Retrieve the JEB session-specific temporary folder. That folder is unique per session, and can be used to drop additional temporary files and folders. Its contents is deleted when the session ends (i.e. when the owning JEB instance is terminated).
Retrieve the temporary folder.
Determine if a hypothetical file would be contained within a hypothetical folder, i.e. whether or not the folder would be an ancestor of the file.
file | file or folder to test (whose existence is not required) |
---|---|
folder | candidate ancestor folder (whose existence is not required) |
Determine if a path refers to an existing file.
Determine whether the provided input string looks like a URL.
Recursively do a directory listing. The list returned is a list of true files (not
directories). Refer to the DirectoryEnumerator
object for a more powerful file
enumerator.
Recursively do a directory listing. The list returned is a list of true files (not
directories). Refer to the DirectoryEnumerator
object for a more powerful file
enumerator.
Parse the elements of a path.
s | a path |
---|
Read the contents of a binary file.@return
IOException |
---|
Read the contents of a binary file that is at most 2 Gb.
file | file to read |
---|---|
maxAllowedSize | optional maximum size, -1 means the max (2 Gb) |
IOException | on IO error, or if the read size exceeds the limit |
---|
Read the contents of a binary file that is at most 2 Gb.
file | file to read |
---|
IOException | on IO error, or if the read size exceeds 2 Gb |
---|
Read a binary file safely. If any error happens, an empty array is returned.
file | input file |
---|
Read a line from the standard input.@return
IOException |
---|
Read a line from the standard input. This method is safe to use; it does not raise, instead returning a null string on error.
Read and reliably return all bytes of an input stream. The stream must be less than 2Gb. The caller is responsible for closing the input stream after reading.@return
IOException |
---|
Read the contents of a stream as a text buffer of UTF-8 encoded strings.@return
IOException |
---|
Read the lines of a text file.@return
IOException |
---|
Read the UTF-8 encoded lines of a text file.
file | input file |
---|
IOException | on IO error |
---|
Read the contents of a stream as a line of strings.
input | the input stream |
---|---|
encoding | the stream encoding |
IOException | on IO error |
---|
Read the lines of a UTF8 encoded text file. On error, the method returns null.
Read the lines of a text file. On error, the method returns null.
file | input file |
---|---|
encoding | file encoding |
Rename a file with options.
src | source file |
---|---|
dst | destination |
mode | define the mechanics of the renaming operations:
|
extension | the new extension, eg ".xyz" |
---|
A safe version of list(FilenameFilter)
.
f | a non-null file object |
---|---|
filter | a file filter |
A safe version of listFiles(FileFilter)
.
f | a non-null file object |
---|---|
filter | a file filter |
Sanitize a path. The returned path is guaranteed to be legal on the current filesystem (FS) if and only if the element being sanitized is a single path element. For full paths elements, sanitization cannot be guaranteed because some characters which could yield to bad paths, cannot be safely sanitized.
Notes:
- the following characters are always sanitized: ? % * | < > "
- if a single path element is provided, the following characters are sanitized as well:
/ \ :
path | a path (absolute or not) |
---|---|
isSinglePathElement | true if the provided path should be treated as a single path element, allowing more aggressive replacements |
replaceBlankCharacters | if true, sanitize all blank characters (per the Unicode definition of a blank character) as well, even though some may be legal |
Attempt to sanitize a path.
Under some conditions, the returned path may be unsafe, eg on Windows: the sanitization of a
path like "x::z" will generate an unsafe path. You must use
sanitizePath(String, boolean, boolean)
to guarantee sanitization.
Set the path of the current working directory.
Note: handle with care, changing the CWD may not impact all subsequent file read and write
operations. It is safer to retrieve and use the CWD with getCwd()
.
Simplify a path. This method is OS-dependent. To call a specific (Unix or Windows) version of
this method, use simplifyPathUnix(String)
or simplifyPathWindows(String)
.
Rules:
- .
and ..
uses are resolved.
- Multiple-path separator uses such as ///
or \\
are resolved.
- Redundant trailing separators are removed.
- The empty path is always resolved to .
.
- If the FS is case-insensitive (e.g. NTFS), the case is not standardized
(Windows).
- Symbolic links are not resolved.
path | input path |
---|
Split a file path into its extension-less part and its extension.
path | a file path |
---|
Split a path into its base part and its filename part.
Unlike getName()
, this method considers both slash and backslash to be valid
path separators.
Write a string to a file using the default character encoding of the current system. Existing file contents will be overwritten.
file | destination file |
---|---|
str | string |
IOException | on error |
---|
Write data to a file whose containing directory structure may not exist. Existing file contents will be overwritten.
IOException |
---|
Write data to a file. Existing file contents will be overwritten.
IOException |
---|
Write a string to a file using the given character encoding of the current system. Existing file contents will be overwritten.
IOException |
---|
Write data to a file whose containing directory structure may not exist. Existing file contents will be overwritten.
createDirs | create the intermediate directories if need be |
---|
IOException |
---|
Write data to a file. Existing file contents will be overwritten.
file | destination file |
---|---|
data | binary data to write |
IOException | on error |
---|
Write a binary file safely.
Write a binary file safely.
Write UTF-8 encoded strings.
IOException |
---|
Write strings.
IOException |
---|
Write strings.
IOException |
---|
Write UTF-8 encoded strings.
IOException |
---|
Write UTF-8 encoded strings. On error, the method returns false.
Write strings. On error, the method returns false.
Write strings. On error, the method returns false.
Write UTF-8 encoded strings. On error, the method returns false.