genopt.io
Class FileHandler

java.lang.Object
  extended by genopt.io.FileHandler
All Implemented Interfaces:
java.lang.Cloneable

public class FileHandler
extends java.lang.Object
implements java.lang.Cloneable

Object that handles file reading and writing and offers various manipulation and access methods of the file contents.

This project was carried out at:

and supported by

GenOpt Copyright (c) 1998-2011, The Regents of the University of California, through Lawrence Berkeley National Laboratory (subject to receipt of any required approvals from the U.S. Dept. of Energy). All rights reserved.

Version:
GenOpt(R) 3.1.0 (December 8, 2011)

Author:
Michael Wetter

Field Summary
protected  java.lang.String[] FileContents
          the content of the file
protected static java.lang.String FS
          System dependent file separator
private  int INCREMENT
          Increment for array that stores the Strings
private static java.lang.String LS
          System dependent line separator
protected  int nLines
          the number of lines in FileContents (occupied element in FileContents)
 
Constructor Summary
FileHandler(java.io.File theFile)
          Constructor.
FileHandler(java.lang.String pathAndName)
          constructor
FileHandler(java.lang.String[] FileLines)
          constructor
FileHandler(java.lang.String path, java.lang.String name)
          constructor
 
Method Summary
static java.lang.String[] addCanonicalPaths(java.lang.String[] str, java.lang.String userDir)
          Adds the canonical path name to the string, unless the string is already a canonical path or is an empty character sequence
static java.lang.String addCanonicalPaths(java.lang.String str, java.lang.String userDir)
          Adds the canonical path name to the string, unless the string is already a canonical path or is an empty character sequence
private  void addElement(java.lang.String s)
          adds an element to the object
static void appendToFile(java.lang.String fileName, java.lang.String text)
          Appends the text to a file and closes the file again
static void copyFile(java.lang.String sourcePath, java.lang.String sourceName, java.lang.String destinationPath, java.lang.String destinationName)
          copies file from source to destination
static java.lang.String cutBegin(java.lang.String s, java.lang.String prefix)
          cuts a suffix from a string.
static java.lang.String cutBeginAndEnd(java.lang.String s, java.lang.String prefix, java.lang.String suffix)
          cuts a leading and terminating string from a string.
static java.lang.String cutEnd(java.lang.String s, java.lang.String suffix)
          cuts a suffix from a string.
static java.lang.String cutEndLineBreak(java.lang.String line)
          cuts a line break ('\n' and/or '\r') from the end of the String
static boolean deleteDirectory(java.io.File path)
          Deletes directories, even if they are not empty
 java.lang.String[] getFileContentsString()
          gets the file contents in a String array where each entry contains the corresponding file line
 int getLineNumberWithString(java.lang.String MatchString, int StartLine)
          gets the number of the line where 'MatchString' occurs the first time.
static int getNextDelimiterPosition(java.lang.String Line, char Delimiter1, char Delimiter2)
          gets the lowest position of two delimiters in 'Line'.
 int getNumberOfLines()
          gets the number of lines
static boolean isEndOfLine(int b)
          checks if the byte is a line feed ('\n') or a carriage return ('\r')
static void makeDirectory(java.lang.String[] path)
          creates directories if they do not exist yet
static java.lang.String replaceAll(java.lang.String find, java.lang.String set, java.lang.String line)
          replaces all occurences of the String 'find' with the String 'set' (even if 'find' appears several times on the same line)
static java.lang.String[] replacePathsByCanonicalPaths(java.lang.String[] str, java.lang.String userDir)
          Replaces all paths with their canonical paths
static java.lang.String replacePathsByCanonicalPaths(java.lang.String str, java.lang.String userDir)
          Replaces all paths with their canonical paths
 boolean replaceString(java.lang.String Find, java.lang.String Set)
          replaces all occurrences of the String 'find' with the String 'set' (even if 'find' appears several times on the same line and/or in the same file)
 void writeFile(java.lang.String pathAndName)
          writes FileContents in a text file
static void writeFile(java.lang.String[] text, java.lang.String path, java.lang.String name)
          writes a String array in a text file
 void writeFile(java.lang.String path, java.lang.String name)
          writes FileContents in a text file
static void writeFile(java.lang.String text, java.lang.String path, java.lang.String name)
          writes a string to a text file
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LS

private static final java.lang.String LS
System dependent line separator


FS

protected static final java.lang.String FS
System dependent file separator


INCREMENT

private final int INCREMENT
Increment for array that stores the Strings

See Also:
Constant Field Values

FileContents

protected java.lang.String[] FileContents
the content of the file


nLines

protected int nLines
the number of lines in FileContents (occupied element in FileContents)

Constructor Detail

FileHandler

public FileHandler(java.lang.String[] FileLines)
constructor

Parameters:
FileLines - where each element is a line of the file stored in a String

FileHandler

public FileHandler(java.lang.String pathAndName)
            throws java.io.IOException
constructor

Parameters:
pathAndName - path and name of file
Throws:
java.io.IOException

FileHandler

public FileHandler(java.lang.String path,
                   java.lang.String name)
            throws java.io.IOException
constructor

Parameters:
path - path of file (or null pointer if file is in current directory
name - name of file
Throws:
java.io.IOException

FileHandler

public FileHandler(java.io.File theFile)
            throws java.io.IOException,
                   java.io.FileNotFoundException,
                   java.lang.SecurityException
Constructor.

Parameters:
theFile - the file
Throws:
java.io.IOException
java.io.FileNotFoundException
java.lang.SecurityException
Method Detail

appendToFile

public static void appendToFile(java.lang.String fileName,
                                java.lang.String text)
                         throws java.io.IOException
Appends the text to a file and closes the file again

Parameters:
fileName - file path and name where the text has to be appended
text - text to be written in the file
Throws:
java.io.IOException

addElement

private void addElement(java.lang.String s)
adds an element to the object

Parameters:
s - String to be added

isEndOfLine

public static final boolean isEndOfLine(int b)
checks if the byte is a line feed ('\n') or a carriage return ('\r')

Parameters:
b - the byte value to be checked
Returns:
true if b is a line feed or a carriage return

replaceString

public boolean replaceString(java.lang.String Find,
                             java.lang.String Set)
replaces all occurrences of the String 'find' with the String 'set' (even if 'find' appears several times on the same line and/or in the same file)

Parameters:
Find - String that has to be found and replaced
Set - String that has to be set at the position of 'Find'
Returns:
true if 'Find' has been found, false otherwise

replacePathsByCanonicalPaths

public static java.lang.String[] replacePathsByCanonicalPaths(java.lang.String[] str,
                                                              java.lang.String userDir)
                                                       throws java.io.IOException
Replaces all paths with their canonical paths

Parameters:
str - The original string
Returns:
A copy of str with all paths replaced by their canonical path
Throws:
java.io.IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries

replacePathsByCanonicalPaths

public static java.lang.String replacePathsByCanonicalPaths(java.lang.String str,
                                                            java.lang.String userDir)
                                                     throws java.io.IOException
Replaces all paths with their canonical paths

Parameters:
str - The original string
Returns:
A copy of str with the path replaced by its canonical path
Throws:
java.io.IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries

addCanonicalPaths

public static java.lang.String[] addCanonicalPaths(java.lang.String[] str,
                                                   java.lang.String userDir)
                                            throws java.io.IOException
Adds the canonical path name to the string, unless the string is already a canonical path or is an empty character sequence

Parameters:
str - The original strings
Returns:
A copy of str that denotes a canonical path name that may not exist on the file system
Throws:
java.io.IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries

addCanonicalPaths

public static java.lang.String addCanonicalPaths(java.lang.String str,
                                                 java.lang.String userDir)
                                          throws java.io.IOException
Adds the canonical path name to the string, unless the string is already a canonical path or is an empty character sequence

Parameters:
str - The original string
Returns:
A copy of str that denotes a canonical path name that may not exist on the file system
Throws:
java.io.IOException - If an I/O error occurs, which is possible because the construction of the canonical pathname may require filesystem queries

replaceAll

public static java.lang.String replaceAll(java.lang.String find,
                                          java.lang.String set,
                                          java.lang.String line)
replaces all occurences of the String 'find' with the String 'set' (even if 'find' appears several times on the same line)

Parameters:
find - String that has to be found and replaced
set - String that has to be set at the position of 'Find'
line - line on which replacement takes place
Returns:
the String after all replacements

getLineNumberWithString

public int getLineNumberWithString(java.lang.String MatchString,
                                   int StartLine)
gets the number of the line where 'MatchString' occurs the first time. The file contents is searched for 'MatchString' beginning at line Number 'StartLine' and towards the end of the line. The first line has number 0.

Parameters:
MatchString - String that has to be searched for
StartLine - number of Line where the search process starts
Returns:
the line number in which 'MatchString' occurs the first time or -1 if 'MatchString' was not found.

deleteDirectory

public static boolean deleteDirectory(java.io.File path)
                               throws java.lang.SecurityException
Deletes directories, even if they are not empty

Parameters:
path - Name of the directory
Returns:
true if and only if the file or directory is successfully deleted; false otherwise
Throws:
java.lang.SecurityException - If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies delete access to the file

getNextDelimiterPosition

public static int getNextDelimiterPosition(java.lang.String Line,
                                           char Delimiter1,
                                           char Delimiter2)
gets the lowest position of two delimiters in 'Line'. If only one delimiter is found in 'Line', then the position of the found delimiter is returned. If no delimiter is found in 'Line', then -1 is returned.

Parameters:
Line - Line to search for the delimiters
Delimiter1 - First delimiter
Delimiter2 - Second delimiter
Returns:
Lowest position of any delimiter, either 'Delimiter1' or 'Delimiter2'

getNumberOfLines

public int getNumberOfLines()
gets the number of lines

Returns:
number of elements of the trimed Vector

getFileContentsString

public java.lang.String[] getFileContentsString()
gets the file contents in a String array where each entry contains the corresponding file line

Returns:
String array where each entry contains the corresponding file line

copyFile

public static void copyFile(java.lang.String sourcePath,
                            java.lang.String sourceName,
                            java.lang.String destinationPath,
                            java.lang.String destinationName)
                     throws java.io.IOException
copies file from source to destination

Parameters:
sourcePath - path of source file (or null pointer if file is in current directory)
sourceName - name of source file
destinationPath - path of destination file (or null pointer if file is in current directory)
destinationName - name of destination file
Throws:
java.io.IOException

makeDirectory

public static void makeDirectory(java.lang.String[] path)
                          throws java.lang.Exception
creates directories if they do not exist yet

Parameters:
path - path that has to be created
Throws:
java.lang.Exception - if a SecurityException occured

writeFile

public static void writeFile(java.lang.String[] text,
                             java.lang.String path,
                             java.lang.String name)
                      throws java.io.IOException
writes a String array in a text file

Parameters:
text - array of text that has to be written into the file
path - path of file (or null pointer if file is in current directory)
name - name of file
Throws:
java.io.IOException

writeFile

public static void writeFile(java.lang.String text,
                             java.lang.String path,
                             java.lang.String name)
                      throws java.io.IOException
writes a string to a text file

Parameters:
text - text that has to be written to the file
path - path of file (or null pointer if file is in current directory)
name - name of file
Throws:
java.io.IOException

writeFile

public void writeFile(java.lang.String pathAndName)
               throws java.io.IOException
writes FileContents in a text file

Parameters:
pathAndName - path and name of file
Throws:
java.io.IOException

writeFile

public void writeFile(java.lang.String path,
                      java.lang.String name)
               throws java.io.IOException
writes FileContents in a text file

Parameters:
path - path of file (or null pointer if file is in current directory)
name - name of file
Throws:
java.io.IOException

cutBeginAndEnd

public static java.lang.String cutBeginAndEnd(java.lang.String s,
                                              java.lang.String prefix,
                                              java.lang.String suffix)
cuts a leading and terminating string from a string. If the string has no leading and terminating characters, it is returned unmodified.
Note: the strings are cutted only once, that means a string of the form """abc"" is returned as ""abc" if the prefix and suffix are specified as: "

Parameters:
s - the string
prefix - the leading string
suffix - the last string
Returns:
the cutted string

cutBegin

public static java.lang.String cutBegin(java.lang.String s,
                                        java.lang.String prefix)
cuts a suffix from a string. If the string is not beginning with the specified string, it is returned unmodified.

Parameters:
s - the string
prefix - the prefix
Returns:
the cutted string

cutEnd

public static java.lang.String cutEnd(java.lang.String s,
                                      java.lang.String suffix)
cuts a suffix from a string. If the string is not ending with the specified string, it is returned unmodified.

Parameters:
s - the string
suffix - the suffix
Returns:
the cutted string

cutEndLineBreak

public static final java.lang.String cutEndLineBreak(java.lang.String line)
cuts a line break ('\n' and/or '\r') from the end of the String

Parameters:
line - the String to be cutted
Returns:
the String without line break