be.hogent.tarsos.lsh.util
Class FileUtils

java.lang.Object
  extended by be.hogent.tarsos.lsh.util.FileUtils

public final class FileUtils
extends java.lang.Object

An utility class for file interaction.

Author:
Joren Six

Nested Class Summary
static interface FileUtils.RowFilter
           
 
Field Summary
static FileUtils.RowFilter ACCEPT_ALL_ROWFILTER
           
 
Method Summary
static void appendFile(java.lang.String contents, java.lang.String name)
          Appends a string to a file on disk.
static java.lang.String basename(java.lang.String fileName)
          Returns the filename without path and without extension.
static java.lang.String combine(java.lang.String... path)
          Joins path elements using the systems path separator.
static void copyFileFromJar(java.lang.String source, java.lang.String target)
          Copy a file from a jar.
static void cp(java.lang.String source, java.lang.String target)
          Copy from source to target.
static boolean exists(java.lang.String fileName)
          Checks if a file exists.
static java.lang.String extension(java.lang.String fileName)
          Return the extension of a file.
static java.util.List<java.lang.String> glob(java.lang.String directory, java.lang.String pattern, boolean recursive)
           Return a list of files in directory that satisfy pattern.
static boolean isDirectory(java.lang.String inputFile)
          Tests whether the file denoted by this abstract pathname is a directory.
static boolean mkdirs(java.lang.String path)
          Creates a directory and parent directories if needed.
static java.lang.String path(java.lang.String fileName)
          Returns the path for a file.
path("/home/user/be.hogent.tarsos.lsh.test.jpg") == "/home/user"
Uses the correct pathSeparator depending on the operating system.
static java.util.List<java.lang.String> readColumnFromCSVData(java.util.List<java.lang.String[]> data, int columnIndex, FileUtils.RowFilter filter)
           
static java.util.List<java.lang.String[]> readCSVFile(java.lang.String fileName, java.lang.String separator, int expectedColumns)
          Reads a CSV-file from disk.
static java.lang.String readFile(java.lang.String name)
          Reads the contents of a file.
static java.lang.String readFileFromJar(java.lang.String path)
          Reads the contents of a file in a jar.
static boolean rm(java.lang.String fileName)
          Removes a file from disk.
static java.lang.String runtimeDirectory()
           
static java.lang.String temporaryDirectory()
           
static void writeFile(java.lang.String contents, java.lang.String name)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ACCEPT_ALL_ROWFILTER

public static final FileUtils.RowFilter ACCEPT_ALL_ROWFILTER
Method Detail

temporaryDirectory

public static java.lang.String temporaryDirectory()

combine

public static java.lang.String combine(java.lang.String... path)
Joins path elements using the systems path separator. e.g. "/tmp" and "be.hogent.tarsos.lsh.test.wav" combined together should yield /tmp/be.hogent.tarsos.lsh.test.wav on UNIX.

Parameters:
path - The path parts part.
Returns:
Each element from path joined by the systems path separator.

runtimeDirectory

public static java.lang.String runtimeDirectory()
Returns:
The path where the program is executed.

writeFile

public static void writeFile(java.lang.String contents,
                             java.lang.String name)
Parameters:
contents -
name -

appendFile

public static void appendFile(java.lang.String contents,
                              java.lang.String name)
Appends a string to a file on disk. Fails silently.

Parameters:
contents - The contents of the file.
name - The name of the file to create.

readFile

public static java.lang.String readFile(java.lang.String name)
Reads the contents of a file.

Parameters:
name - the name of the file to read
Returns:
the contents of the file if successful, an empty string otherwise.

readFileFromJar

public static java.lang.String readFileFromJar(java.lang.String path)
Reads the contents of a file in a jar.

Parameters:
path - the path to read e.g. /package/name/here/help.html
Returns:
the contents of the file when successful, an empty string otherwise.

copyFileFromJar

public static void copyFileFromJar(java.lang.String source,
                                   java.lang.String target)
Copy a file from a jar.

Parameters:
source - The path to read e.g. /package/name/here/help.html
target - The target to save the file to.

readCSVFile

public static java.util.List<java.lang.String[]> readCSVFile(java.lang.String fileName,
                                                             java.lang.String separator,
                                                             int expectedColumns)
Reads a CSV-file from disk. The separator can be chosen.

Parameters:
fileName - the filename, an exception if thrown if the file does not exist
separator - the separator, e.g. ";" or ","
expectedColumns - The expected number of columns, user -1 if the number is unknown. An exception is thrown if there is a row with an unexpected row length.
Returns:
a List of string arrays. The data of the CSV-file can be found in the arrays. Each row corresponds with an array.

readColumnFromCSVData

public static java.util.List<java.lang.String> readColumnFromCSVData(java.util.List<java.lang.String[]> data,
                                                                     int columnIndex,
                                                                     FileUtils.RowFilter filter)

glob

public static java.util.List<java.lang.String> glob(java.lang.String directory,
                                                    java.lang.String pattern,
                                                    boolean recursive)

Return a list of files in directory that satisfy pattern. Pattern should be a valid regular expression not a 'unix glob pattern' so in stead of *.wav you could use .*\.wav

E.g. in a directory home with the files be.hogent.tarsos.lsh.test.txt, blaat.wav and foobar.wav the pattern .*\.wav matches blaat.wav and foobar.wav

Parameters:
directory - A readable directory.
pattern - A valid regular expression.
recursive - A boolean defining if directories should be traversed recursively.
Returns:
a list of filenames matching the pattern for directory.
Throws:
java.lang.Error - an error is thrown if the directory is not ... a directory.
java.util.regex.PatternSyntaxException - Unchecked exception thrown to indicate a syntax error in a regular-expression pattern.

extension

public static java.lang.String extension(java.lang.String fileName)
Return the extension of a file.

Parameters:
fileName - the file to get the extension for
Returns:
the extension. E.g. TXT or JPEG.

basename

public static java.lang.String basename(java.lang.String fileName)
Returns the filename without path and without extension.

Parameters:
fileName -
Returns:
the file name without extension and path

path

public static java.lang.String path(java.lang.String fileName)
Returns the path for a file.
path("/home/user/be.hogent.tarsos.lsh.test.jpg") == "/home/user"
Uses the correct pathSeparator depending on the operating system. On windows c:/be.hogent.tarsos.lsh.test/ is not c:\be.hogent.tarsos.lsh.test\

Parameters:
fileName - the name of the file using correct path separators.
Returns:
the path of the file.

exists

public static boolean exists(java.lang.String fileName)
Checks if a file exists.

Parameters:
fileName - the name of the file to check.
Returns:
true if and only if the file or directory denoted by this abstract pathname exists; false otherwise

mkdirs

public static boolean mkdirs(java.lang.String path)
Creates a directory and parent directories if needed.

Parameters:
path - the path of the directory to create
Returns:
true if the directory was created (possibly with parent directories) , false otherwise

cp

public static void cp(java.lang.String source,
                      java.lang.String target)
Copy from source to target.

Parameters:
source - the source file.
target - the target file.

rm

public static boolean rm(java.lang.String fileName)
Removes a file from disk.

Parameters:
fileName - the file to remove
Returns:
true if and only if the file or directory is successfully deleted; false otherwise

isDirectory

public static boolean isDirectory(java.lang.String inputFile)
Tests whether the file denoted by this abstract pathname is a directory.

Parameters:
inputFile - A pathname string.
Returns:
true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise.