org.ijsberg.iglu.util.io
Class FileSupport

java.lang.Object
  extended by org.ijsberg.iglu.util.io.FileSupport

public abstract class FileSupport
extends Object

Supports retrieval and deletion of particular files in a directory structure as well as other file system manipulation.


Constructor Summary
FileSupport()
           
 
Method Summary
static String convertToUnixStylePath(String path)
          Converts backslashes into forward slashes.
static void copyClassLoadableResourceToFileSystem(String pathToResource, String outputPath)
           
static void copyFile(String fileName, String newFileName, boolean overwriteExisting)
          Copies a file.
static Map<Integer,Integer> countOccurencesInTextFile(File file, String searchString)
           
static File createFile(String filename)
          Creates a file.
static File createTmpDir()
           
static File createTmpDir(String prefix)
           
static void deleteContentsInDirectoryTree(File root, String mask)
          Deletes from a directory all files and subdirectories targeted by a given mask.
static void deleteContentsInDirectoryTree(String path, String mask)
          Deletes from a directory all files and subdirectories targeted by a given mask.
static void deleteFile(File file)
          Deletes a file or a directory including its contents;
static void emptyDirectory(File file)
          Deletes all files and subdirectories from a directory.
static void emptyDirectory(String path)
          Deletes all files and subdirectories from a directory.
static byte[] getBinaryFromClassLoader(String path)
           
static byte[] getBinaryFromClassPath(String fileName, String classPath)
           
static byte[] getBinaryFromFS(File existingFile)
           
static byte[] getBinaryFromFS(String fileName)
           
static byte[] getBinaryFromJar(String fileName, String jarFileName)
          Tries to retrieve a file as ZipEntry from all jars mentioned in system property java.class.path
static File getClassFileFromDirectoryInClassPath(String className)
          Tries to retrieve a class as File from all directories mentioned in system property java.class.path
static ZipEntry getClassZipEntryFromJarInClassPath(String className)
          Tries to retrieve a class as ZipEntry from all jars mentioned in system property java.class.path
static ArrayList<File> getDirectoriesInDirectoryTree(String path)
          Retrieves all directories from a directory and its subdirectories.
static ArrayList<File> getDirectoriesInDirectoryTree(String path, String mask)
          Retrieves all directories from a directory and its subdirectories.
static File getFileFromDirectoryInClassPath(String fileName, String classPath)
          Locates a file in the classpath.
static String getFileNameFromPath(String path)
           
static ArrayList<File> getFilesInDirectoryTree(File directory)
          Retrieves all files from a directory and its subdirectories.
static ArrayList<File> getFilesInDirectoryTree(File file, String mask)
          Retrieves all files from a directory and its subdirectories matching the given mask.
static ArrayList<File> getFilesInDirectoryTree(String path)
          Retrieves all files from a directory and its subdirectories.
static ArrayList<File> getFilesInDirectoryTree(String path, String mask)
          Retrieves files for a given mask from a directory and its subdirectories.
static ArrayList<File> getFilesInDirectoryTree(String path, String mask, boolean returnDirs)
          Retrieves all files from a directory and its subdirectories.
static InputStream getInputStreamFromClassLoader(String path)
           
static ZipEntry getZipEntryFromJar(String fileName, String jarFileName)
           
static void main(String[] args)
          Commandline use of FileSupport only supports recursive investigation of directories.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileSupport

public FileSupport()
Method Detail

getFilesInDirectoryTree

public static ArrayList<File> getFilesInDirectoryTree(String path)
Retrieves all files from a directory and its subdirectories.

Parameters:
path - path to directory
Returns:
A list containing the found files

getFilesInDirectoryTree

public static ArrayList<File> getFilesInDirectoryTree(File directory)
Retrieves all files from a directory and its subdirectories.

Parameters:
directory - directory
Returns:
a list containing the found files

getFilesInDirectoryTree

public static ArrayList<File> getFilesInDirectoryTree(String path,
                                                      String mask)
Retrieves files for a given mask from a directory and its subdirectories.

Parameters:
path - root of directory tree
mask - exact filename, or mask containing wildcards
Returns:
A list containing the found files

getFilesInDirectoryTree

public static ArrayList<File> getFilesInDirectoryTree(File file,
                                                      String mask)
Retrieves all files from a directory and its subdirectories matching the given mask.

Parameters:
file - directory
mask - mask to match
Returns:
a list containing the found files

getDirectoriesInDirectoryTree

public static ArrayList<File> getDirectoriesInDirectoryTree(String path)
Retrieves all directories from a directory and its subdirectories.

Parameters:
path - path to directory
Returns:
A list containing the found directories

getDirectoriesInDirectoryTree

public static ArrayList<File> getDirectoriesInDirectoryTree(String path,
                                                            String mask)
Retrieves all directories from a directory and its subdirectories.

Parameters:
path - path to directory
mask - file name to match
Returns:
A list containing the found directories

getFilesInDirectoryTree

public static ArrayList<File> getFilesInDirectoryTree(String path,
                                                      String mask,
                                                      boolean returnDirs)
Retrieves all files from a directory and its subdirectories.

Parameters:
path - path to directory
mask - file name to match
returnDirs - indicates if directories must be returned as well
Returns:
a list containing the found files

getClassFileFromDirectoryInClassPath

public static File getClassFileFromDirectoryInClassPath(String className)
Tries to retrieve a class as File from all directories mentioned in system property java.class.path

Parameters:
className - class name as retrieved in myObject.getClass().getName()
Returns:
a File if the class file was found or null otherwise

getFileFromDirectoryInClassPath

public static File getFileFromDirectoryInClassPath(String fileName,
                                                   String classPath)
Locates a file in the classpath.

Parameters:
fileName -
classPath -
Returns:
the found file or null if the file can not be located

getBinaryFromFS

public static byte[] getBinaryFromFS(String fileName)
                              throws IOException
Parameters:
fileName -
Returns:
Throws:
IOException

getBinaryFromFS

public static byte[] getBinaryFromFS(File existingFile)
                              throws IOException
Returns:
Throws:
IOException

getClassZipEntryFromJarInClassPath

public static ZipEntry getClassZipEntryFromJarInClassPath(String className)
                                                   throws IOException
Tries to retrieve a class as ZipEntry from all jars mentioned in system property java.class.path

Parameters:
className - class name as retrieved in myObject.getClass().getName()
Returns:
a ZipEntry if the class file was found or null otherwise
Throws:
IOException

getBinaryFromJar

public static byte[] getBinaryFromJar(String fileName,
                                      String jarFileName)
                               throws IOException
Tries to retrieve a file as ZipEntry from all jars mentioned in system property java.class.path

Parameters:
fileName - class name as retrieved in myObject.getClass().getName()
Returns:
a ZipEntry if the class file was found or null otherwise
Throws:
IOException

getBinaryFromClassPath

public static byte[] getBinaryFromClassPath(String fileName,
                                            String classPath)
                                     throws IOException
Throws:
IOException

getBinaryFromClassLoader

public static byte[] getBinaryFromClassLoader(String path)
                                       throws IOException
Throws:
IOException

getInputStreamFromClassLoader

public static InputStream getInputStreamFromClassLoader(String path)
                                                 throws IOException
Parameters:
path - path with regular path separators ('/')
Returns:
Throws:
IOException

copyClassLoadableResourceToFileSystem

public static void copyClassLoadableResourceToFileSystem(String pathToResource,
                                                         String outputPath)
                                                  throws IOException
Parameters:
pathToResource -
outputPath -
Throws:
IOException

getZipEntryFromJar

public static ZipEntry getZipEntryFromJar(String fileName,
                                          String jarFileName)
                                   throws IOException
Throws:
IOException

createFile

public static File createFile(String filename)
                       throws IOException
Creates a file. The file, and the directory structure is created physically, if it does not exist already.

Parameters:
filename -
Returns:
Throws:
IOException

emptyDirectory

public static void emptyDirectory(String path)
Deletes all files and subdirectories from a directory.

Parameters:
path -

emptyDirectory

public static void emptyDirectory(File file)
Deletes all files and subdirectories from a directory.

Parameters:
file -

deleteFile

public static void deleteFile(File file)
Deletes a file or a directory including its contents;

Parameters:
file -

copyFile

public static void copyFile(String fileName,
                            String newFileName,
                            boolean overwriteExisting)
                     throws IOException
Copies a file.

Parameters:
fileName -
newFileName -
overwriteExisting -
Throws:
IOException

deleteContentsInDirectoryTree

public static void deleteContentsInDirectoryTree(String path,
                                                 String mask)
Deletes from a directory all files and subdirectories targeted by a given mask. The method will recurse into subdirectories.

Parameters:
path -
mask -

deleteContentsInDirectoryTree

public static void deleteContentsInDirectoryTree(File root,
                                                 String mask)
Deletes from a directory all files and subdirectories targeted by a given mask. The method will recurse into subdirectories.

Parameters:
root -
mask -

countOccurencesInTextFile

public static Map<Integer,Integer> countOccurencesInTextFile(File file,
                                                             String searchString)
                                                      throws IOException
Parameters:
file -
searchString -
Returns:
a map containing the number of occurrences of the search string in lines, if 1 or more, keyed and sorted by line number
Throws:
IOException - if the file could not be found or is a directory or locked

main

public static void main(String[] args)
Commandline use of FileSupport only supports recursive investigation of directories. Usage: java FileSupport - [] []

Parameters:
args -

convertToUnixStylePath

public static String convertToUnixStylePath(String path)
Converts backslashes into forward slashes.

Parameters:
path -
Returns:

getFileNameFromPath

public static String getFileNameFromPath(String path)

createTmpDir

public static File createTmpDir()
                         throws IOException
Throws:
IOException

createTmpDir

public static File createTmpDir(String prefix)
                         throws IOException
Throws:
IOException


Copyright © 2011. All Rights Reserved.