public class FileUtil extends Object
Modifier and Type | Class and Description |
---|---|
protected static class |
FileUtil.DirectoryFileFilter
simple file filter that lists all directories
|
protected static class |
FileUtil.ExpressionFileFilter
simple file filter that lists all files that match a regular expression
|
static class |
FileUtil.ExtensionFileFilter
Inner class *********************** UtilFileFilter
|
Constructor and Description |
---|
FileUtil() |
Modifier and Type | Method and Description |
---|---|
static File |
cleanDots(File file)
remove any internal "../" "./" and "//" from a url
|
static File |
cleanURL(File f) |
static boolean |
copyBytes(byte[] buf,
File toFile)
copy a buffer to the end of a file, creating it if necessary
|
static boolean |
copyFile(File fromFile,
File toFile)
copy a file, unless fromFile and toFile are equal
|
static File |
copyFileToDir(File fromFile,
File toDir)
copies a File to directory if toFile exists, it is brutally overwritten unless fromFile equals toFile
|
static boolean |
createNewFile(File file)
similar to File.createFile but also creates any required directories
|
static boolean |
deleteAll(File dirToZapp)
very brutal tree zapper that will delete a file or directory tree recursively
|
static File |
ensureFileInDir(File fromFile,
File toDir)
copies a File to directory if and only if toFile does not exist
|
static boolean |
equals(File file1,
File file2)
checks the equivalence of files - todo os specific behavior (just in case)
|
static byte[] |
fileToByteArray(File file)
read a file into a byte array
|
static boolean |
forceDelete(File file)
forces deletion of a file
|
static boolean |
forceDelete(File file,
int loops)
forces deletion of a file
|
static File |
getAuxDir(File hotFile)
get any auxiliary directory with the same name as a file
|
static BufferedInputStream |
getBufferedInputStream(File file)
create a buffered input stream for a file
|
static BufferedOutputStream |
getBufferedOutputStream(File file)
create a buffered output stream for a file
|
static BufferedOutputStream |
getBufferedOutputStream(File file,
boolean append)
create a buffered output stream for a file
|
static File |
getCreateDirectory(String newDir)
create a new directory and return null if the directory could not be created
|
static String |
getExtension(File file) |
static byte[] |
getFastMD5(File f,
int maxSize)
get an md5 from a file that reads at most 2*maxSize bytes of which maxSize are from the front and maxSize are from the back
|
static File |
getFileInDirectory(File dir,
File localFile)
returns a File object corresponding to an instance of localFile placed in dir - No OS calls are made and File is NOT created
|
static boolean |
isAbsoluteFile(File f)
check whether a file is absolute
|
static boolean |
isAbsoluteFile(String f) |
static boolean |
isDirectory(File f)
check whether a file is a directory.
|
static boolean |
isDirectory(String s)
check whether a file is a directory.
|
static boolean |
isWindows() |
static File[] |
listDirectories(File dir)
list all direct child directories
|
static Vector<File> |
listFilesInTree(File dir,
FileFilter filter)
list all files matching given regexp
|
static Vector<File> |
listFilesInTree(File dir,
String expression)
list all files matching given regexp
|
static File[] |
listFilesWithExpression(File dir,
String expression)
list all files matching given regexp
|
static File[] |
listFilesWithExtension(File dir,
String extension)
list all files with a given extension (directories are skipped
|
static boolean |
moveFile(File fromFile,
File toFile)
moves a File by trying to rename, if this fails, a copy with subsequent delete is performed. if toFile exists, it is brutally overwritten
|
static File |
moveFileToDir(File fromFile,
File toDir)
moves a File to directory by trying to rename, if this fails, a copy with subsequent delete is performed. if toFile exists, it is brutally overwritten
|
static File |
newExtension(File f,
String newExt)
create a File object with a new extension
|
static File |
streamToFile(InputStream fis,
File fil) |
static File |
streamToFile(InputStream fis,
String fileName)
dump a stream to a newly created file
|
static MyPair<File,byte[]> |
streamToMD5File(InputStream fis,
File fil)
same as streanToFile but also calculates the md5 hash of the stream
|
static File |
writeFile(IStreamWriter w,
File file)
write to a file
|
public static File getAuxDir(File hotFile)
hotFile
- public static File[] listFilesWithExtension(File dir, String extension)
dir
- the directory to searchextension
- comma separated list of extensions to check for (null = list all)public static File[] listFilesWithExpression(File dir, String expression)
dir
- the directory to searchexpression
- regular expression - uses the simplified syntaxpublic static Vector<File> listFilesInTree(File dir, FileFilter filter)
dir
- the directory to searchfilter
- the filter to apply to filespublic static Vector<File> listFilesInTree(File dir, String expression)
dir
- the directory to searchexpression
- comma separated list of regular expression of a tree with slashes separating directoriespublic static File[] listDirectories(File dir)
dir
- the directory to searchpublic static boolean deleteAll(File dirToZapp)
dirToZapp
- the file directory to utterly anihilatepublic static File streamToFile(InputStream fis, String fileName)
fis
- the inputstream to readfileName
- the file to stream topublic static File writeFile(IStreamWriter w, File file)
file
- the file to writew
- the writer to write topublic static File newExtension(File f, String newExt)
f
- the file, if null always returns nullnewExt
- the new extensionfor details of handling null etc.
public static File streamToFile(InputStream fis, File fil)
fis
- the InputStream to read - if null nothing happensfil
- the file to stream topublic static byte[] getFastMD5(File f, int maxSize)
f
- maxSize
- if<=0 always use entir lengthpublic static MyPair<File,byte[]> streamToMD5File(InputStream fis, File fil)
fis
- the InputStream to read - if null nothing happensfil
- the file to stream topublic static byte[] fileToByteArray(File file)
file
- the file to read into a byte arraypublic static File moveFileToDir(File fromFile, File toDir)
fromFile
- the File to movetoDir
- the Directory to move topublic static boolean moveFile(File fromFile, File toFile)
fromFile
- the File to movetoFile
- the File to createpublic static boolean copyBytes(byte[] buf, File toFile)
buf
- the source buffertoFile
- the destination Filepublic static boolean copyFile(File fromFile, File toFile)
fromFile
- the source FiletoFile
- the destination Filepublic static File ensureFileInDir(File fromFile, File toDir)
fromFile
- the File to movetoDir
- the Directory to move topublic static File cleanDots(File file)
file
- the file to cleanpublic static File copyFileToDir(File fromFile, File toDir)
fromFile
- the File to movetoDir
- the Directory to move topublic static File getFileInDirectory(File dir, File localFile)
dir
- the File Object representing the directorylocalFile
- the local file to place in dir, note that only the path is copied - this does copy treespublic static boolean forceDelete(File file)
file
- the file to deletepublic static boolean forceDelete(File file, int loops)
file
- the file to delete#
- of 42 msec * loop loops to waitpublic static File getCreateDirectory(String newDir)
newDir
- the path or URL of the new directorypublic static File cleanURL(File f)
f
- the file to cleanuppublic static boolean isDirectory(File f)
f
- the file to testpublic static boolean isDirectory(String s)
s
- the file to testpublic static boolean isAbsoluteFile(File f)
f
- the file to testpublic static boolean isAbsoluteFile(String f)
f
- the file path to testpublic static String getExtension(File file)
file
- public static boolean createNewFile(File file)
file
- the file to createpublic static boolean equals(File file1, File file2)
file1
- file2
- public static BufferedInputStream getBufferedInputStream(File file)
file
- public static BufferedOutputStream getBufferedOutputStream(File file)
file
- public static BufferedOutputStream getBufferedOutputStream(File file, boolean append)
file
- append
- public static boolean isWindows()
Copyright © 2018. All rights reserved.