org.cip4.jdflib.util
Class FileUtil

java.lang.Object
  extended by org.cip4.jdflib.util.FileUtil

public class FileUtil
extends Object

collection of helper routines to work with files

Author:
prosirai

Nested Class Summary
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 Summary
FileUtil()
           
 
Method Summary
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 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 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 File writeFile(IStreamWriter w, File file)
          write to a file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileUtil

public FileUtil()
Method Detail

listFilesWithExtension

public static File[] listFilesWithExtension(File dir,
                                            String extension)
list all files with a given extension (directories are skipped

Parameters:
dir - the directory to search
extension - comma separated list of extensions to check for (null = list all)
Returns:
Files[] the matching files, null if none are found

listFilesWithExpression

public static File[] listFilesWithExpression(File dir,
                                             String expression)
list all files matching given regexp

Parameters:
dir - the directory to search
expression - regular expression - uses the simplified syntax
Returns:
Files[] the matching files, null if none are found

listFilesInTree

public static Vector<File> listFilesInTree(File dir,
                                           FileFilter filter)
list all files matching given regexp

Parameters:
dir - the directory to search
filter - the filter to apply to files
Returns:
Vector the matching files, null if none are found

listFilesInTree

public static Vector<File> listFilesInTree(File dir,
                                           String expression)
list all files matching given regexp

Parameters:
dir - the directory to search
expression - 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 File[] listDirectories(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(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 File streamToFile(InputStream fis,
                                String fileName)
dump a stream to a newly created file

Parameters:
fis - the inputstream to read
fileName - the file to stream to
Returns:
the file created by the stream, null if snafu

writeFile

public static File writeFile(IStreamWriter w,
                             File file)
write to a file

Parameters:
file - the file to write
w - the writer to write to
Returns:
the file that was created, null if snafu

newExtension

public static File newExtension(File f,
                                String newExt)
create a File object with a new extension

Parameters:
f - the file, if null always returns null
newExt - the new extension
Returns:
the file with the new extension
See Also:
for details of handling null etc.

streamToFile

public static File streamToFile(InputStream fis,
                                File fil)
Parameters:
fis - the InputStream to read - if null nothing happens
fil - the file to stream to
Returns:
the file created by the stream, null if snafu

fileToByteArray

public static byte[] fileToByteArray(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 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

Parameters:
fromFile - the File to move
toDir - 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(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

Parameters:
fromFile - the File to move
toFile - 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,
                                File toFile)
copy a buffer to the end of a file, creating it if necessary

Parameters:
buf - the source buffer
toFile - the destination File
Returns:
true if success

copyFile

public static boolean copyFile(File fromFile,
                               File toFile)
copy a file, unless fromFile and toFile are equal

Parameters:
fromFile - the source File
toFile - the destination File
Returns:
true if success

ensureFileInDir

public static File ensureFileInDir(File fromFile,
                                   File toDir)
copies a File to directory if and only if toFile does not exist

Parameters:
fromFile - the File to move
toDir - the Directory to move to
Returns:
File the destination File if success,

cleanDots

public static File cleanDots(File file)
remove any internal "../" "./" and "//" from a url

Parameters:
file - the file to clean
Returns:
File - the clean file

copyFileToDir

public static File copyFileToDir(File fromFile,
                                 File toDir)
copies a File to directory if toFile exists, it is brutally overwritten unless fromFile equals toFile

Parameters:
fromFile - the File to move
toDir - 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 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

Parameters:
dir - the File Object representing the directory
localFile - 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

forceDelete

public static boolean forceDelete(File file)
forces deletion of a file

Parameters:
file - the file to delete
Returns:
true if the file no longer exists

getCreateDirectory

public static File getCreateDirectory(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 File cleanURL(File f)
Parameters:
f - the file to cleanup
Returns:
the cleaned file

isDirectory

public static boolean isDirectory(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(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(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(String f)
Parameters:
f - the file path to test
Returns:
true if s is absolute

getExtension

public static String getExtension(File file)
Parameters:
file -
Returns:
the file extension

createNewFile

public static boolean createNewFile(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

equals

public static boolean equals(File file1,
                             File file2)
checks the equivalence of files - todo os specific behavior (just in case)

Parameters:
file1 -
file2 -
Returns:

getBufferedInputStream

public static BufferedInputStream getBufferedInputStream(File file)
create a buffered input stream for a file

Parameters:
file -
Returns:
the buffered input stream, null if snafu

getBufferedOutputStream

public static BufferedOutputStream getBufferedOutputStream(File file)
create a buffered output stream for a file

Parameters:
file -
Returns:
the buffered output stream, null if snafu

getBufferedOutputStream

public static BufferedOutputStream getBufferedOutputStream(File file,
                                                           boolean append)
create a buffered output stream for a file

Parameters:
file -
append -
Returns:
the buffered output stream, null if snafu

isWindows

public static boolean isWindows()
Returns:
true if we are on a windows file system


Copyright © 2013. All Rights Reserved.