public final class FileUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
FileUtil.CPUArchitecture |
static class |
FileUtil.OperatingSystem |
Modifier and Type | Field and Description |
---|---|
static int |
BUFFER_SIZE |
static FileUtil.CPUArchitecture |
detectedArch
Caveat: Sometimes this may not be entirely accurate, e.g.
|
static FileUtil.OperatingSystem |
detectedOS |
Constructor and Description |
---|
FileUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
copy(java.io.InputStream source,
java.io.OutputStream destination,
long length)
Copies
length bytes from the source input stream to the
destination output stream. |
static boolean |
copyFile(java.io.File copyFrom,
java.io.File copyTo) |
static java.io.File |
createTempFile(java.lang.String prefix,
java.lang.String suffix,
java.io.File directory)
Create a temp file in a specific directory.
|
static boolean |
equals(java.io.File a,
java.io.File b) |
static boolean |
equalStreams(java.io.InputStream a,
java.io.InputStream b,
long size) |
static long |
estimateUsage(java.io.File file,
long flen)
Guesstimate real disk usage for a file with a given filename, of a given length.
|
static void |
fill(java.io.OutputStream os,
long length)
Write hard to identify random data to the OutputStream.
|
static void |
fill(java.io.OutputStream os,
java.util.Random random,
long length)
Deprecated.
|
static long |
findLength(java.io.InputStream source)
Find the length of an input stream.
|
static java.io.File |
getCanonicalFile(java.io.File file) |
static java.nio.charset.Charset |
getFileEncodingCharset()
Returns the Charset which is equal to the "file.encoding" property.
|
static LineReadingInputStream |
getLogTailReader(java.io.File logfile,
long byteLimit)
Returns a line reading stream for the content of
logfile . |
static boolean |
isParent(java.io.File poss,
java.io.File filename)
Is possParent a parent of filename?
Why doesn't java provide this? :(
|
static boolean |
moveTo(java.io.File orig,
java.io.File dest,
boolean overwrite)
Like renameTo(), but can move across filesystems, by copying the data.
|
static java.lang.StringBuilder |
readUTF(java.io.File file)
Reads the entire content of a file as UTF-8 and returns it.
|
static java.lang.StringBuilder |
readUTF(java.io.File file,
long offset)
Reads the content of a file as UTF-8, starting at a specified offset, and returns it.
|
static java.lang.StringBuilder |
readUTF(java.io.InputStream stream)
Reads the entire content of a stream as UTF-8 and returns it.
|
static java.lang.StringBuilder |
readUTF(java.io.InputStream stream,
long offset)
Reads the content of a stream as UTF-8, starting at a specified offset, and returns it.
|
static boolean |
removeAll(java.io.File wd)
Delete everything in a directory.
|
static boolean |
renameTo(java.io.File orig,
java.io.File dest) |
static java.lang.String |
sanitize(java.lang.String fileName) |
static java.lang.String |
sanitize(java.lang.String filename,
java.lang.String mimeType) |
static java.lang.String |
sanitizeFileName(java.lang.String fileName,
FileUtil.OperatingSystem targetOS,
java.lang.String extraChars)
Sanitizes the given filename to be valid on the given operating system.
|
static java.lang.String |
sanitizeFileNameWithExtras(java.lang.String fileName,
java.lang.String extraChars) |
static void |
secureDelete(java.io.File file) |
static void |
secureDelete(java.io.File file,
java.util.Random random)
Deprecated.
|
static boolean |
secureDeleteAll(java.io.File wd) |
static boolean |
setOwnerPerm(java.io.File f,
boolean r,
boolean w,
boolean x)
Set owner-only permissions on the given file.
|
static boolean |
setOwnerRW(java.io.File f)
Set owner-only RW on the given file.
|
static boolean |
setOwnerRWX(java.io.File f)
Set owner-only RWX on the given file.
|
static void |
skipFully(java.io.InputStream is,
long skip)
Reliably skip a number of bytes or throw.
|
static boolean |
writeTo(java.io.InputStream input,
java.io.File target) |
public static final int BUFFER_SIZE
public static final FileUtil.OperatingSystem detectedOS
public static final FileUtil.CPUArchitecture detectedArch
public static LineReadingInputStream getLogTailReader(java.io.File logfile, long byteLimit) throws java.io.IOException
logfile
. The stream will
contain at most byteLimit
bytes. If byteLimit
is less than the
size of logfile
, the first part of the file will be skipped. If this leaves a
partial line at the beginning of the content to read, that partial line will also be
skipped.logfile
- The file to openbyteLimit
- The maximum number of bytes to readjava.io.IOException
- if an I/O error occurspublic static java.nio.charset.Charset getFileEncodingCharset()
public static long estimateUsage(java.io.File file, long flen)
public static boolean isParent(java.io.File poss, java.io.File filename)
public static java.io.File getCanonicalFile(java.io.File file)
public static java.lang.StringBuilder readUTF(java.io.File file) throws java.io.FileNotFoundException, java.io.IOException
file
- The file to readfile
java.io.FileNotFoundException
- if file
cannot be openedjava.io.IOException
- if an I/O error occurspublic static java.lang.StringBuilder readUTF(java.io.File file, long offset) throws java.io.FileNotFoundException, java.io.IOException
file
- The file to readoffset
- The point in file
at which to start readingfile
, starting at offset
java.io.FileNotFoundException
- if file
cannot be openedjava.io.IOException
- if an I/O error occurspublic static java.lang.StringBuilder readUTF(java.io.InputStream stream) throws java.io.IOException
stream
- The stream to readstream
java.io.IOException
- if an I/O error occurspublic static java.lang.StringBuilder readUTF(java.io.InputStream stream, long offset) throws java.io.IOException
stream
- The stream to readoffset
- The point in stream
at which to start readingstream
, starting at offset
java.io.IOException
- if an I/O error occurspublic static void skipFully(java.io.InputStream is, long skip) throws java.io.IOException
java.io.IOException
public static boolean writeTo(java.io.InputStream input, java.io.File target) throws java.io.FileNotFoundException, java.io.IOException
java.io.FileNotFoundException
java.io.IOException
public static boolean renameTo(java.io.File orig, java.io.File dest)
public static boolean moveTo(java.io.File orig, java.io.File dest, boolean overwrite)
orig
- dest
- overwrite
- public static java.lang.String sanitizeFileName(java.lang.String fileName, FileUtil.OperatingSystem targetOS, java.lang.String extraChars)
public static java.lang.String sanitize(java.lang.String fileName)
public static java.lang.String sanitizeFileNameWithExtras(java.lang.String fileName, java.lang.String extraChars)
public static java.lang.String sanitize(java.lang.String filename, java.lang.String mimeType)
public static long findLength(java.io.InputStream source) throws java.io.IOException
InputStream.read(byte[])
method returns
-1
, thus signalling the end of the stream.source
- The input stream to find the length ofjava.io.IOException
- if an I/O error occurspublic static void copy(java.io.InputStream source, java.io.OutputStream destination, long length) throws java.io.IOException
length
bytes from the source input stream to the
destination output stream. If length
is -1
as much bytes as possible will be copied (i.e. until
InputStream.read()
returns -1
to signal the end of
the stream).source
- The input stream to read fromdestination
- The output stream to write tolength
- The number of bytes to copyjava.io.IOException
- if an I/O error occurspublic static boolean secureDeleteAll(java.io.File wd) throws java.io.IOException
java.io.IOException
public static boolean removeAll(java.io.File wd)
public static void secureDelete(java.io.File file) throws java.io.IOException
java.io.IOException
@Deprecated public static void secureDelete(java.io.File file, java.util.Random random) throws java.io.IOException
java.io.IOException
public static boolean setOwnerRW(java.io.File f)
public static boolean setOwnerRWX(java.io.File f)
public static boolean setOwnerPerm(java.io.File f, boolean r, boolean w, boolean x)
public static boolean equals(java.io.File a, java.io.File b)
public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix, java.io.File directory) throws java.io.IOException
java.io.IOException
public static boolean copyFile(java.io.File copyFrom, java.io.File copyTo)
public static void fill(java.io.OutputStream os, long length) throws java.io.IOException
os
- The stream to write to.length
- The number of bytes to write.java.io.IOException
- If unable to write to the stream.@Deprecated public static void fill(java.io.OutputStream os, java.util.Random random, long length) throws java.io.IOException
java.io.IOException
public static boolean equalStreams(java.io.InputStream a, java.io.InputStream b, long size) throws java.io.IOException
java.io.IOException