public final class FileTools
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static interface |
FileTools.FileContentWriter
Strategy interface for writing content to an open, managed
OutputStream. |
| Modifier and Type | Field and Description |
|---|---|
static java.nio.file.CopyOption[] |
EMPTY_COPY_OPTION_ARRAY |
static java.nio.file.LinkOption[] |
EMPTY_LINK_OPTION_ARRAY |
static java.nio.file.StandardOpenOption[] |
EMPTY_STANDARD_OPEN_OPTION_ARRAY |
static java.lang.String |
SLASH |
| Modifier and Type | Method and Description |
|---|---|
static java.io.File |
atomicMove(java.io.File src,
java.io.File dst)
Moves a file atomically within the same filesystem.
|
static java.lang.String |
combineRelativePathsAsNeeded(java.lang.String outerPath,
java.lang.String innerPath)
Combines and normalizes two relative path segments.
|
static boolean |
containsInvalidFilenameChars(java.lang.String filename)
Returns
true if the filename contains characters outside [a-zA-Z0-9_\-]. |
static long |
copy(java.io.InputStream is,
java.io.DataOutput os,
long maxlen) |
static long |
copy(java.io.InputStream is,
java.io.OutputStream os) |
static long |
copy(java.io.InputStream is,
java.io.OutputStream os,
boolean forceFSSync) |
static long |
copy(java.io.InputStream is,
java.io.OutputStream os,
boolean forceFSSync,
boolean closeOutputStream) |
static long |
copy(java.io.InputStream is,
java.io.OutputStream os,
boolean forceFSSync,
boolean closeOutputStream,
long maxlen)
Copies data from an
InputStream to an OutputStream. |
static long |
copy(java.io.InputStream is,
java.io.OutputStream os,
long maxlen) |
static java.lang.String |
copyAndDigest(java.io.InputStream is,
java.io.File f,
java.lang.String algorithm) |
static java.lang.String |
copyAndDigest(java.io.InputStream is,
java.io.File f,
java.lang.String algorithm,
boolean forceFSSync)
Copies an
InputStream to a file while computing its cryptographic hash. |
static void |
copyFile(java.io.File src,
java.io.File dst) |
static void |
copyFile(java.io.File src,
java.io.File dst,
boolean forceFSSync)
Copies a source file to a destination file.
|
static java.io.BufferedReader |
getBufferedReader(java.io.File f) |
static java.io.BufferedReader |
getBufferedReader(java.io.File f,
java.lang.String charsetName) |
static java.io.BufferedReader |
getBufferedReader(java.io.InputStream is) |
static java.io.BufferedReader |
getBufferedReader(java.io.InputStream is,
java.lang.String charsetName) |
static java.io.BufferedReader |
getBufferedReader(java.nio.file.Path path) |
static java.io.BufferedReader |
getBufferedReader(java.nio.file.Path path,
java.lang.String charsetName) |
static java.io.BufferedReader |
getBufferedReader(java.nio.channels.ReadableByteChannel channel) |
static java.io.BufferedReader |
getBufferedReader(java.nio.channels.ReadableByteChannel channel,
java.lang.String charsetName) |
static java.io.BufferedReader |
getBufferedReader(java.lang.String content) |
static java.io.BufferedReader |
getBufferedReader(java.lang.String content,
java.lang.String charsetName) |
static java.lang.String |
getErrortextIfContainsInvalidFilenameChars(java.lang.String filename,
java.lang.String attributeName,
java.lang.String boDescription)
Formats a localized error message if the filename contains invalid characters.
|
static java.lang.String |
getFilenameExtension(java.lang.String filename) |
static java.lang.String |
getFilenameFromPath(java.lang.String path)
Extracts the terminal file name component from a path string.
|
static java.lang.String |
getFilenameNamePart(java.lang.String filename) |
static java.lang.String |
getHashForBytes(java.lang.String algorithm,
byte[] bytes)
Computes the cryptographic checksum of a raw byte array.
|
static java.lang.String |
getHashForFile(java.lang.String algorithm,
java.io.File f)
Computes the cryptographic checksum of a file.
|
static long |
getLinesCount(java.io.File f)
Calculates the number of lines in a text file.
|
static long |
getLinesCount(java.lang.String filename) |
static java.io.File |
getTempDir()
Returns the system default temporary directory (
java.io.tmpdir) as a File. |
static java.nio.file.Path |
getTempDirPath()
Returns the system default temporary directory (
java.io.tmpdir) as a Path. |
static java.lang.String |
inputStreamToString(java.io.InputStream is) |
static java.lang.String |
inputStreamToString(java.io.InputStream is,
java.lang.String charsetName)
Drains an
InputStream into a String using the specified charset. |
static boolean |
isAtomicMoveSupported(java.io.File src,
java.io.File dst)
Probes whether an atomic move can be performed between the locations of
src and dst. |
static boolean |
isDirectory(java.io.File dir)
Verifies that the specified file exists and is a directory.
|
static boolean |
isEmptyDir(java.io.File dir)
Checks if an existing directory contains no files or subdirectories.
|
static boolean |
isNonIgnoredFilenameWithOneOfSuffixes(java.io.File f,
java.util.Collection<java.lang.String> suffixes)
Checks if the file is not ignored and matches one of the accepted extensions.
|
static boolean |
isStandardIgnoreFilename(java.lang.String filename)
Checks if a filename matches default VCS and temporary backup patterns (e.g.
|
static java.lang.String |
loadFile(java.io.File f) |
static java.lang.String |
loadFile(java.io.File f,
java.lang.String charsetName)
Reads an entire file into a string using the specified encoding.
|
static java.lang.String |
loadFile(java.nio.file.Path p) |
static java.lang.String |
loadFile(java.nio.file.Path p,
java.lang.String charsetName)
Reads an entire file path into a string using the specified encoding.
|
static java.lang.String |
loadFile(java.lang.String filename) |
static java.lang.String |
loadFile(java.lang.String filename,
java.lang.String charsetName) |
static java.nio.channels.ReadableByteChannel |
newChannel(java.lang.String content) |
static java.nio.channels.ReadableByteChannel |
newChannel(java.lang.String content,
int bufferSize) |
static java.nio.channels.ReadableByteChannel |
newChannel(java.lang.String content,
int bufferSize,
java.lang.String charsetName) |
static java.nio.channels.ReadableByteChannel |
newChannel(java.lang.String content,
java.lang.String charsetName) |
static java.io.InputStream |
newInputStream(java.io.File f) |
static java.io.InputStream |
newInputStream(java.nio.file.Path path) |
static java.io.InputStream |
newInputStream(java.lang.String content) |
static java.io.InputStream |
newInputStream(java.lang.String content,
java.nio.charset.Charset charset) |
static java.io.InputStream |
newInputStream(java.lang.String content,
int bufferSize) |
static java.io.InputStream |
newInputStream(java.lang.String content,
int bufferSize,
java.nio.charset.Charset charset)
Creates a buffered or unbuffered
InputStream wrapping the provided string bytes. |
static java.io.InputStream |
newInputStream(java.lang.String content,
int bufferSize,
java.lang.String charsetName) |
static java.io.InputStream |
newInputStream(java.lang.String content,
java.lang.String charsetName) |
static java.nio.channels.FileChannel |
newReadingFileChannel(java.io.File f) |
static java.nio.channels.FileChannel |
newReadingFileChannel(java.nio.file.Path path) |
static boolean |
open(java.io.File f)
Opens the specified file with the desktop default application.
|
static java.lang.String |
packageToFilePath(java.lang.String packageName)
Translates a Java package identifier into a filesystem path (replaces dots with separators).
|
static byte[] |
read(java.io.File f)
Reads all bytes from a file.
|
static byte[] |
read(java.io.InputStream is)
Fully drains an
InputStream into a byte array. |
static java.lang.String |
readIntoString(java.io.BufferedReader br)
Reads full content from a
BufferedReader and closes it upon completion. |
static java.lang.String |
readIntoString(java.io.InputStream is) |
static java.lang.String |
readIntoString(java.io.InputStream is,
java.lang.String charsetName)
Drains an InputStream into a String using the given charset.
|
static java.util.List<java.lang.String> |
readLines(java.io.File f) |
static java.util.List<java.lang.String> |
readLines(java.io.File f,
long linesToRead) |
static java.util.List<java.lang.String> |
readLines(java.io.File f,
long linesToRead,
java.lang.String charsetName) |
static java.util.List<java.lang.String> |
readLines(java.io.File f,
java.lang.String charsetName) |
static java.util.List<java.lang.String> |
readLines(java.io.File f,
java.lang.String charsetName,
long linesToRead)
Reads up to
linesToRead lines from a file. |
static java.lang.String |
replaceInvalidFilenameChars(java.lang.String filename)
Replaces any character not matching
[a-zA-Z0-9_\-] with an underscore. |
static void |
saveFile(byte[] bytes,
java.io.File f) |
static void |
saveFile(byte[] bytes,
java.io.File f,
boolean forceFSSync) |
static void |
saveFile(byte[] bytes,
java.io.File f,
boolean forceFSSync,
boolean append) |
static void |
saveFile(byte[] bytes,
java.io.OutputStream fos)
Writes raw bytes to an
OutputStream and closes it. |
static void |
saveFile(java.io.InputStream is,
java.io.File target) |
static void |
saveFile(java.io.InputStream is,
java.io.File target,
boolean forceFSSync) |
static void |
saveFile(java.io.InputStream is,
java.io.File target,
boolean forceFSSync,
boolean append)
Copies an
InputStream to a target file. |
static void |
saveFile(java.lang.String content,
java.io.File f) |
static void |
saveFile(java.lang.String content,
java.io.File f,
java.lang.String charsetName) |
static void |
saveFile(java.lang.String content,
java.io.File f,
java.lang.String charsetName,
boolean forceFSSync) |
static void |
saveFile(java.lang.String content,
java.io.File f,
java.lang.String charsetName,
boolean forceFSSync,
boolean append)
Saves string content to a file with optional hardware sync and append modes.
|
static java.lang.String[] |
splitFilenameAndExtension(java.lang.String filename)
Splits a filename into base name and lowercase extension.
|
static void |
testDir(java.io.File dir)
Deprecated.
Use
isDirectory(File) instead. |
public static final java.nio.file.LinkOption[] EMPTY_LINK_OPTION_ARRAY
public static final java.nio.file.CopyOption[] EMPTY_COPY_OPTION_ARRAY
public static final java.nio.file.StandardOpenOption[] EMPTY_STANDARD_OPEN_OPTION_ARRAY
public static final java.lang.String SLASH
public static java.io.File getTempDir()
java.io.tmpdir) as a File.public static java.nio.file.Path getTempDirPath()
java.io.tmpdir) as a Path.@Deprecated public static void testDir(java.io.File dir)
isDirectory(File) instead.public static boolean isDirectory(java.io.File dir)
dir - the directory to checktrue if the file exists and is a directoryMissingArgumentException - if dir is nulljava.lang.IllegalArgumentException - if dir does not exist or is not a directorypublic static boolean isEmptyDir(java.io.File dir)
dir - the directory to checktrue if the directory is emptyMissingArgumentException - if dir is nulljava.lang.IllegalArgumentException - if dir does not exist or is not a directorypublic static java.lang.String loadFile(java.lang.String filename)
public static java.lang.String loadFile(java.lang.String filename,
java.lang.String charsetName)
public static java.lang.String loadFile(java.io.File f)
public static java.lang.String loadFile(java.io.File f,
java.lang.String charsetName)
f - the file to readcharsetName - character encoding nameMissingArgumentException - if f or charsetName is nullIRuntimeException - wrapping IOException or UnsupportedEncodingExceptionpublic static java.lang.String loadFile(java.nio.file.Path p)
public static java.lang.String loadFile(java.nio.file.Path p,
java.lang.String charsetName)
p - the path to readcharsetName - character encoding nameMissingArgumentException - if p or charsetName is nullIRuntimeException - wrapping IOException or UnsupportedEncodingExceptionpublic static java.lang.String inputStreamToString(java.io.InputStream is)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String inputStreamToString(java.io.InputStream is,
java.lang.String charsetName)
throws java.io.IOException
InputStream into a String using the specified charset.
Note: Does not close the provided is.
is - source stream to readcharsetName - character encoding nameMissingArgumentException - if is or charsetName is nulljava.io.IOException - if an I/O error occurspublic static java.lang.String readIntoString(java.io.InputStream is)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String readIntoString(java.io.InputStream is,
java.lang.String charsetName)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String readIntoString(java.io.BufferedReader br)
throws java.io.IOException
BufferedReader and closes it upon completion.br - reader to drain and closeMissingArgumentException - if br is nulljava.io.IOException - if reading failspublic static java.nio.channels.FileChannel newReadingFileChannel(java.io.File f)
throws java.io.IOException
java.io.IOExceptionpublic static java.nio.channels.FileChannel newReadingFileChannel(java.nio.file.Path path)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.InputStream newInputStream(java.io.File f)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.InputStream newInputStream(java.nio.file.Path path)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.InputStream newInputStream(java.lang.String content,
java.lang.String charsetName)
public static java.io.InputStream newInputStream(java.lang.String content,
int bufferSize,
java.lang.String charsetName)
public static java.io.InputStream newInputStream(java.lang.String content)
public static java.io.InputStream newInputStream(java.lang.String content,
java.nio.charset.Charset charset)
public static java.io.InputStream newInputStream(java.lang.String content,
int bufferSize)
public static java.io.InputStream newInputStream(java.lang.String content,
int bufferSize,
java.nio.charset.Charset charset)
InputStream wrapping the provided string bytes.public static java.nio.channels.ReadableByteChannel newChannel(java.lang.String content)
public static java.nio.channels.ReadableByteChannel newChannel(java.lang.String content,
java.lang.String charsetName)
public static java.nio.channels.ReadableByteChannel newChannel(java.lang.String content,
int bufferSize)
public static java.nio.channels.ReadableByteChannel newChannel(java.lang.String content,
int bufferSize,
java.lang.String charsetName)
public static java.io.BufferedReader getBufferedReader(java.io.File f)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.BufferedReader getBufferedReader(java.io.File f,
java.lang.String charsetName)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.BufferedReader getBufferedReader(java.nio.file.Path path)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.BufferedReader getBufferedReader(java.nio.file.Path path,
java.lang.String charsetName)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.BufferedReader getBufferedReader(java.nio.channels.ReadableByteChannel channel)
throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingExceptionpublic static java.io.BufferedReader getBufferedReader(java.nio.channels.ReadableByteChannel channel,
java.lang.String charsetName)
throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingExceptionpublic static java.io.BufferedReader getBufferedReader(java.lang.String content)
throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingExceptionpublic static java.io.BufferedReader getBufferedReader(java.lang.String content,
java.lang.String charsetName)
throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingExceptionpublic static java.io.BufferedReader getBufferedReader(java.io.InputStream is)
throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingExceptionpublic static java.io.BufferedReader getBufferedReader(java.io.InputStream is,
java.lang.String charsetName)
throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingExceptionpublic static java.util.List<java.lang.String> readLines(java.io.File f,
long linesToRead)
public static java.util.List<java.lang.String> readLines(java.io.File f,
long linesToRead,
java.lang.String charsetName)
public static java.util.List<java.lang.String> readLines(java.io.File f)
public static java.util.List<java.lang.String> readLines(java.io.File f,
java.lang.String charsetName)
public static java.util.List<java.lang.String> readLines(java.io.File f,
java.lang.String charsetName,
long linesToRead)
linesToRead lines from a file.f - the file to readcharsetName - character encodinglinesToRead - maximum lines to read, or -1 for all linesMissingArgumentException - if f or charsetName is nullIRuntimeException - wrapping IOException on failurepublic static byte[] read(java.io.File f)
throws java.io.IOException
null if f is null.java.io.IOExceptionpublic static byte[] read(java.io.InputStream is)
throws java.io.IOException
InputStream into a byte array. Returns null if is is null.java.io.IOExceptionpublic static long getLinesCount(java.lang.String filename)
throws java.io.IOException
java.io.IOExceptionpublic static long getLinesCount(java.io.File f)
throws java.io.IOException
Empty files return 0. A trailing newline is counted as an additional line.
f - file to countMissingArgumentException - if f is nulljava.io.IOException - if reading failspublic static java.lang.String copyAndDigest(java.io.InputStream is,
java.io.File f,
java.lang.String algorithm)
throws java.io.IOException,
java.security.NoSuchAlgorithmException
java.io.IOExceptionjava.security.NoSuchAlgorithmExceptionpublic static java.lang.String copyAndDigest(java.io.InputStream is,
java.io.File f,
java.lang.String algorithm,
boolean forceFSSync)
throws java.io.IOException,
java.security.NoSuchAlgorithmException
InputStream to a file while computing its cryptographic hash.is - source streamf - target filealgorithm - digest algorithm (e.g. "SHA-256", "MD5")forceFSSync - if true, forces OS cache flush to hardware storageMissingArgumentException - if is, f, or algorithm is nulljava.io.IOExceptionjava.security.NoSuchAlgorithmExceptionpublic static java.lang.String getHashForFile(java.lang.String algorithm,
java.io.File f)
throws java.security.NoSuchAlgorithmException,
java.io.IOException
java.security.NoSuchAlgorithmExceptionjava.io.IOExceptionpublic static java.lang.String getHashForBytes(java.lang.String algorithm,
byte[] bytes)
throws java.security.NoSuchAlgorithmException
java.security.NoSuchAlgorithmExceptionpublic static long copy(java.io.InputStream is,
java.io.OutputStream os)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.InputStream is,
java.io.OutputStream os,
boolean forceFSSync)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.InputStream is,
java.io.OutputStream os,
boolean forceFSSync,
boolean closeOutputStream)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.InputStream is,
java.io.OutputStream os,
boolean forceFSSync,
boolean closeOutputStream,
long maxlen)
throws java.io.IOException
InputStream to an OutputStream.
Caution: Closes is on completion or error. os is closed
only if closeOutputStream is true.
is - source stream (always closed)os - target streamforceFSSync - forces hardware sync if os is a FileOutputStreamcloseOutputStream - whether to close os upon completionmaxlen - maximum bytes to transferMissingArgumentException - if is or os is nullIncompleteTransferException - if the stream terminates prematurely before reaching maxlenjava.io.IOException - on general I/O errorspublic static long copy(java.io.InputStream is,
java.io.OutputStream os,
long maxlen)
throws java.io.IOException
java.io.IOExceptionpublic static long copy(java.io.InputStream is,
java.io.DataOutput os,
long maxlen)
throws java.io.IOException
java.io.IOExceptionpublic static void copyFile(java.io.File src,
java.io.File dst)
throws java.io.IOException
java.io.IOExceptionpublic static void copyFile(java.io.File src,
java.io.File dst,
boolean forceFSSync)
throws java.io.IOException
src - source filedst - destination fileforceFSSync - forces hardware storage synchronizationMissingArgumentException - if src or dst is nulljava.io.IOExceptionpublic static java.io.File atomicMove(java.io.File src,
java.io.File dst)
throws java.io.IOException
MissingArgumentException - if src or dst is nulljava.nio.file.AtomicMoveNotSupportedException - if atomic moves are unsupported across mountsjava.io.IOExceptionpublic static boolean isAtomicMoveSupported(java.io.File src,
java.io.File dst)
src and dst.
Creates transient probe files in both directory trees to verify mount point boundaries.
src - source directory/file locationdst - destination directory/file locationtrue if atomic move is supported between both pathspublic static java.lang.String getFilenameExtension(java.lang.String filename)
public static java.lang.String getFilenameNamePart(java.lang.String filename)
public static java.lang.String[] splitFilenameAndExtension(java.lang.String filename)
Hidden files starting with a leading dot without a subsequent period (e.g. .bashrc)
are treated as having no extension (base name intact, extension null).
filename - filename without path[baseName, lowercaseExtensionOrNull]public static boolean isStandardIgnoreFilename(java.lang.String filename)
filename - bare filename without parent path elementstrue if filename matches standard ignore rulespublic static boolean isNonIgnoredFilenameWithOneOfSuffixes(java.io.File f,
java.util.Collection<java.lang.String> suffixes)
f - file to inspectsuffixes - collection of acceptable suffixes (e.g. [".java", ".nrx"]); if null or empty, all non-ignored files passtrue if file is a non-directory, non-ignored file matching the suffix criteriapublic static java.lang.String replaceInvalidFilenameChars(java.lang.String filename)
[a-zA-Z0-9_\-] with an underscore.filename - raw filename, may be nullnullpublic static boolean containsInvalidFilenameChars(java.lang.String filename)
true if the filename contains characters outside [a-zA-Z0-9_\-].filename - filename to check, may be nulltrue if invalid characters are present, false otherwise or if nullpublic static java.lang.String getErrortextIfContainsInvalidFilenameChars(java.lang.String filename,
java.lang.String attributeName,
java.lang.String boDescription)
filename - filename to validateattributeName - attribute name for the error message contextboDescription - business entity description (e.g. "Konto 4223")null if validMissingArgumentException - if any parameter is nullpublic static boolean open(java.io.File f)
public static java.lang.String combineRelativePathsAsNeeded(java.lang.String outerPath,
java.lang.String innerPath)
Example: "com/oashi/bo" + "../erp/Konto.java" → "com/oashi/erp/Konto.java"
outerPath - base relative pathinnerPath - target relative pathMissingArgumentException - if outerPath or innerPath is nullpublic static java.lang.String getFilenameFromPath(java.lang.String path)
public static java.lang.String packageToFilePath(java.lang.String packageName)
public static void saveFile(byte[] bytes,
java.io.OutputStream fos)
OutputStream and closes it.
Caution: Consumes and closes fos regardless of success or failure.
bytes - data payloadfos - target stream (closed upon completion)MissingArgumentException - if fos or bytes is nullIRuntimeException - wrapping IOExceptionpublic static void saveFile(java.lang.String content,
java.io.File f)
public static void saveFile(java.lang.String content,
java.io.File f,
java.lang.String charsetName)
public static void saveFile(java.lang.String content,
java.io.File f,
java.lang.String charsetName,
boolean forceFSSync)
public static void saveFile(java.lang.String content,
java.io.File f,
java.lang.String charsetName,
boolean forceFSSync,
boolean append)
content - string to writef - target filecharsetName - character encodingforceFSSync - if true, invokes FileDescriptor.sync()append - if true, appends instead of truncating existing fileMissingArgumentException - if content, f, or charsetName is nullIRuntimeException - wrapping IOException or AccessDeniedExceptionpublic static void saveFile(byte[] bytes,
java.io.File f)
public static void saveFile(byte[] bytes,
java.io.File f,
boolean forceFSSync)
public static void saveFile(byte[] bytes,
java.io.File f,
boolean forceFSSync,
boolean append)
public static void saveFile(java.io.InputStream is,
java.io.File target)
public static void saveFile(java.io.InputStream is,
java.io.File target,
boolean forceFSSync)
public static void saveFile(java.io.InputStream is,
java.io.File target,
boolean forceFSSync,
boolean append)
InputStream to a target file.
Caution: Consumes and closes is on exit.
is - source stream (closed upon completion)target - destination fileforceFSSync - if true, invokes FileDescriptor.sync()append - if true, appends instead of truncatingMissingArgumentException - if is or target is nullIRuntimeException - wrapping IOExceptionCopyright © 2000-2026 OAshi S.à r.l. All Rights Reserved.