public final class FileUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.nio.charset.Charset |
CHARSET |
static long |
ONE_GB |
static long |
ONE_KB |
static long |
ONE_MB |
static long |
ONE_TB |
Modifier and Type | Method and Description |
---|---|
static void |
append(java.io.File file,
java.lang.String... lines) |
static void |
appendAndSync(java.io.File file,
java.lang.String... lines) |
static void |
clean(java.nio.ByteBuffer buffer) |
static void |
close(java.io.Closeable... cs) |
static void |
close(java.lang.Iterable<? extends java.io.Closeable> cs) |
static void |
closeQuietly(java.lang.AutoCloseable c) |
static void |
closeQuietly(java.io.Closeable c) |
static void |
closeQuietly(java.lang.Iterable<? extends java.lang.AutoCloseable> cs) |
static void |
copyTo(java.io.DataInput in,
java.io.OutputStream out,
int length) |
static void |
copyWithConfirm(java.io.File from,
java.io.File to) |
static void |
copyWithConfirm(java.lang.String from,
java.lang.String to) |
static void |
copyWithOutConfirm(java.lang.String from,
java.lang.String to) |
static java.io.File |
createDeletableTempFile(java.lang.String prefix,
java.lang.String suffix) |
static void |
createDirectory(java.io.File directory) |
static void |
createDirectory(java.lang.String directory) |
static void |
createHardLink(java.io.File from,
java.io.File to) |
static void |
createHardLink(java.lang.String from,
java.lang.String to) |
static void |
createHardLinkWithConfirm(java.io.File from,
java.io.File to) |
static void |
createHardLinkWithConfirm(java.lang.String from,
java.lang.String to) |
static void |
createHardLinkWithoutConfirm(java.lang.String from,
java.lang.String to) |
static java.io.File |
createTempFile(java.lang.String prefix,
java.lang.String suffix) |
static java.io.File |
createTempFile(java.lang.String prefix,
java.lang.String suffix,
java.io.File directory)
Pretty much like
File.createTempFile(String, String, File) , but with
the guarantee that the "random" part of the generated file name between
prefix and suffix is a positive, increasing long value. |
static void |
delete(java.io.File... files) |
static boolean |
delete(java.lang.String file) |
static void |
deleteAsync(java.lang.String file) |
static void |
deleteChildrenRecursive(java.io.File dir)
Deletes all files and subdirectories under "dir".
|
static void |
deleteDirectoryIfEmpty(java.nio.file.Path path)
Deletes the specified directory if it is empty
|
static void |
deleteRecursive(java.io.File dir)
Deletes the specified directory after having deleted its content.
|
static void |
deleteRecursiveOnExit(java.io.File dir)
Schedules deletion of all file and subdirectories under "dir" on JVM shutdown.
|
static void |
deleteRecursiveWithThrottle(java.io.File dir,
com.google.common.util.concurrent.RateLimiter rateLimiter)
Deletes all files and subdirectories under "dir".
|
static void |
deleteWithConfirm(java.io.File file) |
static java.lang.Throwable |
deleteWithConfirm(java.io.File file,
java.lang.Throwable accumulate) |
static java.lang.Throwable |
deleteWithConfirm(java.io.File file,
java.lang.Throwable accumulate,
com.google.common.util.concurrent.RateLimiter rateLimiter) |
static void |
deleteWithConfirm(java.lang.String file) |
static java.lang.Throwable |
deleteWithConfirm(java.lang.String filePath,
java.lang.Throwable accumulate) |
static void |
deleteWithConfirmWithThrottle(java.io.File file,
com.google.common.util.concurrent.RateLimiter rateLimiter) |
static long |
folderSize(java.io.File folder)
Get the size of a directory in bytes
|
static java.lang.String |
getCanonicalPath(java.io.File file) |
static java.lang.String |
getCanonicalPath(java.lang.String filename) |
static java.nio.file.FileStore |
getFileStore(java.nio.file.Path path)
Returns the
FileStore representing the file store where a file
is located. |
static long |
getFreeSpace(java.io.File file)
Returns the number of unallocated bytes on the specified partition.
|
static java.lang.String |
getRelativePath(java.lang.String basePath,
java.lang.String path)
Convert absolute path into a path relative to the base path
|
static java.io.File |
getTempDir() |
static long |
getTotalSpace(java.io.File file)
Returns the size of the specified partition.
|
static long |
getUsableSpace(java.io.File file)
Returns the number of available bytes on the specified partition.
|
static void |
handleCorruptSSTable(CorruptSSTableException e) |
static void |
handleFSError(FSError e) |
static void |
handleFSErrorAndPropagate(FSError e)
handleFSErrorAndPropagate will invoke the disk failure policy error handler,
which may or may not stop the daemon or transports.
|
static boolean |
isContained(java.io.File folder,
java.io.File file)
Return true if file is contained in folder
|
static boolean |
isSubDirectory(java.io.File parent,
java.io.File child) |
static void |
moveRecursively(java.nio.file.Path source,
java.nio.file.Path target)
Moves the contents of a directory to another directory.
|
static long |
parseFileSize(java.lang.String value) |
static java.util.List<java.lang.String> |
readLines(java.io.File file) |
static void |
renameWithConfirm(java.io.File from,
java.io.File to) |
static void |
renameWithConfirm(java.lang.String from,
java.lang.String to) |
static void |
renameWithOutConfirm(java.lang.String from,
java.lang.String to) |
static void |
replace(java.io.File file,
java.lang.String... lines) |
static void |
setFSErrorHandler(FSErrorHandler handler) |
static java.lang.String |
stringifyFileSize(double value) |
static void |
truncate(java.lang.String path,
long size) |
static void |
write(java.io.File file,
java.util.List<java.lang.String> lines,
java.nio.file.StandardOpenOption... options)
Write lines to a file adding a newline to the end of each supplied line using the provided open options.
|
public static final java.nio.charset.Charset CHARSET
public static final long ONE_KB
public static final long ONE_MB
public static final long ONE_GB
public static final long ONE_TB
public static java.io.File getTempDir()
public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File directory)
File.createTempFile(String, String, File)
, but with
the guarantee that the "random" part of the generated file name between
prefix
and suffix
is a positive, increasing long
value.public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix)
public static java.io.File createDeletableTempFile(java.lang.String prefix, java.lang.String suffix)
public static void createHardLink(java.lang.String from, java.lang.String to)
public static void createHardLink(java.io.File from, java.io.File to)
public static void createHardLinkWithConfirm(java.io.File from, java.io.File to)
public static void createHardLinkWithConfirm(java.lang.String from, java.lang.String to)
public static void createHardLinkWithoutConfirm(java.lang.String from, java.lang.String to)
public static java.lang.Throwable deleteWithConfirm(java.lang.String filePath, java.lang.Throwable accumulate)
public static java.lang.Throwable deleteWithConfirm(java.io.File file, java.lang.Throwable accumulate)
public static java.lang.Throwable deleteWithConfirm(java.io.File file, java.lang.Throwable accumulate, com.google.common.util.concurrent.RateLimiter rateLimiter)
public static void deleteWithConfirm(java.lang.String file)
public static void deleteWithConfirm(java.io.File file)
public static void deleteWithConfirmWithThrottle(java.io.File file, com.google.common.util.concurrent.RateLimiter rateLimiter)
public static void copyWithOutConfirm(java.lang.String from, java.lang.String to)
public static void copyWithConfirm(java.lang.String from, java.lang.String to)
public static void copyWithConfirm(java.io.File from, java.io.File to)
public static void renameWithOutConfirm(java.lang.String from, java.lang.String to)
public static void renameWithConfirm(java.lang.String from, java.lang.String to)
public static void renameWithConfirm(java.io.File from, java.io.File to)
public static void truncate(java.lang.String path, long size)
public static void closeQuietly(java.io.Closeable c)
public static void closeQuietly(java.lang.AutoCloseable c)
public static void close(java.io.Closeable... cs) throws java.io.IOException
java.io.IOException
public static void close(java.lang.Iterable<? extends java.io.Closeable> cs) throws java.io.IOException
java.io.IOException
public static void closeQuietly(java.lang.Iterable<? extends java.lang.AutoCloseable> cs)
public static java.lang.String getCanonicalPath(java.lang.String filename)
public static java.lang.String getCanonicalPath(java.io.File file)
public static boolean isContained(java.io.File folder, java.io.File file)
public static java.lang.String getRelativePath(java.lang.String basePath, java.lang.String path)
public static void clean(java.nio.ByteBuffer buffer)
public static void createDirectory(java.lang.String directory)
public static void createDirectory(java.io.File directory)
public static boolean delete(java.lang.String file)
public static void delete(java.io.File... files)
public static void deleteAsync(java.lang.String file)
public static long parseFileSize(java.lang.String value)
public static java.lang.String stringifyFileSize(double value)
public static void deleteRecursiveWithThrottle(java.io.File dir, com.google.common.util.concurrent.RateLimiter rateLimiter)
dir
- Directory to be deletedFSWriteError
- if any part of the tree cannot be deletedpublic static void deleteRecursive(java.io.File dir)
dir
- Directory to be deletedFSWriteError
- if any part of the tree cannot be deletedpublic static void deleteChildrenRecursive(java.io.File dir)
dir
- Directory to be deletedFSWriteError
- if any part of the tree cannot be deletedpublic static void deleteRecursiveOnExit(java.io.File dir)
dir
- Directory to be deletedpublic static void handleCorruptSSTable(CorruptSSTableException e)
public static void handleFSError(FSError e)
public static void handleFSErrorAndPropagate(FSError e)
e
- A filesystem errorpublic static long folderSize(java.io.File folder)
folder
- The directory for which we need size.public static void copyTo(java.io.DataInput in, java.io.OutputStream out, int length) throws java.io.IOException
java.io.IOException
public static boolean isSubDirectory(java.io.File parent, java.io.File child) throws java.io.IOException
java.io.IOException
public static void append(java.io.File file, java.lang.String... lines)
public static void appendAndSync(java.io.File file, java.lang.String... lines)
public static void replace(java.io.File file, java.lang.String... lines)
public static void write(java.io.File file, java.util.List<java.lang.String> lines, java.nio.file.StandardOpenOption... options)
file
- lines
- options
- public static java.util.List<java.lang.String> readLines(java.io.File file)
public static void setFSErrorHandler(FSErrorHandler handler)
public static long getTotalSpace(java.io.File file)
This method handles large file system by returning Long.MAX_VALUE
if the size overflow.
See JDK-8179320 for more information.
file
- the partition0L
if the abstract pathname does not name a partitionpublic static long getFreeSpace(java.io.File file)
This method handles large file system by returning Long.MAX_VALUE
if the number of unallocated bytes
overflow. See JDK-8179320 for more information
file
- the partition0L
if the abstract pathname does not name a partition.public static long getUsableSpace(java.io.File file)
This method handles large file system by returning Long.MAX_VALUE
if the number of available bytes
overflow. See JDK-8179320 for more information
file
- the partition0L
if the abstract pathname does not name a partition.public static java.nio.file.FileStore getFileStore(java.nio.file.Path path) throws java.io.IOException
FileStore
representing the file store where a file
is located. This FileStore
handles large file system by returning Long.MAX_VALUE
from FileStore#getTotalSpace()
, FileStore#getUnallocatedSpace()
and FileStore#getUsableSpace()
it the value is bigger than Long.MAX_VALUE
. See JDK-8162520
for more information.path
- the path to the filejava.io.IOException
public static void moveRecursively(java.nio.file.Path source, java.nio.file.Path target) throws java.io.IOException
Once a file has been copied to the target directory it will be deleted from the source directory. If a file already exists in the target directory a warning will be logged and the file will not be deleted.
source
- the directory containing the files to movetarget
- the directory where the files must be movedjava.io.IOException
public static void deleteDirectoryIfEmpty(java.nio.file.Path path) throws java.io.IOException
path
- the path to the directoryjava.io.IOException
Copyright © 2009- The Apache Software Foundation