Lightweight Bartering Grid

lbg.common.io
Class CommonFileHandling

java.lang.Object
  extended by lbg.common.io.CommonFileHandling

public abstract class CommonFileHandling
extends java.lang.Object

A CommonFileHandling provides common File handling routines.

Author:
Cyril Briquet, Xavier Dalem

Field Summary
static java.lang.String CLASS_EXT
          Class file default extension.
static java.lang.String CSV_EXT
          CSV spreadsheet file default extension.
static java.lang.String FILE_SEP
          Filesystem file separator (i.e.
static java.lang.String JAR_EXT
          JAR file default extension.
static java.lang.String LOG_EXT
          Log file default extension.
static java.lang.String LOG_PREFIX
          Log file default prefix.
static java.lang.String OUTPUT_DATA_EXT
          Output data file default extension.
static java.lang.String PDF_EXT
          PDF file default extension.
static java.lang.String TXT_EXT
          Text file default extension.
 
Constructor Summary
CommonFileHandling()
           
 
Method Summary
static void closeBR(java.io.BufferedReader br, java.lang.String fn)
          Closes target BufferedReader encapsulating target filename.
static void copyFile(java.lang.String fn1, java.lang.String fn2)
          Copies target source file to target destination file.
static void createDirectory(java.lang.String target_dir)
          Creates a new directory.
static boolean dump(java.lang.Object obj)
          Dumps target Object to standard output.
static boolean dumpSeparator()
          Dumps separating line to System console standard output.
static java.lang.String extractBaseFileNameFromFileName(java.lang.String fn, java.lang.String ext)
          Extracts base file name (without extension) from target file name without complete path.
static java.lang.String extractBaseFileNameFromPath(java.lang.String fn, java.lang.String ext)
          Extracts base file name (without extension) from target file name with complete path.
static java.lang.String extractFileNameFromPath(java.lang.String fn)
          Extracts file name from target file name with complete path.
static java.lang.String extractMainClass(java.lang.String jarfn)
          Extracts the main class of the target jar file's embedded manifest.
static java.io.File getFirstMatchingFile(java.io.File f)
          Returns The first non-existing file matching the original one.
static boolean isAbsolutePath(java.lang.String path)
          Checks if target path is absolute.
static boolean isCSVFileName(java.lang.String fn)
          Checks if target file name is a CSV spreadsheet file name.
static boolean isJarFileName(java.lang.String fn)
          Checks if target file name is a Java archive file name.
static boolean isLogFilename(java.lang.String fn)
          Checks if target file name is a log file name.
static boolean isPDFFileName(java.lang.String fn)
          Checks if target file name is a PDF file name.
static boolean isTextFileName(java.lang.String fn)
          Checks if target file name is a text file name.
static java.util.Properties loadProperties(java.lang.String fn)
          Loads Properties from target text file.
static java.io.BufferedReader openBR(java.lang.String fn)
          Opens a BufferedReader encapsulating target filename.
static java.lang.String readFile(java.lang.String fn)
          Reads text file contents given target filename.
static java.lang.String readFile(java.lang.String fn, boolean dont_complain)
          Reads text file contents given target filename.
static java.lang.String readLine(java.io.BufferedReader br, java.lang.String fn)
          Reads one text line from target BufferedReader encapsulating target filename.
static void removeDirectory(java.lang.String target_dir)
          Recursively removes a directory and all its contents, including subdirectories.
static void removeDirectory(java.lang.String target_dir, boolean fail_silently)
          Recursively removes a directory and all its contents, including subdirectories.
static void writeFile(java.io.File f, java.lang.String data)
          Writes target data to target file.
static boolean writeFile(java.lang.String fn, java.lang.String data)
          Writes target data to target file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FILE_SEP

public static final java.lang.String FILE_SEP
Filesystem file separator (i.e. / or \ depending on O.S. type).


CLASS_EXT

public static final java.lang.String CLASS_EXT
Class file default extension.

See Also:
Constant Field Values

CSV_EXT

public static final java.lang.String CSV_EXT
CSV spreadsheet file default extension.

See Also:
Constant Field Values

JAR_EXT

public static final java.lang.String JAR_EXT
JAR file default extension.

See Also:
Constant Field Values

LOG_EXT

public static final java.lang.String LOG_EXT
Log file default extension.

See Also:
Constant Field Values

LOG_PREFIX

public static final java.lang.String LOG_PREFIX
Log file default prefix.

See Also:
Constant Field Values

OUTPUT_DATA_EXT

public static final java.lang.String OUTPUT_DATA_EXT
Output data file default extension.

See Also:
Constant Field Values

PDF_EXT

public static final java.lang.String PDF_EXT
PDF file default extension.

See Also:
Constant Field Values

TXT_EXT

public static final java.lang.String TXT_EXT
Text file default extension.

See Also:
Constant Field Values
Constructor Detail

CommonFileHandling

public CommonFileHandling()
Method Detail

openBR

public static java.io.BufferedReader openBR(java.lang.String fn)
                                     throws GridException
Opens a BufferedReader encapsulating target filename.

Parameters:
fn - target filename
Returns:
encapsulating BufferedReader
Throws:
GridException - if target file cannot be opened

closeBR

public static void closeBR(java.io.BufferedReader br,
                           java.lang.String fn)
                    throws GridException
Closes target BufferedReader encapsulating target filename.

Parameters:
br - target encapsulating BufferedReader
fn - target filename
Throws:
GridException - if target file cannot be closed

readLine

public static java.lang.String readLine(java.io.BufferedReader br,
                                        java.lang.String fn)
                                 throws GridException
Reads one text line from target BufferedReader encapsulating target filename.

Parameters:
br - target encapsulating BufferedReader
fn - target filename
Returns:
read line, or null if there is no more line to read
Throws:
GridException - if current line cannot be read

readFile

public static java.lang.String readFile(java.lang.String fn)
                                 throws GridException
Reads text file contents given target filename.

Parameters:
fn - target filename
Returns:
read file
Throws:
GridException - if a parameter is illegal, or if an error occurs while reding the file

readFile

public static java.lang.String readFile(java.lang.String fn,
                                        boolean dont_complain)
                                 throws GridException
Reads text file contents given target filename.

Parameters:
fn - target filename
dont_complain - if true, no Exception will be thrown, and null will be returned
Returns:
read file, or null if dont_complain is true, and either a parameter is illegal, or an error occurs while reding the file
Throws:
GridException - if dont_complain is false, and either a parameter is illegal, or an error occurs while reding the file

writeFile

public static boolean writeFile(java.lang.String fn,
                                java.lang.String data)
                         throws GridException
Writes target data to target file.

Parameters:
fn - target filename
data - target data
Returns:
true if write operation was a success
Throws:
GridException - if target data cannot be written

writeFile

public static void writeFile(java.io.File f,
                             java.lang.String data)
                      throws GridException
Writes target data to target file.

Parameters:
f - target file
data - target data
Throws:
GridException - if target data cannot be written

copyFile

public static void copyFile(java.lang.String fn1,
                            java.lang.String fn2)
                     throws GridException
Copies target source file to target destination file.

Parameters:
fn1 - target source filename
fn2 - target destination filename
Throws:
GridException - if an error occurs while copying the file

getFirstMatchingFile

public static java.io.File getFirstMatchingFile(java.io.File f)
Returns The first non-existing file matching the original one.

Files are checked in a wget fashion. If the supplied files doesn't exist, it is returned. If it exists, a new file of the same name, but with a ".0" appended to it. As long as the files exist, the suffix will be incremented, returning the first non-existing file.

For example, if the files "foo", "foo.0" and "foo.2" exist, this method applied to new File("foo") will return a File("foo.1")

Parameters:
f - base file
Returns:
file that matches the above contract

createDirectory

public static void createDirectory(java.lang.String target_dir)
                            throws GridException
Creates a new directory.

Parameters:
target_dir - target directory to create
Throws:
GridException - if a parameter is illegal

removeDirectory

public static void removeDirectory(java.lang.String target_dir)
                            throws GridException
Recursively removes a directory and all its contents, including subdirectories.

Parameters:
target_dir - target directory to remove
Throws:
GridException - if a parameter is illegal

removeDirectory

public static void removeDirectory(java.lang.String target_dir,
                                   boolean fail_silently)
                            throws GridException
Recursively removes a directory and all its contents, including subdirectories.

Parameters:
target_dir - target directory to remove
fail_silently - if true, no GridException is thrown
Throws:
GridException - if a parameter is illegal

loadProperties

public static java.util.Properties loadProperties(java.lang.String fn)
                                           throws GridException
Loads Properties from target text file.

Parameters:
fn - target text filename
Throws:
GridException - if an error occurs while loading the Properties text file

dump

public static boolean dump(java.lang.Object obj)
Dumps target Object to standard output.

Parameters:
obj - target Object to dump to standard output
Returns:
true if Object is legal

dumpSeparator

public static boolean dumpSeparator()
Dumps separating line to System console standard output.


extractFileNameFromPath

public static java.lang.String extractFileNameFromPath(java.lang.String fn)
                                                throws GridException
Extracts file name from target file name with complete path.

Parameters:
fn - target file name with complete path
Returns:
extracted file name
Throws:
GridException - if target file name with complete path is illegal

extractBaseFileNameFromFileName

public static java.lang.String extractBaseFileNameFromFileName(java.lang.String fn,
                                                               java.lang.String ext)
                                                        throws GridException
Extracts base file name (without extension) from target file name without complete path.

Parameters:
fn - target file name without complete path
ext - target extension
Returns:
extracted base file name
Throws:
GridException - if target file name without complete path or target extension is illegal

extractBaseFileNameFromPath

public static java.lang.String extractBaseFileNameFromPath(java.lang.String fn,
                                                           java.lang.String ext)
                                                    throws GridException
Extracts base file name (without extension) from target file name with complete path.

Parameters:
fn - target file name with complete path
ext - target extension
Returns:
extracted base file name
Throws:
GridException - if target file name with complete path or target extension is illegal

isAbsolutePath

public static boolean isAbsolutePath(java.lang.String path)
Checks if target path is absolute.

Parameters:
path - target path to check
Returns:
true if target path is absolute
See Also:
File.isAbsolute()

isJarFileName

public static boolean isJarFileName(java.lang.String fn)
Checks if target file name is a Java archive file name.

Parameters:
fn - target file name
Returns:
true if target file name is a Java archive file name

isTextFileName

public static boolean isTextFileName(java.lang.String fn)
Checks if target file name is a text file name.

Parameters:
fn - target file name
Returns:
true if target file name is a text file name

isCSVFileName

public static boolean isCSVFileName(java.lang.String fn)
Checks if target file name is a CSV spreadsheet file name.

Parameters:
fn - target file name
Returns:
true if target file name is a CSV spreadsheet file name

isPDFFileName

public static boolean isPDFFileName(java.lang.String fn)
Checks if target file name is a PDF file name.

Parameters:
fn - target file name
Returns:
true if target file name is a PDF file name

isLogFilename

public static boolean isLogFilename(java.lang.String fn)
Checks if target file name is a log file name.

Parameters:
fn - target file name
Returns:
true if target file name is a log file name

extractMainClass

public static java.lang.String extractMainClass(java.lang.String jarfn)
                                         throws GridException
Extracts the main class of the target jar file's embedded manifest.

Parameters:
jarfn - target jar file name
Returns:
main class of the target jar, or null if the main class cannot be found
Throws:
GridException - if a parameter is illegal, or if the target jar cannot be processed

Lightweight Bartering Grid

Copyright (c) 2005-2008, Cyril Briquet, parts Xavier Dalem.