at.tuwien.ifs.somtoolbox.util
Class FileUtils

java.lang.Object
  extended by at.tuwien.ifs.somtoolbox.util.FileUtils

public class FileUtils
extends java.lang.Object

This class bundles file-related utilities.

Version:
$Id: FileUtils.java 4225 2011-10-12 12:32:15Z mayer $
Author:
Rudolf Mayer, Doris Baum

Nested Class Summary
static class FileUtils.SOMDescriptionFileFilter
           
 
Field Summary
private static java.lang.String COMMENT_INDICATOR
           
 
Constructor Summary
FileUtils()
           
 
Method Summary
static java.lang.String appendDirectorySeparator(java.lang.String dir)
          Appends the File.separator to a path if the given path is a directory
static void clearOutputDir(java.lang.String outputDir)
           
static void close(java.io.Closeable f)
           
static java.lang.String consumeHeaderComments(java.io.BufferedReader br)
          Reads (and discards) all comment lines (indicated by "#") at the beginning of a file.
static void copyFile(java.lang.String source, java.lang.String destination)
           
static void copyFileSafe(java.io.File destinationFileName, java.io.File sourceFileName)
           
static void copyFileSafe(java.lang.String destinationFileName, java.lang.String sourceFileName)
           
static void copyResource(java.lang.String destinationDirectory, java.lang.String sourceDirectory, java.lang.String fileName)
           
static java.lang.String extractSOMLibDataPrefix(java.lang.String filename)
           
static java.lang.String extractSOMLibInputPrefix(java.lang.String filename)
          Extracts the prefix from a SOM description filename so that the corresponding other two description files can be found
static boolean fileStartsWith(java.lang.String fileName, java.lang.String match)
           
static java.lang.String[] findAllSOMLibFiles(com.martiansoftware.jsap.JSAPResult config, java.lang.String optNameInputs, java.lang.String optNameInputDir, java.lang.String extensionToFind, java.lang.String extensionToCheck)
           
static java.util.LinkedList<java.io.File> getAllFilesInRoot(java.io.File root)
          Finds all files matching any of the given extensions, in any subdirecotry of the given root path
static java.util.LinkedList<java.io.File> getAllFilesInRoot(java.io.File root, java.lang.String... extensions)
          Finds all files matching any of the given extensions, in any subdirecotry of the given root path
private static java.util.LinkedList<java.io.File> getAllFilesInRoot(java.util.LinkedList<java.io.File> fileList, java.io.File root, java.io.FileFilter fileFilter)
           
static java.io.InputStream getInputStream(java.lang.String fileType, java.io.File file)
           
static java.io.InputStream getInputStream(java.lang.String fileType, java.lang.String fileName)
           
static java.lang.String getPathFrom(java.lang.String unitDescriptionFileName)
           
static java.lang.String getPathPrefix(java.lang.String fDir)
          returns the given path, if needed appended by a File.separator
static java.lang.String getSuffix(java.lang.String suffix, boolean gzipped)
          Computes the suffix (extension) of a file name, with or without .gz
static void gunzip(java.lang.String source)
           
static void gunzip(java.lang.String source, java.lang.String destination)
           
static boolean isURL(java.lang.String potentialURL)
          Checks whether the given String is a valid URL or not
static java.io.BufferedReader openFile(java.lang.String fileType, java.io.File file)
          Opens a file specified by argument file and returns a BufferedReader.
static java.io.BufferedReader openFile(java.lang.String fileType, java.lang.String fileName)
          Opens a file specified by argument fileName and returns a BufferedReader.
static java.io.PrintWriter openFileForWriting(java.lang.String fileType, java.lang.String fileName)
           
static java.io.PrintWriter openFileForWriting(java.lang.String fileType, java.lang.String fileName, boolean gzipped)
           
static java.lang.String prepareOutputDir(java.lang.String dir)
          Deprecated. use appendDirectorySeparator(String) instead
static java.lang.String readFileContents(java.io.File file)
          Reads the complete contents of the given file.
static java.lang.String readFileContents(java.lang.String filename)
          Reads the complete contents of the file denoted by the given filename.
static java.lang.String readFromFile(java.lang.String fileName)
           
static java.lang.String readFromFile(java.lang.String resourcePath, java.lang.String fileName)
           
static java.lang.String readFromReader(java.io.Reader reader)
           
static java.lang.String readFromStream(java.io.InputStream in)
           
static java.lang.String[] readLines(java.lang.String filename)
           
static java.util.ArrayList<java.lang.String> readLinesAsList(java.lang.String filename)
           
static java.util.HashMap<java.lang.String,java.lang.String> readSOMLibFileHeaders(java.io.BufferedReader br, java.lang.String fileType)
          Reads the headers of a SOMLib File, and stores the values in a map.
static void saveImageToFile(java.lang.String fileName, java.awt.image.BufferedImage buim)
           
static java.lang.String stripPathPrefix(java.lang.String fileName)
           
static void writeFile(java.lang.String fileName, java.lang.String data)
           
static void writeToFile(java.lang.String content, java.lang.String pathname)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COMMENT_INDICATOR

private static final java.lang.String COMMENT_INDICATOR
See Also:
Constant Field Values
Constructor Detail

FileUtils

public FileUtils()
Method Detail

openFile

public static java.io.BufferedReader openFile(java.lang.String fileType,
                                              java.lang.String fileName)
                                       throws java.io.FileNotFoundException
Opens a file specified by argument fileName and returns a BufferedReader. This method opens both, uncompressed and gzipped files transparent to the calling function. If the specified file is not found, the suffix .gz is appended. If this name is again not found, a FileNotFoundException is thrown.

Parameters:
fileType - the type of the file to open.
fileName - the name of the file to open.
Returns:
a BufferedReader to the requested file.
Throws:
java.io.FileNotFoundException - if the file with the given name is not found.

openFile

public static java.io.BufferedReader openFile(java.lang.String fileType,
                                              java.io.File file)
                                       throws java.io.FileNotFoundException
Opens a file specified by argument file and returns a BufferedReader. This method opens both, uncompressed and gzipped files transparent to the calling function. If the specified file is not found, the suffix .gz is appended. If this name is again not found, a FileNotFoundException is thrown.

Parameters:
fileType - the type of the file to open.
file - the file to open.
Returns:
a BufferedReader to the requested file.
Throws:
java.io.FileNotFoundException - if the file is not found.

getInputStream

public static java.io.InputStream getInputStream(java.lang.String fileType,
                                                 java.lang.String fileName)
                                          throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException

getInputStream

public static java.io.InputStream getInputStream(java.lang.String fileType,
                                                 java.io.File file)
                                          throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException

openFileForWriting

public static java.io.PrintWriter openFileForWriting(java.lang.String fileType,
                                                     java.lang.String fileName)
                                              throws java.io.IOException
Throws:
java.io.IOException

openFileForWriting

public static java.io.PrintWriter openFileForWriting(java.lang.String fileType,
                                                     java.lang.String fileName,
                                                     boolean gzipped)
                                              throws java.io.IOException
Throws:
java.io.IOException

extractSOMLibInputPrefix

public static java.lang.String extractSOMLibInputPrefix(java.lang.String filename)
Extracts the prefix from a SOM description filename so that the corresponding other two description files can be found


extractSOMLibDataPrefix

public static java.lang.String extractSOMLibDataPrefix(java.lang.String filename)

readSOMLibFileHeaders

public static java.util.HashMap<java.lang.String,java.lang.String> readSOMLibFileHeaders(java.io.BufferedReader br,
                                                                                         java.lang.String fileType)
                                                                                  throws java.io.IOException
Reads the headers of a SOMLib File, and stores the values in a map.

Throws:
java.io.IOException

findAllSOMLibFiles

public static java.lang.String[] findAllSOMLibFiles(com.martiansoftware.jsap.JSAPResult config,
                                                    java.lang.String optNameInputs,
                                                    java.lang.String optNameInputDir,
                                                    java.lang.String extensionToFind,
                                                    java.lang.String extensionToCheck)

stripPathPrefix

public static java.lang.String stripPathPrefix(java.lang.String fileName)

copyFile

public static void copyFile(java.lang.String source,
                            java.lang.String destination)
                     throws java.io.FileNotFoundException,
                            java.io.IOException,
                            SOMToolboxException
Throws:
java.io.FileNotFoundException
java.io.IOException
SOMToolboxException

copyFileSafe

public static void copyFileSafe(java.io.File destinationFileName,
                                java.io.File sourceFileName)

copyFileSafe

public static void copyFileSafe(java.lang.String destinationFileName,
                                java.lang.String sourceFileName)

copyResource

public static void copyResource(java.lang.String destinationDirectory,
                                java.lang.String sourceDirectory,
                                java.lang.String fileName)

fileStartsWith

public static boolean fileStartsWith(java.lang.String fileName,
                                     java.lang.String match)
                              throws java.io.FileNotFoundException,
                                     java.io.IOException
Throws:
java.io.FileNotFoundException
java.io.IOException

readLines

public static java.lang.String[] readLines(java.lang.String filename)
                                    throws java.io.IOException
Throws:
java.io.IOException

readLinesAsList

public static java.util.ArrayList<java.lang.String> readLinesAsList(java.lang.String filename)
                                                             throws java.io.FileNotFoundException,
                                                                    java.io.IOException
Throws:
java.io.FileNotFoundException
java.io.IOException

readFileContents

public static java.lang.String readFileContents(java.lang.String filename)
                                         throws java.io.FileNotFoundException,
                                                java.io.IOException
Reads the complete contents of the file denoted by the given filename. uses readFileContents(File).

Throws:
java.io.FileNotFoundException
java.io.IOException

readFileContents

public static java.lang.String readFileContents(java.io.File file)
                                         throws java.io.IOException
Reads the complete contents of the given file.

Throws:
java.io.IOException

readFromStream

public static java.lang.String readFromStream(java.io.InputStream in)
                                       throws java.io.IOException
Throws:
java.io.IOException

readFromReader

public static java.lang.String readFromReader(java.io.Reader reader)
                                       throws java.io.IOException
Throws:
java.io.IOException

readFromFile

public static java.lang.String readFromFile(java.lang.String resourcePath,
                                            java.lang.String fileName)
                                     throws java.io.FileNotFoundException,
                                            java.io.IOException
Throws:
java.io.FileNotFoundException
java.io.IOException

readFromFile

public static java.lang.String readFromFile(java.lang.String fileName)
                                     throws java.io.FileNotFoundException,
                                            java.io.IOException
Throws:
java.io.FileNotFoundException
java.io.IOException

prepareOutputDir

@Deprecated
public static java.lang.String prepareOutputDir(java.lang.String dir)
Deprecated. use appendDirectorySeparator(String) instead


appendDirectorySeparator

public static java.lang.String appendDirectorySeparator(java.lang.String dir)
Appends the File.separator to a path if the given path is a directory


clearOutputDir

public static void clearOutputDir(java.lang.String outputDir)

writeToFile

public static void writeToFile(java.lang.String content,
                               java.lang.String pathname)
                        throws java.io.IOException
Throws:
java.io.IOException

getPathFrom

public static java.lang.String getPathFrom(java.lang.String unitDescriptionFileName)

saveImageToFile

public static void saveImageToFile(java.lang.String fileName,
                                   java.awt.image.BufferedImage buim)
                            throws SOMToolboxException
Throws:
SOMToolboxException

writeFile

public static void writeFile(java.lang.String fileName,
                             java.lang.String data)
                      throws java.io.FileNotFoundException,
                             java.io.IOException
Throws:
java.io.FileNotFoundException
java.io.IOException

isURL

public static boolean isURL(java.lang.String potentialURL)
Checks whether the given String is a valid URL or not


consumeHeaderComments

public static java.lang.String consumeHeaderComments(java.io.BufferedReader br)
                                              throws java.io.IOException
Reads (and discards) all comment lines (indicated by "#") at the beginning of a file.

Returns:
the first non-comment line
Throws:
java.io.IOException

getPathPrefix

public static java.lang.String getPathPrefix(java.lang.String fDir)
returns the given path, if needed appended by a File.separator


getSuffix

public static java.lang.String getSuffix(java.lang.String suffix,
                                         boolean gzipped)
Computes the suffix (extension) of a file name, with or without .gz


getAllFilesInRoot

public static java.util.LinkedList<java.io.File> getAllFilesInRoot(java.io.File root)
Finds all files matching any of the given extensions, in any subdirecotry of the given root path


getAllFilesInRoot

public static java.util.LinkedList<java.io.File> getAllFilesInRoot(java.io.File root,
                                                                   java.lang.String... extensions)
Finds all files matching any of the given extensions, in any subdirecotry of the given root path


getAllFilesInRoot

private static java.util.LinkedList<java.io.File> getAllFilesInRoot(java.util.LinkedList<java.io.File> fileList,
                                                                    java.io.File root,
                                                                    java.io.FileFilter fileFilter)

gunzip

public static void gunzip(java.lang.String source)
                   throws SOMToolboxException
Throws:
SOMToolboxException

gunzip

public static void gunzip(java.lang.String source,
                          java.lang.String destination)

close

public static void close(java.io.Closeable f)