public class FileHandler
extends java.lang.Object
implements java.lang.Cloneable
This project was carried out at:
and supported byGenOpt Copyright (c) 1998-2021, 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.
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
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
|
Modifier and Type | Method and Description |
---|---|
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
|
private static final java.lang.String LS
protected static final java.lang.String FS
private final int INCREMENT
protected java.lang.String[] FileContents
protected int nLines
FileContents
)public FileHandler(java.lang.String[] FileLines)
FileLines
- where
each element is a line of the file stored in a Stringpublic FileHandler(java.lang.String pathAndName) throws java.io.IOException
pathAndName
- path and name of filejava.io.IOException
public FileHandler(java.lang.String path, java.lang.String name) throws java.io.IOException
path
- path of file (or null pointer if file is in current
directoryname
- name of filejava.io.IOException
public FileHandler(java.io.File theFile) throws java.io.IOException, java.io.FileNotFoundException, java.lang.SecurityException
theFile
- the filejava.io.IOException
java.io.FileNotFoundException
java.lang.SecurityException
public static void appendToFile(java.lang.String fileName, java.lang.String text) throws java.io.IOException
fileName
- file path and name where the text has to be appendedtext
- text to be written in the filejava.io.IOException
private void addElement(java.lang.String s)
s
- String to be addedpublic static final boolean isEndOfLine(int b)
b
- the byte value to be checkedtrue
if b
is a line feed or a carriage returnpublic boolean replaceString(java.lang.String Find, java.lang.String Set)
Find
- String that has to be found and replacedSet
- String that has to be set at the position of 'Find'true
if 'Find' has been found, false
otherwisepublic static java.lang.String[] replacePathsByCanonicalPaths(java.lang.String[] str, java.lang.String userDir) throws java.io.IOException
str
- The original stringjava.io.IOException
- If an I/O error occurs, which is possible because the construction of the
canonical pathname may require filesystem queriespublic static java.lang.String replacePathsByCanonicalPaths(java.lang.String str, java.lang.String userDir) throws java.io.IOException
str
- The original stringjava.io.IOException
- If an I/O error occurs, which is possible because the construction of the
canonical pathname may require filesystem queriespublic static java.lang.String[] addCanonicalPaths(java.lang.String[] str, java.lang.String userDir) throws java.io.IOException
str
- The original stringsjava.io.IOException
- If an I/O error occurs, which is possible because the construction of the
canonical pathname may require filesystem queriespublic static java.lang.String addCanonicalPaths(java.lang.String str, java.lang.String userDir) throws java.io.IOException
str
- The original stringjava.io.IOException
- If an I/O error occurs, which is possible because the construction of the
canonical pathname may require filesystem queriespublic static java.lang.String replaceAll(java.lang.String find, java.lang.String set, java.lang.String line)
find
- String that has to be found and replacedset
- String that has to be set at the position of 'Find'line
- line on which replacement takes placepublic int getLineNumberWithString(java.lang.String MatchString, int StartLine)
MatchString
- String that has to be searched forStartLine
- number of Line where the search process startspublic static boolean deleteDirectory(java.io.File path) throws java.lang.SecurityException
path
- Name of the directoryjava.lang.SecurityException
- If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies delete access to the filepublic static int getNextDelimiterPosition(java.lang.String Line, char Delimiter1, char Delimiter2)
Line
- Line to search for the delimitersDelimiter1
- First delimiterDelimiter2
- Second delimiterpublic int getNumberOfLines()
public java.lang.String[] getFileContentsString()
public static void copyFile(java.lang.String sourcePath, java.lang.String sourceName, java.lang.String destinationPath, java.lang.String destinationName) throws java.io.IOException
sourcePath
- path of source file (or null pointer if file is in current
directory)sourceName
- name of source filedestinationPath
- path of destination file (or null pointer if file is in current
directory)destinationName
- name of destination filejava.io.IOException
public static void makeDirectory(java.lang.String[] path) throws java.lang.Exception
path
- path that has to be createdjava.lang.Exception
- if a SecurityException occuredpublic static void writeFile(java.lang.String[] text, java.lang.String path, java.lang.String name) throws java.io.IOException
text
- array of text that has to be written into the filepath
- path of file (or null pointer if file is in current
directory)name
- name of filejava.io.IOException
public static void writeFile(java.lang.String text, java.lang.String path, java.lang.String name) throws java.io.IOException
text
- text that has to be written to the filepath
- path of file (or null pointer if file is in current
directory)name
- name of filejava.io.IOException
public void writeFile(java.lang.String pathAndName) throws java.io.IOException
pathAndName
- path and name of filejava.io.IOException
public void writeFile(java.lang.String path, java.lang.String name) throws java.io.IOException
path
- path of file (or null pointer if file is in current
directory)name
- name of filejava.io.IOException
public static java.lang.String cutBeginAndEnd(java.lang.String s, java.lang.String prefix, java.lang.String suffix)
"""abc""
is returned as ""abc"
if the
prefix and suffix are specified as: "
s
- the stringprefix
- the leading stringsuffix
- the last stringpublic static java.lang.String cutBegin(java.lang.String s, java.lang.String prefix)
s
- the stringprefix
- the prefixpublic static java.lang.String cutEnd(java.lang.String s, java.lang.String suffix)
s
- the stringsuffix
- the suffixpublic static final java.lang.String cutEndLineBreak(java.lang.String line)
line
- the String to be cutted