public class FileUtils extends Object
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
appendText(String fileName,
String line)
Appends the given line at the end of given text file.
|
static void |
appendText(String fileName,
StringBuffer buffer) |
static void |
copy(File fin,
File fout)
Copies a file.
|
static void |
copy(InputStream in,
FileOutputStream out,
long size) |
static void |
copy(InputStream in,
OutputStream os,
long size) |
static void |
copy(String from,
String to)
Copies a file.
|
static void |
copyFile(File fin,
File fout)
This method is used to copy a given file to another file
using the buffer sixe specified
|
static void |
copyTree(File din,
File dout)
Copies the entire tree to a new location.
|
static void |
copyWithoutClose(InputStream in,
FileOutputStream out,
long size) |
static File |
createTempFile(File directory) |
static boolean |
deleteFile(File f)
Delete a file.
|
static boolean |
deleteFileMaybe(File f)
Wrapper for File.delete
This version will return true if the file does not exist when the method returns.
|
static void |
deleteFileNowOrLater(File f)
Delete a file.
|
static boolean |
deleteFileWithWaitLoop(File f)
Delete a file.
|
static File[] |
findFilesInDir(File dir,
String regexp)
Find files matching the regular expression in the given directory
|
static String |
formatFileCollection(Collection<File> files)
Creates a String listing the absolute paths of files, separated by
the platform's line separator.
|
static Set<File> |
getAllFilesAndDirectoriesUnder(File directory) |
static Set<File> |
getAllFilesUnder(File directory,
FilenameFilter filenameFilter)
Return a set of all the files (File objects) under the directory specified, with
relative pathnames filtered with the filename filter (can be null for all files).
|
static Set<File> |
getAllFilesUnder(File directory,
FilenameFilter filenameFilter,
boolean relativize) |
static File |
getDirectory(File f) |
static String |
getIllegalFilenameCharacters() |
static File |
getManagedFile(String file,
File dir)
If the path dir/file does not exist, look for it in the classpath.
|
static File |
getParentFile(File f)
The method, java.io.File.getParentFile() does not necessarily do what
you would think it does.
|
static boolean |
hasExtension(File f,
String ext) |
static boolean |
hasExtension(String filename,
String ext) |
static boolean |
hasExtensionIgnoreCase(File f,
String ext) |
static boolean |
hasExtensionIgnoreCase(String filename,
String ext) |
static boolean |
isFriendlyFilename(String filename) |
static boolean |
isJar(File f) |
static boolean |
isJar(String filename) |
static boolean |
isLegalFilename(String filename) |
static boolean |
isZip(File f) |
static boolean |
isZip(String filename) |
static void |
liquidate(File parent) |
static File[] |
listAllFiles(File dirName,
String ext)
Returns an array of abstract pathnames that matches with the given
file extension.
|
static File[] |
listFiles(File f) |
static File[] |
listFiles(File f,
FileFilter ff) |
static File[] |
listFiles(File f,
FilenameFilter fnf) |
static String |
makeForwardSlashes(String inputStr)
Returns a String with uniform slashes such that all the
occurances of '\\' are replaced with '/'.
|
static String |
makeFriendlyFilename(String filename) |
static String |
makeFriendlyFilenameExtension(String filename) |
static String |
makeFriendlyFilenameNoExtension(String filename) |
static String |
makeLegalFilename(String filename) |
static String |
makeLegalNoBlankFileName(String filename) |
static boolean |
mkdirsMaybe(File f)
Wrapper for File.mkdirs
This version will return true if the directory exists when the method returns.
|
static FileOutputStream |
openFileOutputStream(File out)
Opens a stream to the specified output file, retrying if necessary.
|
static String |
quoteString(String s)
Given a string (typically a path), quote the string such that spaces
are protected from interpretation by a Unix or Windows command shell.
|
static String |
readSmallFile(File file) |
static String |
readSmallFile(String fileName)
A utility routine to read a text file efficiently and return
the contents as a String.
|
static File |
relativize(File parent,
File child)
Given a directory and a fully-qualified file somewhere
under that directory, return the portion of the child
that is relative to the parent.
|
static boolean |
renameFile(File fromFile,
File toFile)
Rename, running gc on Windows if needed to try to force open streams to close.
|
static byte[] |
resourceToBytes(String resourceName)
Read in the given resourceName as a resource, and convert to a byte array
|
static String |
resourceToString(String resourceName)
Read in the given resourceName as a resource, and convert to a String
|
static String |
revertFriendlyFilename(String filename) |
static String |
revertFriendlyFilenameExtension(String filename) |
static File |
safeGetCanonicalFile(File f) |
static String |
safeGetCanonicalPath(File f) |
static boolean |
safeIsDirectory(File f) |
static boolean |
safeIsDirectory(String s) |
static boolean |
safeIsRealDirectory(File f) |
static boolean |
safeIsRealDirectory(String s) |
static List<File> |
searchDir(File dirName,
String ext)
Returns a list of abstract pathnames that matches with the given
file extension.
|
static void |
setFileProperties() |
static boolean |
whack(File parent)
Deletes a directory and its contents.
|
static boolean |
whack(File parent,
Collection<File> undeletedFiles)
Deletes a directory and its contents.
|
static void |
writeStringToFile(String s,
File f)
Write the String to a file.
|
public static void setFileProperties()
public static File getParentFile(File f)
f
- public static boolean mkdirsMaybe(File f)
f
- The file pointing to the directory to be createdpublic static boolean deleteFileMaybe(File f)
f
- The file to be deletedpublic static File[] listFiles(File f, FileFilter ff)
public static File[] listFiles(File f, FilenameFilter fnf)
public static boolean safeIsDirectory(File f)
public static boolean safeIsRealDirectory(String s)
public static boolean safeIsRealDirectory(File f)
public static boolean safeIsDirectory(String s)
public static boolean isLegalFilename(String filename)
public static boolean isFriendlyFilename(String filename)
public static String makeFriendlyFilenameNoExtension(String filename)
public static String revertFriendlyFilenameExtension(String filename)
public static void liquidate(File parent)
public static boolean isJar(String filename)
public static boolean isZip(String filename)
public static boolean isJar(File f)
public static boolean isZip(File f)
public static boolean whack(File parent)
parent
- the File at the top of the subtree to deletepublic static boolean whack(File parent, Collection<File> undeletedFiles)
parent
- the File at the top of the subtree to deletepublic static void deleteFileNowOrLater(File f)
f
- file to deletepublic static boolean deleteFileWithWaitLoop(File f)
f
- file to deletepublic static boolean deleteFile(File f)
f
- file to deletepublic static FileOutputStream openFileOutputStream(File out) throws IOException
out
- the output File for which a stream is neededIOException
- for any errors opening the streampublic static Set<File> getAllFilesUnder(File directory, FilenameFilter filenameFilter) throws IOException
IOException
public static Set<File> getAllFilesUnder(File directory, FilenameFilter filenameFilter, boolean relativize) throws IOException
IOException
public static Set<File> getAllFilesAndDirectoriesUnder(File directory) throws IOException
IOException
public static File relativize(File parent, File child)
public static String formatFileCollection(Collection<File> files)
files
- the Collection of File objects to be listedpublic static File[] listAllFiles(File dirName, String ext)
dirName
- dir name under which search will beginext
- file extension to look forpublic static List<File> searchDir(File dirName, String ext)
dirName
- dir name under which search will beginext
- file extension to look forpublic static void copy(String from, String to) throws IOException
from
- Name of file to copyto
- Name of new fileIOException
- if an error while copying the contentpublic static void copy(File fin, File fout) throws IOException
fin
- File to copyfout
- New fileIOException
- if an error while copying the contentpublic static void copyTree(File din, File dout) throws IOException
din
- File pointing at root of tree to copydout
- File pointing at root of new treeIOException
- if an error while copying the contentpublic static String makeForwardSlashes(String inputStr)
inputStr
- non null String does not contain `\\` character
public static String quoteString(String s)
s
- input stringpublic static String getIllegalFilenameCharacters()
public static void copyFile(File fin, File fout) throws IOException
fin
- the source filefout
- the destination fileIOException
public static void copy(InputStream in, FileOutputStream out, long size) throws IOException
IOException
public static void copyWithoutClose(InputStream in, FileOutputStream out, long size) throws IOException
IOException
public static void copy(InputStream in, OutputStream os, long size) throws IOException
IOException
public static boolean renameFile(File fromFile, File toFile)
fromFile
- to be renamedtoFile
- name for the renamed filepublic static void appendText(String fileName, String line) throws RuntimeException
fileName
- name of the text file that needs to be appended toline
- the line to append toRuntimeException
- in case of any error - that makes it callable
from a code not within try-catch. Note that NPE will be thrown if either
argument is null.
Note that this method is not tested with String containing characters
with 2 bytes.public static void appendText(String fileName, StringBuffer buffer) throws IOException, FileNotFoundException
IOException
FileNotFoundException
public static String readSmallFile(String fileName) throws IOException, FileNotFoundException
small
.fileName
- String representing absolute path of the fileIOException
- if there is an i/o error.FileNotFoundException
- if the file could not be foundpublic static String readSmallFile(File file) throws IOException
IOException
public static File getManagedFile(String file, File dir) throws IOException
file
- - path to look fordir
- - directory where the path file should existIOException
public static void writeStringToFile(String s, File f) throws IOException
s
- The String to write to the filef
- The file to write the String toIOException
- if any errorspublic static File[] findFilesInDir(File dir, String regexp)
dir
- the directory to searchregexp
- the regular expression patternpublic static String resourceToString(String resourceName)
resourceName
- public static byte[] resourceToBytes(String resourceName)
resourceName
- Copyright © 2018. All rights reserved.