public class FileUtils
extends org.apache.commons.io.FileUtils
FileUtils
contains useful methods for dealing with files &
directories.Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ENCODING |
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
cleanDirectory(File directory)
Cleans a directory without deleting it.
|
static void |
closeQuietly(Channel channel)
Unconditionally close a
Channel . |
static void |
copyFile(File srcFile,
File destFile,
boolean preserveFileDate)
Copies a file to a new location.
|
static void |
copyStreamToFile(InputStream input,
File destination) |
static File |
createFile(String filename) |
static boolean |
deleteTree(File dir)
Delete a file tree recursively.
|
static boolean |
deleteTree(File dir,
String[] topLevelDirsToIgnore)
Delete a file tree recursively.
|
static void |
extractResources(String resourceName,
Class callingClass,
File outputDir,
boolean keepParentDirectory)
Extract the specified resource to the given directory for
remain all directory struct
|
static File |
findFileByName(File folder,
String filename,
boolean recursive) |
static Collection<File> |
findFiles(File folder,
org.apache.commons.io.filefilter.IOFileFilter filter,
boolean recursive) |
static long |
getFileTimeStamp(URL url)
Returns a file timestamp.
|
static String |
getResourcePath(String resourceName,
Class callingClass) |
static String |
getResourcePath(String resourceName,
Class callingClass,
String encoding) |
static boolean |
isFile(URL url) |
static boolean |
moveFileWithCopyFallback(File sourceFile,
File destinationFile)
Try to move a file by renaming with backup attempt by copying/deleting via NIO.
|
static File |
newFile(File parent,
String child)
Workaround for JDK bug
4117557.
|
static File |
newFile(String pathName)
Workaround for JDK bug
4117557.
|
static File |
newFile(String parent,
String child)
Workaround for JDK bug
4117557.
|
static File |
newFile(URI uri)
Workaround for JDK bug
4117557.
|
static String |
normalizeFilePath(URL url,
String encoding)
Remove from uri to file prefix file:/
Add if need file separator to begin
|
static File |
openDirectory(String directory) |
static String |
prepareWinFilename(String filename) |
static boolean |
renameFile(File srcFile,
File destFile) |
static boolean |
renameFile(String srcFilePath,
String destFilePath) |
static boolean |
renameFileHard(File srcFile,
File destFile) |
static boolean |
renameFileHard(String srcFilePath,
String destFilePath) |
static void |
safeCopyFile(File in,
File out)
Copy in file to out file
Don't use java.nio as READ_ONLY memory mapped files cannot be deleted
|
static File |
stringToFile(String filename,
String data)
Reads the incoming String into a file at at the given destination.
|
static File |
stringToFile(String filename,
String data,
boolean append) |
static File |
stringToFile(String filename,
String data,
boolean append,
boolean newLine) |
static void |
unzip(File archive,
File directory)
Unzip the specified archive to the given directory
|
byteCountToDisplaySize, byteCountToDisplaySize, checksum, checksumCRC32, contentEquals, contentEqualsIgnoreEOL, convertFileCollectionToFileArray, copyDirectory, copyDirectory, copyDirectory, copyDirectory, copyDirectoryToDirectory, copyFile, copyFile, copyFileToDirectory, copyFileToDirectory, copyInputStreamToFile, copyURLToFile, copyURLToFile, deleteDirectory, deleteQuietly, directoryContains, forceDelete, forceDeleteOnExit, forceMkdir, getFile, getFile, getTempDirectory, getTempDirectoryPath, getUserDirectory, getUserDirectoryPath, isFileNewer, isFileNewer, isFileNewer, isFileOlder, isFileOlder, isFileOlder, isSymlink, iterateFiles, iterateFiles, iterateFilesAndDirs, lineIterator, lineIterator, listFiles, listFiles, listFilesAndDirs, moveDirectory, moveDirectoryToDirectory, moveFile, moveFileToDirectory, moveToDirectory, openInputStream, openOutputStream, openOutputStream, readFileToByteArray, readFileToString, readFileToString, readFileToString, readLines, readLines, readLines, sizeOf, sizeOfAsBigInteger, sizeOfDirectory, sizeOfDirectoryAsBigInteger, toFile, toFiles, touch, toURLs, waitFor, write, write, write, write, write, write, writeByteArrayToFile, writeByteArrayToFile, writeLines, writeLines, writeLines, writeLines, writeLines, writeLines, writeLines, writeLines, writeStringToFile, writeStringToFile, writeStringToFile, writeStringToFile, writeStringToFile, writeStringToFile
public static String DEFAULT_ENCODING
public static void copyStreamToFile(InputStream input, File destination) throws IOException
IOException
public static void cleanDirectory(File directory) throws IOException
directory
- directory to cleanIOException
- in case cleaning is unsuccessfulpublic static File createFile(String filename) throws IOException
IOException
public static File openDirectory(String directory) throws IOException
IOException
public static File stringToFile(String filename, String data) throws IOException
filename
- name and path of the file to createdata
- the contents of the fileIOException
- If the creating or writing to the file stream failspublic static File stringToFile(String filename, String data, boolean append) throws IOException
IOException
public static File stringToFile(String filename, String data, boolean append, boolean newLine) throws IOException
IOException
public static String getResourcePath(String resourceName, Class callingClass) throws IOException
IOException
public static String getResourcePath(String resourceName, Class callingClass, String encoding) throws IOException
IOException
public static String normalizeFilePath(URL url, String encoding) throws UnsupportedEncodingException
url
- file uri to resourceencoding
- - Java encoding namesUnsupportedEncodingException
- if encoding is unknownpublic static boolean deleteTree(File dir)
dir
- dir to wipe outpublic static boolean deleteTree(File dir, String[] topLevelDirsToIgnore)
dir
- dir to wipe outtopLevelDirsToIgnore
- which top-level directories to ignore,
if null or empty then ignoredpublic static void unzip(File archive, File directory) throws IOException
IOException
public static File newFile(String pathName)
java.io.File class
.
No physical file created in this method.File
public static File newFile(URI uri)
java.io.File class
.
No physical file created in this method.File
public static File newFile(File parent, String child)
java.io.File class
.
No physical file created in this method.File
public static File newFile(String parent, String child)
java.io.File class
.
No physical file created in this method.File
public static void extractResources(String resourceName, Class callingClass, File outputDir, boolean keepParentDirectory) throws IOException
resourceName
- - full resource namecallingClass
- - classloader for this class is usedoutputDir
- - extract to this directorykeepParentDirectory
- true - full structure of directories is kept; false - file - removed all directories, directory - started from resource pointIOException
- if any errorspublic static boolean moveFileWithCopyFallback(File sourceFile, File destinationFile)
public static void safeCopyFile(File in, File out) throws IOException
in
- out
- IOException
public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException
This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.
srcFile
- an existing file to copy, must not be null
destFile
- the new file, must not be null
preserveFileDate
- true if the file date of the copy should be the same
as the originalNullPointerException
- if source or destination is null
IOException
- if source or destination is invalidIOException
- if an IO error occurs during copyingFileUtils.copyFileToDirectory(File, File, boolean)
public static void closeQuietly(Channel channel)
Channel
.
Equivalent to Channel.close()
, except any exceptions will be ignored.
This is typically used in finally blocks.
channel
- the Channel to close, may be null or already closedpublic static boolean isFile(URL url)
public static long getFileTimeStamp(URL url)
url
- the file URL.public static Collection<File> findFiles(File folder, org.apache.commons.io.filefilter.IOFileFilter filter, boolean recursive)
Copyright © 2003–2016 MuleSoft, Inc.. All rights reserved.