Package org.cip4.jdflib.util
Class FileUtil
- java.lang.Object
-
- org.cip4.jdflib.util.FileUtil
-
public class FileUtil extends java.lang.Objectcollection of helper routines to work with files
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classFileUtil.DirectoryFileFiltersimple file filter that lists all directoriesprotected static classFileUtil.ExpressionFileFiltersimple file filter that lists all files that match a regular expressionstatic classFileUtil.ExtensionFileFilter
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.io.FileaddSecure(java.io.File baseFile, java.io.File file)securely add a subdirectorystatic java.io.FilecleanDots(java.io.File file)remove any internal "../" "./" and "//" from a urlstatic java.io.FilecleanURL(java.io.File f)static booleancopyBytes(byte[] buf, java.io.File toFile)copy a buffer to the end of a file, creating it if necessarystatic booleancopyFile(java.io.File fromFile, java.io.File toFile)copy a file, unless fromFile and toFile are equalstatic java.io.FilecopyFileToDir(java.io.File fromFile, java.io.File toDir)copies a File to directory if toFile exists, it is brutally overwritten unless fromFile equals toFilestatic booleancreateFile(java.io.File file)similar to File.createFile but also creates any required directoriesstatic booleancreateNewFile(java.io.File file)similar to File.createFile but also creates any required directoriesstatic booleandeleteAll(java.io.File dirToZapp)very brutal tree zapper that will delete a file or directory tree recursivelystatic voiddumpException(java.io.File except, java.lang.Throwable t)static java.io.FileensureFileInDir(java.io.File fromFile, java.io.File toDir)copies a File to directory if and only if toFile does not existstatic booleanequals(java.io.File file1, java.io.File file2)checks the equivalence of files - todo os specific behavior (just in case)static booleanexists(java.io.File file)static byte[]fileToByteArray(java.io.File file)read a file into a byte arraystatic java.lang.StringfileToString(java.io.File file, java.nio.charset.Charset s)read a file into a byte arraystatic booleanforceDelete(java.io.File file)forces deletion of a filestatic booleanforceDelete(java.io.File file, int loops)forces deletion of a filestatic java.io.FilegetAuxDir(java.io.File hotFile)get any auxiliary directory with the same name as a filestatic java.io.BufferedInputStreamgetBufferedInputStream(java.io.File file)create a buffered input stream for a filestatic java.io.BufferedOutputStreamgetBufferedOutputStream(java.io.File file)create a buffered output stream for a filestatic java.io.BufferedOutputStreamgetBufferedOutputStream(java.io.File file, boolean append)create a buffered output stream for a filestatic java.io.FilegetCreateDirectory(java.lang.String newDir)create a new directory and return null if the directory could not be createdstatic java.lang.StringgetExtension(java.io.File file)static byte[]getFastMD5(java.io.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 java.io.FilegetFileInDirectory(java.io.File dir, java.io.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 java.lang.StringgetSecureFileName(java.io.File file)secure check of a file namestatic java.lang.StringgetSecureName(java.io.File file)secure check of a file namestatic java.lang.StringgetSecurePath(java.io.File file, boolean allowAbsolute)secure check of a file pathstatic booleanisAbsoluteFile(java.io.File f)check whether a file is absolutestatic booleanisAbsoluteFile(java.lang.String f)static booleanisDirectory(java.io.File f)check whether a file is a directory.static booleanisDirectory(java.lang.String s)check whether a file is a directory.static booleanisFile(java.io.File file)static booleanisLocked(java.io.File file)static booleanisWindows()Deprecated.usePlatformUtilstatic java.io.File[]listDirectories(java.io.File dir)list all direct child directoriesstatic java.util.Vector<java.io.File>listFilesInTree(java.io.File dir, java.io.FileFilter filter)list all files matching given regexpstatic java.util.Vector<java.io.File>listFilesInTree(java.io.File dir, java.lang.String expression)list all files matching given regexpstatic java.io.File[]listFilesWithExpression(java.io.File dir, java.lang.String expression)list all files matching given regexpstatic java.io.File[]listFilesWithExpression(java.io.File dir, java.lang.String expression, int max)list all files matching given regexpstatic java.io.File[]listFilesWithExtension(java.io.File dir, java.lang.String allExtensions)static java.io.File[]listFilesWithExtension(java.io.File dir, java.lang.String extension, int max)list all files with a given extension (directories are skippedstatic booleanmoveFile(java.io.File fromFile, java.io.File toFile)moves a File by trying to rename, if this fails, a copy with subsequent delete is performed.static java.io.FilemoveFileToDir(java.io.File fromFile, java.io.File toDir)moves a File to directory by trying to rename, if this fails, a copy with subsequent delete is performed.static java.io.FilenewExtension(java.io.File f, java.lang.String newExt)create a File object with a new extensionstatic intnumFiles(java.io.File dir, int max)number of files - at most maxstatic java.io.FilestreamToFile(java.io.InputStream fis, java.io.File fil)static java.io.FilestreamToFile(java.io.InputStream fis, java.lang.String fileName)dump a stream to a newly created filestatic MyPair<java.io.File,byte[]>streamToMD5File(java.io.InputStream fis, java.io.File fil)same as streanToFile but also calculates the md5 hash of the streamstatic java.io.FilestringToFile(java.lang.String s, java.io.File fil)static java.io.FilewriteFile(IStreamWriter w, java.io.File file)write to a file
-
-
-
Method Detail
-
isLocked
public static boolean isLocked(java.io.File file)
-
exists
public static boolean exists(java.io.File file)
-
isFile
public static boolean isFile(java.io.File file)
-
getAuxDir
public static java.io.File getAuxDir(java.io.File hotFile)
get any auxiliary directory with the same name as a file- Parameters:
hotFile-- Returns:
-
listFilesWithExtension
public static java.io.File[] listFilesWithExtension(java.io.File dir, java.lang.String extension, int max)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
public static java.io.File[] listFilesWithExtension(java.io.File dir, java.lang.String allExtensions)
-
listFilesWithExpression
public static java.io.File[] listFilesWithExpression(java.io.File dir, java.lang.String expression)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
public static java.io.File[] listFilesWithExpression(java.io.File dir, java.lang.String expression, int max)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
public static int numFiles(java.io.File dir, int max)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
public static java.util.Vector<java.io.File> listFilesInTree(java.io.File dir, java.io.FileFilter filter)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
public static java.util.Vector<java.io.File> listFilesInTree(java.io.File dir, java.lang.String expression)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
public static java.io.File[] listDirectories(java.io.File dir)
list all direct child directories- Parameters:
dir- the directory to search- Returns:
- Files[] the matching directories, null if none are found
-
deleteAll
public static boolean deleteAll(java.io.File dirToZapp)
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
public static java.io.File streamToFile(java.io.InputStream fis, java.lang.String fileName)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
public static java.io.File writeFile(IStreamWriter w, java.io.File file)
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
public static java.io.File newExtension(java.io.File f, java.lang.String newExt)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:
for details of handling null etc.
-
streamToFile
public static java.io.File streamToFile(java.io.InputStream fis, java.io.File fil)- 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
public static java.io.File stringToFile(java.lang.String s, java.io.File fil)- 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
public static byte[] getFastMD5(java.io.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- Parameters:
f-maxSize- if<=0 always use entir length- Returns:
-
streamToMD5File
public static MyPair<java.io.File,byte[]> streamToMD5File(java.io.InputStream fis, java.io.File fil)
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
public static java.lang.String fileToString(java.io.File file, java.nio.charset.Charset s)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
public static byte[] fileToByteArray(java.io.File file)
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
public static java.io.File moveFileToDir(java.io.File fromFile, java.io.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- 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
public static boolean moveFile(java.io.File fromFile, java.io.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- 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
public static boolean copyBytes(byte[] buf, java.io.File toFile)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
public static boolean copyFile(java.io.File fromFile, java.io.File toFile)copy a file, unless fromFile and toFile are equal- Parameters:
fromFile- the source FiletoFile- the destination File- Returns:
- true if success
-
ensureFileInDir
public static java.io.File ensureFileInDir(java.io.File fromFile, java.io.File toDir)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
public static java.io.File cleanDots(java.io.File file)
remove any internal "../" "./" and "//" from a url- Parameters:
file- the file to clean- Returns:
- File - the clean file
-
copyFileToDir
public static java.io.File copyFileToDir(java.io.File fromFile, java.io.File toDir)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
public static java.io.File getFileInDirectory(java.io.File dir, java.io.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- 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
public static java.io.File addSecure(java.io.File baseFile, java.io.File file) throws java.lang.IllegalArgumentExceptionsecurely add a subdirectory- Parameters:
baseFile-file-- Returns:
- Throws:
java.lang.IllegalArgumentException
-
getSecureName
public static java.lang.String getSecureName(java.io.File file) throws java.lang.IllegalArgumentExceptionsecure check of a file name- Parameters:
file-- Returns:
- Throws:
java.lang.IllegalArgumentException
-
getSecureFileName
public static java.lang.String getSecureFileName(java.io.File file)
secure check of a file name- Parameters:
file-- Returns:
- Throws:
java.lang.IllegalArgumentException
-
getSecurePath
public static java.lang.String getSecurePath(java.io.File file, boolean allowAbsolute) throws java.lang.IllegalArgumentExceptionsecure check of a file path- Parameters:
baseFile-file-- Returns:
- Throws:
java.lang.IllegalArgumentException
-
forceDelete
public static boolean forceDelete(java.io.File file)
forces deletion of a file- Parameters:
file- the file to delete- Returns:
- true if the file no longer exists
-
forceDelete
public static boolean forceDelete(java.io.File file, int loops)forces deletion of a file- Parameters:
file- the file to delete- Returns:
- true if the file no longer exists
-
getCreateDirectory
public static java.io.File getCreateDirectory(java.lang.String newDir)
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
public static java.io.File cleanURL(java.io.File f)
- Parameters:
f- the file to cleanup- Returns:
- the cleaned file
-
isDirectory
public static boolean isDirectory(java.io.File f)
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
public static boolean isDirectory(java.lang.String s)
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
public static boolean isAbsoluteFile(java.io.File f)
check whether a file is absolute- Parameters:
f- the file to test- Returns:
- true if f is absolute;
-
isAbsoluteFile
public static boolean isAbsoluteFile(java.lang.String f)
- Parameters:
f- the file path to test- Returns:
- true if s is absolute
-
getExtension
public static java.lang.String getExtension(java.io.File file)
- Parameters:
file-- Returns:
- the file extension
-
createNewFile
public static boolean createNewFile(java.io.File file)
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
public static boolean createFile(java.io.File file) throws java.io.IOExceptionsimilar 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:
java.io.IOException
-
equals
public static boolean equals(java.io.File file1, java.io.File file2)checks the equivalence of files - todo os specific behavior (just in case)- Parameters:
file1-file2-- Returns:
-
getBufferedInputStream
public static java.io.BufferedInputStream getBufferedInputStream(java.io.File file)
create a buffered input stream for a file- Parameters:
file-- Returns:
- the buffered input stream, null if snafu
-
getBufferedOutputStream
public static java.io.BufferedOutputStream getBufferedOutputStream(java.io.File file)
create a buffered output stream for a file- Parameters:
file-- Returns:
- the buffered output stream, null if snafu
-
getBufferedOutputStream
public static java.io.BufferedOutputStream getBufferedOutputStream(java.io.File file, boolean append)create a buffered output stream for a file- Parameters:
file-append-- Returns:
- the buffered output stream, null if snafu
-
isWindows
@Deprecated public static boolean isWindows()
Deprecated.usePlatformUtil- Returns:
- true if we are on a windows file system
-
dumpException
public static void dumpException(java.io.File except, java.lang.Throwable t)
-
-