at.tuwien.ifs.somtoolbox.util
Class FileUtils

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

public class FileUtils
extends Object

This class bundles file-related utilities.

Version:
$Id: FileUtils.java 3916 2010-11-04 14:46:37Z mayer $
Author:
Rudolf Mayer, Doris Baum

Nested Class Summary
static class FileUtils.SOMDescriptionFileFilter
           
 
Field Summary
private static String COMMENT_INDICATOR
           
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void clearOutputDir(String outputDir)
           
static void close(Closeable f)
           
static String consumeHeaderComments(BufferedReader br)
          Reads (and discards) all comment lines (indicated by "#") at the beginning of a file.
static void copyFile(String source, String destination)
           
static void copyFileSafe(File destinationFileName, File sourceFileName)
           
static void copyFileSafe(String destinationFileName, String sourceFileName)
           
static void copyResource(String destinationDirectory, String sourceDirectory, String fileName)
           
static String extractSOMLibDataPrefix(String filename)
           
static String extractSOMLibInputPrefix(String filename)
          Extracts the prefix from a SOM description filename so that the corresponding other two description files can be found
static boolean fileStartsWith(String fileName, String match)
           
static String[] findAllSOMLibFiles(com.martiansoftware.jsap.JSAPResult config, String optNameInputs, String optNameInputDir, String extensionToFind, String extensionToCheck)
           
static LinkedList<File> getAllFilesInRoot(File root, String... extensions)
          Finds all files matching any of the given extensions, in any subdirecotry of the given root path
private static LinkedList<File> getAllFilesInRoot(LinkedList<File> fileList, File root, FileFilter fileFilter)
           
static InputStream getInputStream(String fileType, File file)
           
static InputStream getInputStream(String fileType, String fileName)
           
static String getPathFrom(String unitDescriptionFileName)
           
static String getPathPrefix(String fDir)
          returns the given path, if needed appended by a File.separator
static String getSuffix(String suffix, boolean gzipped)
          Computes the suffix (extension) of a file name, with or without .gz
static void gunzip(String source)
           
static void gunzip(String source, String destination)
           
static boolean isURL(String potentialURL)
          Checks whether the given String is a valid URL or not
static BufferedReader openFile(String fileType, File file)
          Opens a file specified by argument file and returns a BufferedReader.
static BufferedReader openFile(String fileType, String fileName)
          Opens a file specified by argument fileName and returns a BufferedReader.
static PrintWriter openFileForWriting(String fileType, String fileName)
           
static PrintWriter openFileForWriting(String fileType, String fileName, boolean gzipped)
           
static String prepareOutputDir(String dir)
           
static String readFileContents(File file)
          Reads the complete contents of the given file.
static String readFileContents(String filename)
          Reads the complete contents of the file denoted by the given filename.
static String readFromFile(String fileName)
           
static String readFromFile(String resourcePath, String fileName)
           
static String[] readLines(String filename)
           
static ArrayList<String> readLinesAsList(String filename)
           
static HashMap<String,String> readSOMLibFileHeaders(BufferedReader br, String fileType)
          Reads the headers of a SOMLib File, and stores the values in a map.
static void saveImageToFile(String fileName, BufferedImage buim)
           
static String stripPathPrefix(String fileName)
           
static void writeFile(String fileName, String data)
           
static void writeToFile(String content, 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 String COMMENT_INDICATOR
See Also:
Constant Field Values
Constructor Detail

FileUtils

public FileUtils()
Method Detail

openFile

public static BufferedReader openFile(String fileType,
                                      String fileName)
                               throws 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:
FileNotFoundException - if the file with the given name is not found.

openFile

public static BufferedReader openFile(String fileType,
                                      File file)
                               throws 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:
FileNotFoundException - if the file is not found.

getInputStream

public static InputStream getInputStream(String fileType,
                                         String fileName)
                                  throws FileNotFoundException
Throws:
FileNotFoundException

getInputStream

public static InputStream getInputStream(String fileType,
                                         File file)
                                  throws FileNotFoundException
Throws:
FileNotFoundException

openFileForWriting

public static PrintWriter openFileForWriting(String fileType,
                                             String fileName)
                                      throws IOException
Throws:
IOException

openFileForWriting

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

extractSOMLibInputPrefix

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


extractSOMLibDataPrefix

public static String extractSOMLibDataPrefix(String filename)

readSOMLibFileHeaders

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

Throws:
IOException

findAllSOMLibFiles

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

stripPathPrefix

public static String stripPathPrefix(String fileName)

copyFile

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

copyFileSafe

public static void copyFileSafe(File destinationFileName,
                                File sourceFileName)

copyFileSafe

public static void copyFileSafe(String destinationFileName,
                                String sourceFileName)

copyResource

public static void copyResource(String destinationDirectory,
                                String sourceDirectory,
                                String fileName)

fileStartsWith

public static boolean fileStartsWith(String fileName,
                                     String match)
                              throws FileNotFoundException,
                                     IOException
Throws:
FileNotFoundException
IOException

readLines

public static String[] readLines(String filename)
                          throws IOException
Throws:
IOException

readLinesAsList

public static ArrayList<String> readLinesAsList(String filename)
                                         throws FileNotFoundException,
                                                IOException
Throws:
FileNotFoundException
IOException

readFileContents

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

Throws:
FileNotFoundException
IOException

readFileContents

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

Throws:
IOException

readFromFile

public static String readFromFile(String resourcePath,
                                  String fileName)
                           throws FileNotFoundException,
                                  IOException
Throws:
FileNotFoundException
IOException

readFromFile

public static String readFromFile(String fileName)
                           throws FileNotFoundException,
                                  IOException
Throws:
FileNotFoundException
IOException

prepareOutputDir

public static String prepareOutputDir(String dir)

clearOutputDir

public static void clearOutputDir(String outputDir)

writeToFile

public static void writeToFile(String content,
                               String pathname)
                        throws IOException
Throws:
IOException

getPathFrom

public static String getPathFrom(String unitDescriptionFileName)

saveImageToFile

public static void saveImageToFile(String fileName,
                                   BufferedImage buim)
                            throws SOMToolboxException
Throws:
SOMToolboxException

writeFile

public static void writeFile(String fileName,
                             String data)
                      throws FileNotFoundException,
                             IOException
Throws:
FileNotFoundException
IOException

isURL

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


consumeHeaderComments

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

Returns:
the first non-comment line
Throws:
IOException

getPathPrefix

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


getSuffix

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


getAllFilesInRoot

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


getAllFilesInRoot

private static LinkedList<File> getAllFilesInRoot(LinkedList<File> fileList,
                                                  File root,
                                                  FileFilter fileFilter)

gunzip

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

gunzip

public static void gunzip(String source,
                          String destination)

close

public static void close(Closeable f)