public final class FileUtil extends Object
Versions exists take Input and OutputStreams as parameters, to allow for copying streams (URL's etc.).
Modifier and Type | Field | Description |
---|---|---|
static int |
BUF_SIZE |
The size of the buffer used for copying
|
Modifier and Type | Method | Description |
---|---|---|
static void |
close(InputStream pInput) |
Tries to close the given stream.
|
static void |
close(OutputStream pOutput) |
Tries to close the given stream.
|
static boolean |
copy(File pFromFile,
File pToFile) |
Copies the fromFile to the toFile location.
|
static boolean |
copy(File pFromFile,
File pToFile,
boolean pOverWrite) |
Copies the fromFile to the toFile location.
|
static boolean |
copy(InputStream pFrom,
OutputStream pTo) |
Copies all data from one stream to another.
|
static boolean |
copy(String pFromFileName,
String pToFileName) |
Copies the fromFile to the toFile location.
|
static boolean |
copy(String pFromFileName,
String pToFileName,
boolean pOverWrite) |
Copies the fromFile to the toFile location.
|
static boolean |
delete(File pFile) |
Deletes the specified file.
|
static boolean |
delete(File pFile,
boolean pForce) |
Deletes the specified file.
|
static boolean |
delete(String pFilename) |
Deletes the specified file.
|
static boolean |
delete(String pFilename,
boolean pForce) |
Deletes the specified file.
|
static String |
getBasename(File pFile) |
Gets the file name of the given file, without the extension (type).
|
static String |
getBasename(String pFileName) |
Gets the file name of the given file, without the extension (type).
|
static String |
getBasename0(String pFileName) |
|
static String |
getDirectoryname(String pPath) |
Extracts the directory path without the filename, from a complete
filename path.
|
static String |
getDirectoryname(String pPath,
char pSeparator) |
Extracts the directory path without the filename, from a complete
filename path.
|
static String |
getExtension(File pFile) |
Gets the file (type) extension of the given file.
|
static String |
getExtension(String pFileName) |
Gets the file (type) extension of the given file.
|
static String |
getFilename(String pPath) |
Extracts the filename of a complete filename path.
|
static String |
getFilename(String pPath,
char pSeparator) |
Extracts the filename of a complete filename path.
|
static long |
getFreeSpace(File pPath) |
|
static String |
getTempDir() |
Gets the default temp directory for the system.
|
static File |
getTempDirFile() |
Gets the default temp directory for the system as a File.
|
static long |
getTotalSpace(File pPath) |
|
static long |
getUsableSpace(File pPath) |
|
static boolean |
isEmpty(File pFile) |
Tests if a file or directory has no content.
|
static File[] |
list(String pFolder) |
Lists all files (and directories) in a specific folder.
|
static File[] |
list(String pFolder,
String pFilenameMask) |
Lists all files (and directories) in a specific folder which are
embraced by the wildcard filename mask provided.
|
static void |
main(String[] pArgs) |
|
static byte[] |
read(File pFile) |
Gets the contents of the given file, as a byte array.
|
static byte[] |
read(InputStream pInput) |
Reads all data from the input stream to a byte array.
|
static byte[] |
read(String pFilename) |
Gets the contents of the given file, as a byte array.
|
static boolean |
rename(File pFrom,
File pTo) |
Renames the specified file, if the destination does not exist.
|
static boolean |
rename(File pFrom,
File pTo,
boolean pOverWrite) |
Renames the specified file.
|
static boolean |
rename(File pFrom,
String pTo) |
Renames the specified file, if the destination does not exist.
|
static boolean |
rename(File pFrom,
String pTo,
boolean pOverWrite) |
Renames the specified file.
|
static boolean |
rename(String pFrom,
String pTo) |
Renames the specified file, if the destination does not exist.
|
static boolean |
rename(String pFrom,
String pTo,
boolean pOverWrite) |
Renames the specified file.
|
static File |
resolve(File pPath) |
|
static File[] |
resolve(File[] pPaths) |
|
static File |
resolve(File pParent,
String pChild) |
|
static File |
resolve(String pPath) |
|
static File |
toFile(URL pURL) |
Creates a
File based on the path part of the URL, for
file-protocol (file: ) based URLs. |
static String |
toHumanReadableSize(long pSizeInBytes) |
Formats the given number to a human readable format.
|
static void |
visitFiles(File pDirectory,
FileFilter pFilter,
com.twelvemonkeys.util.Visitor<File> pVisitor) |
Visits all files in
pDirectory . |
static boolean |
write(File pFile,
byte[] pData) |
Writes the contents from a byte array to a file.
|
static boolean |
write(OutputStream pOutput,
byte[] pData) |
Writes the contents from a byte array to an output stream.
|
static boolean |
write(String pFilename,
byte[] pData) |
Writes the contents from a byte array to a file.
|
public static final int BUF_SIZE
public static void main(String[] pArgs) throws IOException
IOException
public static boolean copy(String pFromFileName, String pToFileName) throws IOException
pFromFileName
- The name of the file to copy frompToFileName
- The name of the file to copy toIOException
- if an i/o error occurs during copypublic static boolean copy(String pFromFileName, String pToFileName, boolean pOverWrite) throws IOException
pFromFileName
- The name of the file to copy frompToFileName
- The name of the file to copy topOverWrite
- Specifies if the toFile should be overwritten, if it
exists.IOException
- if an i/o error occurs during copypublic static boolean copy(File pFromFile, File pToFile) throws IOException
pFromFile
- The file to copy frompToFile
- The file to copy toIOException
- if an i/o error occurs during copypublic static boolean copy(File pFromFile, File pToFile, boolean pOverWrite) throws IOException
pFromFile
- The file to copy frompToFile
- The file to copy topOverWrite
- Specifies if the toFile should be overwritten, if it
exists.true
if the file was copied successfully,
false
if the output file exists, and the
pOwerWrite
parameter is
false
. In all other cases, an
IOExceptio
n is thrown, and the method does not return.IOException
- if an i/o error occurs during copypublic static void close(InputStream pInput)
pInput
- the stream to closepublic static void close(OutputStream pOutput)
pOutput
- the stream to closepublic static boolean copy(InputStream pFrom, OutputStream pTo) throws IOException
pFrom
- The input srteam to copy frompTo
- The output stream to copy toIOException
- if an i/o error occurs during copyIllegalArgumentException
- if either pFrom
or pTo
is
null
public static String getExtension(String pFileName)
'.'
.
If the filename contains no period, null
is returned.pFileName
- the full filename with extensionnull
public static String getExtension(File pFile)
'.'
.
If the filename contains no period, null
is returned.pFile
- the filenull
public static String getBasename(String pFileName)
'.'
.
If the filename contains no period, the complete file name is returned
(same as pFileName
, if the string contains no path elements).pFileName
- the full filename with extensionpublic static String getBasename(File pFile)
'.'
.
If the filename contains no period, pFile.getName()
is returned.pFile
- the filepublic static String getDirectoryname(String pPath)
pPath
- The full filename path.File.getParent()
,
getFilename(java.lang.String)
public static String getDirectoryname(String pPath, char pSeparator)
pPath
- The full filename path.pSeparator
- the separator char used in pPath
File.getParent()
,
getFilename(java.lang.String)
public static String getFilename(String pPath)
pPath
- The full filename path.File.getName()
,
getDirectoryname(java.lang.String)
public static String getFilename(String pPath, char pSeparator)
pPath
- The full filename path.pSeparator
- The file separator.File.getName()
,
getDirectoryname(java.lang.String)
public static boolean isEmpty(File pFile)
pFile
- The file to testtrue
if the file is empty, otherwise
false
.public static File getTempDirFile()
File
, representing the default temp directory.File.createTempFile(java.lang.String, java.lang.String, java.io.File)
public static String getTempDir()
String
, representing the path to the default temp
directory.File.createTempFile(java.lang.String, java.lang.String, java.io.File)
public static byte[] read(String pFilename) throws IOException
pFilename
- the name of the file to get content fromIOException
- if the read operation failspublic static byte[] read(File pFile) throws IOException
pFile
- the file to get content fromIOException
- if the read operation failspublic static byte[] read(InputStream pInput) throws IOException
pInput
- The input stream to read fromIOException
- if an i/o error occurs during read.public static boolean write(OutputStream pOutput, byte[] pData) throws IOException
pOutput
- The output stream to write topData
- The byte array to writetrue
, otherwise an IOException is thrown.IOException
- if an i/o error occurs during write.public static boolean write(File pFile, byte[] pData) throws IOException
pFile
- The file to write topData
- The byte array to writetrue
, otherwise an IOException is thrown.IOException
- if an i/o error occurs during write.public static boolean write(String pFilename, byte[] pData) throws IOException
pFilename
- The name of the file to write topData
- The byte array to writetrue
, otherwise an IOException is thrown.IOException
- if an i/o error occurs during write.public static boolean delete(File pFile, boolean pForce) throws IOException
pFile
- The file to deletepForce
- Forces delete, even if the parameter is a directory, and
is not empty. Be careful!true
, if the file existed and was deleted.IOException
- if an i/o error occurs during delete.public static boolean delete(String pFilename, boolean pForce) throws IOException
pFilename
- The name of file to deletepForce
- Forces delete, even if the parameter is a directory, and
is not empty. Careful!true
, if the file existed and was deleted.IOException
- if deletion failspublic static boolean delete(File pFile) throws IOException
pFile
- The file to deletetrue
, if the file existed and was deleted.IOException
- if deletion failspublic static boolean delete(String pFilename) throws IOException
pFilename
- The name of file to deletetrue
, if the file existed and was deleted.IOException
- if deletion failspublic static boolean rename(File pFrom, File pTo, boolean pOverWrite) throws IOException
pFrom
- The file to renamepTo
- The new filepOverWrite
- Specifies if the tofile should be overwritten, if it
existstrue
, if the file was renamed.FileNotFoundException
- if pFrom
does not exist.IOException
public static boolean rename(File pFrom, File pTo) throws IOException
pFrom
- The file to renamepTo
- The new filetrue
, if the file was renamed.IOException
- if rename failspublic static boolean rename(File pFrom, String pTo, boolean pOverWrite) throws IOException
pFrom
- The file to renamepTo
- The new name of the filepOverWrite
- Specifies if the tofile should be overwritten, if it
existstrue
, if the file was renamed.IOException
- if rename failspublic static boolean rename(File pFrom, String pTo) throws IOException
pFrom
- The file to renamepTo
- The new name of the filetrue
, if the file was renamed.IOException
- if rename failspublic static boolean rename(String pFrom, String pTo, boolean pOverWrite) throws IOException
pFrom
- The name of the file to renamepTo
- The new name of the filepOverWrite
- Specifies if the tofile should be overwritten, if it
existstrue
, if the file was renamed.IOException
- if rename failspublic static boolean rename(String pFrom, String pTo) throws IOException
pFrom
- The name of the file to renamepTo
- The new name of the filetrue
, if the file was renamed.IOException
- if rename failspublic static File[] list(String pFolder) throws FileNotFoundException
pFolder
- The folder to listjava.io.File
objects.FileNotFoundException
- if pFolder
is not a readable filepublic static File[] list(String pFolder, String pFilenameMask) throws FileNotFoundException
pFolder
- The folder to listpFilenameMask
- The wildcard filename maskjava.io.File
objects.FileNotFoundException
- if pFolder
is not a readable fileFile.listFiles(FilenameFilter)
public static File toFile(URL pURL)
File
based on the path part of the URL, for
file-protocol (file:
) based URLs.pURL
- the file:
URLFile
object representing the URLNullPointerException
- if pURL
is null
IllegalArgumentException
- if pURL
is
not a file-protocol URL.File.toURI()
,
File(java.net.URI)
public static long getFreeSpace(File pPath)
public static long getUsableSpace(File pPath)
public static long getTotalSpace(File pPath)
public static String toHumanReadableSize(long pSizeInBytes)
df -h
.pSizeInBytes
- the size in bytepublic static void visitFiles(File pDirectory, FileFilter pFilter, com.twelvemonkeys.util.Visitor<File> pVisitor)
pDirectory
. Optionally filtered through a FileFilter
.pDirectory
- the directory to visit files inpFilter
- the filter, may be null
, meaning all files will be visitedpVisitor
- the visitorIllegalArgumentException
- if either pDirectory
or pVisitor
are null
Visitor
Copyright © 2018. All rights reserved.