Package org.cip4.jdflib.util
Class FileUtil
java.lang.Object
org.cip4.jdflib.util.FileUtil
collection of helper routines to work with files
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classsimple file filter that lists all directoriesprotected static classsimple file filter that lists all files that match a regular expressionstatic class -
Method Summary
Modifier and TypeMethodDescriptionstatic Filesecurely add a subdirectorystatic Fileremove any internal "../" "./" and "//" from a urlstatic Filestatic booleancopy a buffer to the end of a file, creating it if necessarystatic booleancopy a file, unless fromFile and toFile are equalstatic FilecopyFileToDir(File fromFile, File toDir) copies a File to directory if toFile exists, it is brutally overwritten unless fromFile equals toFilestatic booleancreateFile(File file) similar to File.createFile but also creates any required directoriesstatic booleancreateNewFile(File file) similar to File.createFile but also creates any required directoriesstatic booleanvery brutal tree zapper that will delete a file or directory tree recursivelystatic voiddumpException(File except, Throwable t) static FileensureFileInDir(File fromFile, File toDir) copies a File to directory if and only if toFile does not existstatic booleanensureParent(File file) static booleanchecks the equivalence of files - todo os specific behavior (just in case)static booleanstatic byte[]fileToByteArray(File file) read a file into a byte arraystatic StringfileToString(File file, Charset s) read a file into a byte arraystatic booleanforceDelete(File file) forces deletion of a filestatic booleanforceDelete(File file, int loops) forces deletion of a filestatic Fileget any auxiliary directory with the same name as a filestatic BufferedInputStreamgetBufferedInputStream(File file) create a buffered input stream for a filestatic BufferedOutputStreamgetBufferedOutputStream(File file) create a buffered output stream for a filestatic BufferedOutputStreamgetBufferedOutputStream(File file, boolean append) create a buffered output stream for a filestatic FilegetCreateDirectory(String newDir) create a new directory and return null if the directory could not be createdstatic StringgetExtension(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 backstatic FilegetFileInDirectory(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 createdstatic StringgetSecureFileName(File file) secure check of a file namestatic StringgetSecureName(File file) secure check of a file namestatic StringgetSecurePath(File file, boolean allowAbsolute) secure check of a file pathstatic booleancheck whether a file is absolutestatic booleanstatic booleanisDirectory(File f) check whether a file is a directory.static booleancheck whether a file is a directory.static booleanstatic booleanstatic booleanDeprecated.static File[]listDirectories(File dir) list all direct child directorieslistFilesInTree(File dir, FileFilter filter) list all files matching given regexplistFilesInTree(File dir, String expression) list all files matching given regexpstatic File[]listFilesWithExpression(File dir, String expression) list all files matching given regexpstatic File[]listFilesWithExpression(File dir, String expression, int max) list all files matching given regexpstatic File[]listFilesWithExtension(File dir, String allExtensions) static File[]listFilesWithExtension(File dir, String extension, int max) list all files with a given extension (directories are skippedstatic booleanmoves a File by trying to rename, if this fails, a copy with subsequent delete is performed.static FilemoveFileToDir(File fromFile, File toDir) moves a File to directory by trying to rename, if this fails, a copy with subsequent delete is performed.static FilenewExtension(File f, String newExt) create a File object with a new extensionstatic intnumber of files - at most maxstatic FilestreamToFile(InputStream fis, File fil) static FilestreamToFile(InputStream fis, String fileName) dump a stream to a newly created filestreamToMD5File(InputStream fis, File fil) same as streanToFile but also calculates the md5 hash of the streamstatic FilestringToFile(String s, File fil) static FilewriteFile(IStreamWriter w, File file) write to a file
-
Method Details
-
isLocked
-
exists
-
isFile
-
getAuxDir
get any auxiliary directory with the same name as a file- Parameters:
hotFile-- Returns:
-
listFilesWithExtension
list all files with a given extension (directories are skipped- Parameters:
dir- the directory to searchextension- comma separated list of extensions to check for (null = list all)- Returns:
- Files[] the matching files, null if none are found
-
listFilesWithExtension
-
listFilesWithExpression
list all files matching given regexp- Parameters:
dir- the directory to searchexpression- regular expression - uses the simplified syntax- Returns:
- Files[] the matching files, null if none are found
-
listFilesWithExpression
list all files matching given regexp- Parameters:
dir- the directory to searchexpression- regular expression - uses the simplified syntax- Returns:
- Files[] the matching files, null if none are found
-
numFiles
number of files - at most max- Parameters:
dir- the directory to searchexpression- regular expression - uses the simplified syntax- Returns:
- Files[] the matching files, null if none are found
-
listFilesInTree
list all files matching given regexp- Parameters:
dir- the directory to searchfilter- the filter to apply to files- Returns:
- Vector
the matching files, null if none are found
-
listFilesInTree
list all files matching given regexp- Parameters:
dir- the directory to searchexpression- comma separated list of regular expression of a tree with slashes separating directories- Returns:
- Files[] the matching files, null if none are found
-
listDirectories
list all direct child directories- Parameters:
dir- the directory to search- Returns:
- Files[] the matching directories, null if none are found
-
deleteAll
very brutal tree zapper that will delete a file or directory tree recursively- Parameters:
dirToZapp- the file directory to utterly anihilate- Returns:
- true if ciao
-
streamToFile
dump a stream to a newly created file- Parameters:
fis- the inputstream to readfileName- the file to stream to- Returns:
- the file created by the stream, null if snafu
-
writeFile
write to a file- Parameters:
file- the file to writew- the writer to write to- Returns:
- the file that was created, null if snafu
-
newExtension
create a File object with a new extension- Parameters:
f- the file, if null always returns nullnewExt- the new extension- Returns:
- the file with the new extension
- See Also:
-
streamToFile
- Parameters:
fis- the InputStream to read - if null nothing happensfil- the file to stream to- Returns:
- the file created by the stream, null if snafu
-
stringToFile
- Parameters:
s- the String to read - if null nothing happensfil- the file to stream to- Returns:
- the file created by the stream, null if snafu
-
getFastMD5
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- Parameters:
f-maxSize- if<=0 always use entir length- Returns:
-
streamToMD5File
same as streanToFile but also calculates the md5 hash of the stream- Parameters:
fis- the InputStream to read - if null nothing happensfil- the file to stream to- Returns:
- the file created by the stream, null if snafu
-
fileToString
read a file into a byte array- Parameters:
file- the file to read into a byte array- Returns:
- the correctly sized byte array, null if no bytes were read
-
fileToByteArray
read a file into a byte array- Parameters:
file- the file to read into a byte array- Returns:
- the correctly sized byte array, null if no bytes were read
-
moveFileToDir
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- Parameters:
fromFile- the File to movetoDir- the Directory to move to- Returns:
- File the moved File if success, else null, i.e. toFile exists with the contents of fromFile
-
moveFile
moves a File by trying to rename, if this fails, a copy with subsequent delete is performed. if toFile exists, it is brutally overwritten- Parameters:
fromFile- the File to movetoFile- the File to create- Returns:
- boolean true if success, i.e. toFile exists with the contents of fromFile
-
copyBytes
copy a buffer to the end of a file, creating it if necessary- Parameters:
buf- the source buffertoFile- the destination File- Returns:
- true if success
-
copyFile
copy a file, unless fromFile and toFile are equal- Parameters:
fromFile- the source FiletoFile- the destination File- Returns:
- true if success
-
ensureFileInDir
copies a File to directory if and only if toFile does not exist- Parameters:
fromFile- the File to movetoDir- the Directory to move to- Returns:
- File the destination File if success,
-
cleanDots
remove any internal "../" "./" and "//" from a url- Parameters:
file- the file to clean- Returns:
- File - the clean file
-
copyFileToDir
copies a File to directory if toFile exists, it is brutally overwritten unless fromFile equals toFile- Parameters:
fromFile- the File to movetoDir- the Directory to move to- Returns:
- File the moved File if success, else null, i.e. toFile exists with the contents of fromFile
-
getFileInDirectory
returns a File object corresponding to an instance of localFile placed in dir - No OS calls are made and File is NOT created- Parameters:
dir- the File Object representing the directorylocalFile- the local file to place in dir, note that only the path is copied - this does copy trees- Returns:
- File the File object that represents localFile in Dir
-
addSecure
securely add a subdirectory- Parameters:
baseFile-file-- Returns:
- Throws:
IllegalArgumentException
-
getSecureName
secure check of a file name- Parameters:
file-- Returns:
- Throws:
IllegalArgumentException
-
getSecureFileName
secure check of a file name- Parameters:
file-- Returns:
- Throws:
IllegalArgumentException
-
getSecurePath
public static String getSecurePath(File file, boolean allowAbsolute) throws IllegalArgumentException secure check of a file path- Parameters:
baseFile-file-- Returns:
- Throws:
IllegalArgumentException
-
forceDelete
forces deletion of a file- Parameters:
file- the file to delete- Returns:
- true if the file no longer exists
-
forceDelete
forces deletion of a file- Parameters:
file- the file to delete- Returns:
- true if the file no longer exists
-
getCreateDirectory
create a new directory and return null if the directory could not be created- Parameters:
newDir- the path or URL of the new directory- Returns:
-
cleanURL
- Parameters:
f- the file to cleanup- Returns:
- the cleaned file
-
isDirectory
check whether a file is a directory. If a unix file is a symbolic link to a directory, it is also assumed to be a directory- Parameters:
f- the file to test- Returns:
- true if f is a directory or link to a directory
-
isDirectory
check whether a file is a directory. If a unix file is a symbolic link to a directory, it is also assumed to be a directory- Parameters:
s- the file to test- Returns:
- true if f is a directory or link to a directory
-
isAbsoluteFile
check whether a file is absolute- Parameters:
f- the file to test- Returns:
- true if f is absolute;
-
isAbsoluteFile
- Parameters:
f- the file path to test- Returns:
- true if s is absolute
-
getExtension
- Parameters:
file-- Returns:
- the file extension
-
createNewFile
similar to File.createFile but also creates any required directories- Parameters:
file- the file to create- Returns:
- true if the file exists after the call, else false
-
createFile
similar to File.createFile but also creates any required directories- Parameters:
file- the file to create- Returns:
- true if the file exists after the call, else false
- Throws:
IOException
-
ensureParent
-
equals
checks the equivalence of files - todo os specific behavior (just in case)- Parameters:
file1-file2-- Returns:
-
getBufferedInputStream
create a buffered input stream for a file- Parameters:
file-- Returns:
- the buffered input stream, null if snafu
-
getBufferedOutputStream
create a buffered output stream for a file- Parameters:
file-- Returns:
- the buffered output stream, null if snafu
-
getBufferedOutputStream
create a buffered output stream for a file- Parameters:
file-append-- Returns:
- the buffered output stream, null if snafu
-
isWindows
Deprecated.usePlatformUtil- Returns:
- true if we are on a windows file system
-
dumpException
-
PlatformUtil