org.openadaptor.util
Class FileUtils

java.lang.Object
  extended by org.openadaptor.util.FileUtils

public class FileUtils
extends java.lang.Object


Field Summary
(package private) static org.apache.log4j.Logger log
           
 
Constructor Summary
FileUtils()
           
 
Method Summary
static void appendTimeStamp(java.lang.StringBuffer sb)
          Appends current time to String buffer in a Timestamp format.
static void deleteFile(java.lang.String fileName)
          deletes the supplied file.
static boolean deleteFileFailSilently(java.lang.String fileName)
          deletes the supplied file.
static boolean fileExists(java.lang.String fileName)
          checks that the file corresponding to the file name passed is valid (ie.
static java.io.Reader fileReaderWithEncoding(java.lang.String filename, java.lang.String enc)
           Opens a Reader on a file encoded with the supplied encodingg format.
static long getLineCount(java.lang.String fileName)
          returns the number of lines in the file defined by the file name supplied
static java.lang.String newFilenameFromPattern(java.lang.String pattern)
          Creates a filename from the pattern string supplied.
static java.io.FileWriter newFileWriterFromPattern(java.lang.String pattern, boolean append)
          Creates a FileWriter on a file whose name is generated from pattern.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

static org.apache.log4j.Logger log
Constructor Detail

FileUtils

public FileUtils()
Method Detail

newFileWriterFromPattern

public static java.io.FileWriter newFileWriterFromPattern(java.lang.String pattern,
                                                          boolean append)
                                                   throws java.io.IOException
Creates a FileWriter on a file whose name is generated from pattern.

Parameters:
pattern - Filename pattern.
append - Append to any existing file?
Returns:
FileWriter, or null if pattern is null.
Throws:
java.io.IOException - If we cannot create the new file for writing.

newFilenameFromPattern

public static java.lang.String newFilenameFromPattern(java.lang.String pattern)
                                               throws java.io.IOException
Creates a filename from the pattern string supplied. The pattern syntax allows some metavalues to be substituted:
%T
Insert a timestamp in yyyymmddThhmmss format
THE FOLLOWING NOT YET SUPPORTED
%A
Insert Adaptor name
%C
Insert adaptor Component name
%{digit}I
Insert next valid sequence number padded with {digit} leading zeros (digit zero means ten}
E.g. %A.%C_%T.log might yield FooBarLink.JMSSource_20020311T123456_789.log Note that only ONE timestamp (%T) is allowed per pattern.

Parameters:
pattern - Pattern string for filename.
Returns:
new filename, or null if pattern was null.
Throws:
java.io.IOException

appendTimeStamp

public static void appendTimeStamp(java.lang.StringBuffer sb)
Appends current time to String buffer in a Timestamp format. Format is yyyymmddThhmmss_fff (where fff is milliseconds).


fileReaderWithEncoding

public static java.io.Reader fileReaderWithEncoding(java.lang.String filename,
                                                    java.lang.String enc)
                                             throws java.io.FileNotFoundException,
                                                    java.io.UnsupportedEncodingException,
                                                    java.io.IOException
 Opens a Reader on a file encoded with the supplied encodingg format. Supported
 formats are the standard Java encodings (UTF-8, ISO-8859-1 etc.). You may use
 an URL in the InputFileName property. The FileSource will attempt to connect
 to the remote server and open an InputStream to the file using the java.net.URL
 libraries. Note that you must include the protocol indicator at the start of
 the URL for this to work.

                A.C1.InputFileName = http://www.myserver.com/path/data-file.txt

 

Parameters:
filename - - Name of file to be opened, or null for stdin.
enc - - Encoding name, see Java encodings. Null implies default value ISO-8859-1.
Returns:
Reader for the file.
Throws:
java.io.FileNotFoundException - - If given file is not found.
java.io.UnsupportedEncodingException - - If the encoding name is not recognised.
java.io.IOException - - If there are problems when using URL's

getLineCount

public static long getLineCount(java.lang.String fileName)
                         throws java.io.IOException
returns the number of lines in the file defined by the file name supplied

Parameters:
fileName - - the name (including path) of the file to check
Returns:
- int count of the number of lines (0 if empty)
Throws:
java.lang.IllegalArgumentException - - if the file name passed was null, the file was not found, was not a normal file, was unable to be read
java.io.IOException - - if there was a problem reading the data from the file when trying to determine the line count

fileExists

public static boolean fileExists(java.lang.String fileName)
checks that the file corresponding to the file name passed is valid (ie. exists and is a file)

Parameters:
fileName - - the name of the file to check
Returns:
true if the file exists and is a file

deleteFileFailSilently

public static boolean deleteFileFailSilently(java.lang.String fileName)
deletes the supplied file. Checks to see if the file exists and has write access before it will do anything. Any errors will be written to the log and false returned.

Parameters:
fileName - the file to delete
Returns:
true if the file was deleted

deleteFile

public static void deleteFile(java.lang.String fileName)
                       throws IbafException
deletes the supplied file. Checks to see if the file exists and has write access before it will do anything.

Parameters:
fileName - the file to delete
Throws:
IbafException - - if the file can't be written to or is a directory or there was some sort of IO Error.