Class FileUtils
- java.lang.Object
-
- com.sun.identity.install.tools.util.FileUtils
-
public class FileUtils extends Object
-
-
Field Summary
Fields Modifier and Type Field Description static StringEQUAL_TOField HASH *static StringEXAMPLE_FIELDField EXAMPLE_FIELD *static StringHASHField HASH *static StringSPACEField HASH *static StringSQRBRACKET_CLOSEField SQRBRACKET_CLOSE *static StringSQRBRACKET_OPENField SQRBRACKET_OPEN *
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanaddListProperty(String fileName, String property, String value)Adds an entry to a list property in Product Config propertiesstatic booleanaddMapProperty(String configPath, String key, String name, String value)Method addMapProperty Adds a map property like key[name]=value in PRODUCT Config.propertiesstatic voidappendDataToFile(String fileName, String data)static booleanappendLinesToFile(String filePath, String[] linesToAppend)Appends the given set of lines to the specified file.static booleanappendLinesToFile(String filePath, String[] linesToAppend, boolean create)Appends the given set of lines to the specified file.static voidbackupFile(String fileName, String suffix)static voidcopyDirContents(File srcDir, File destDir)Method copyDirContentsstatic booleancopyFile(String source, String destination)Copies a filestatic voidcopyJarFile(String srcDir, String destDir, String filename)Method copyJarFile to destination directorystatic intgetFirstOccurence(String filePath, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase)Method getFirstOccurencestatic intgetFirstOccurence(String filePath, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase, int beginAtLine)Method getFirstOccurencestatic intgetLastOccurence(String filePath, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase)Method getLastOccurencestatic StringgetLineByNumber(String filePath, int lineNum)Method getLineByNumberstatic StringgetLineWithPattern(String filePath, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase, int beginAtLine)Method getLineWithPatternstatic StringgetParentDirPath(String path)Returns the parent folder of the given file/folderstatic StringgetParentDirPath(String path, int level)Returns the parent folder upper level count to the current pathstatic PropertiesgetProperties(String configPath)Method getProperties Loads a properties file into memory and creates ajava.util.Propertiesobjectstatic intgetTotalLineNums(String fileName)Method declarationstatic booleaninsertLineByNumber(String filePath, int lineNum, String line)Method insertLineByNumberstatic booleanisDirValid(String directory)static booleanisFileValid(String filename)static booleanmatchPattern(String line, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase)Method matchPatternstatic booleanremoveDir(File dir)Method removeDir.static voidremoveFiles(String srcDir, String desDir)Method removeFiles.static voidremoveJarFiles(String srcDir, String fileName)Method removeJarFiles. jar files will be removed from source directorystatic voidremoveLines(String fileName, String value)static booleanremoveLinesByNum(String filePath, int lineNum, int numLines)Method removeLinesByNumstatic StringremoveLinesByNumber(String filePath, int lineNum, int numLines)Method removeLinesByNumberstatic StringreplaceBackWithForward(String str)Method replaceBackWithForward
-
-
-
Field Detail
-
SQRBRACKET_OPEN
public static final String SQRBRACKET_OPEN
Field SQRBRACKET_OPEN *- See Also:
- Constant Field Values
-
SQRBRACKET_CLOSE
public static final String SQRBRACKET_CLOSE
Field SQRBRACKET_CLOSE *- See Also:
- Constant Field Values
-
EXAMPLE_FIELD
public static final String EXAMPLE_FIELD
Field EXAMPLE_FIELD *- See Also:
- Constant Field Values
-
HASH
public static final String HASH
Field HASH *- See Also:
- Constant Field Values
-
EQUAL_TO
public static final String EQUAL_TO
Field HASH *- See Also:
- Constant Field Values
-
SPACE
public static final String SPACE
Field HASH *- See Also:
- Constant Field Values
-
-
Method Detail
-
copyFile
public static boolean copyFile(String source, String destination) throws Exception
Copies a file- Parameters:
source- filedestination- file- Returns:
- true if copy operation succeed
- Throws:
Exception
-
copyJarFile
public static void copyJarFile(String srcDir, String destDir, String filename) throws Exception
Method copyJarFile to destination directory- Parameters:
srcDir- the source directory containing the jar filedestDir- the destination directory to copy the jar file tofilename- the name of the jar file to copy- Throws:
Exception
-
copyDirContents
public static void copyDirContents(File srcDir, File destDir) throws Exception
Method copyDirContents- Parameters:
srcDir- the source directory whose contents are copieddestDir- the destination directory to copy contents into- Throws:
Exception
-
removeDir
public static boolean removeDir(File dir)
Method removeDir. All the files in the directory will be removed- Parameters:
dir- to be removed. @ return true if the directory is deleted. False other wise
-
removeJarFiles
public static void removeJarFiles(String srcDir, String fileName)
Method removeJarFiles. jar files will be removed from source directory- Parameters:
srcDir- - Source directoryfileName- - file to be deleted
-
removeFiles
public static void removeFiles(String srcDir, String desDir)
Method removeFiles. All the files existing in source directory will be removed from Destination Directory- Parameters:
srcDir- - Source directory to compare filesdesDir- - Destination directory to remove files
-
removeLines
public static void removeLines(String fileName, String value) throws Exception
- Throws:
Exception
-
appendDataToFile
public static void appendDataToFile(String fileName, String data) throws Exception
- Throws:
Exception
-
backupFile
public static void backupFile(String fileName, String suffix) throws Exception
- Throws:
Exception
-
isFileValid
public static boolean isFileValid(String filename)
-
replaceBackWithForward
public static String replaceBackWithForward(String str)
Method replaceBackWithForward- Parameters:
str- the string in which back slashes are replaced- Returns:
- the string with all back slashes replaced by forward slashes
-
isDirValid
public static boolean isDirValid(String directory)
-
getLineByNumber
public static String getLineByNumber(String filePath, int lineNum)
Method getLineByNumber- Parameters:
filePath- the path of the file to readlineNum- the 1-based line number to retrieve- Returns:
- the contents of the specified line, or null if it cannot be read
-
matchPattern
public static boolean matchPattern(String line, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase)
Method matchPattern- Parameters:
line- the line of text to testpattern- the pattern to match within the linematchBegin- whether the pattern must match at the beginning of the linematchEnd- whether the pattern must match at the end of the lineignoreCase- whether the match should be case-insensitive- Returns:
- true if the line matches the pattern according to the given options
-
getLineWithPattern
public static String getLineWithPattern(String filePath, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase, int beginAtLine)
Method getLineWithPattern- Parameters:
filePath- the path of the file to searchpattern- the pattern to search formatchBegin- whether the pattern must match at the beginning of a linematchEnd- whether the pattern must match at the end of a lineignoreCase- whether the match should be case-insensitivebeginAtLine- the line number at which to start searching- Returns:
- the first line that matches the pattern, or null if none is found
-
getFirstOccurence
public static int getFirstOccurence(String filePath, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase, int beginAtLine)
Method getFirstOccurence- Parameters:
filePath- the path of the file to searchpattern- the pattern to search formatchBegin- whether the pattern must match at the beginning of a linematchEnd- whether the pattern must match at the end of a lineignoreCase- whether the match should be case-insensitivebeginAtLine- the line number at which to start searching- Returns:
- the line number of the first match, or -1 if none is found
-
getFirstOccurence
public static int getFirstOccurence(String filePath, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase)
Method getFirstOccurence- Parameters:
filePath- the path of the file to searchpattern- the pattern to search formatchBegin- whether the pattern must match at the beginning of a linematchEnd- whether the pattern must match at the end of a lineignoreCase- whether the match should be case-insensitive- Returns:
- the line number of the first match, or -1 if none is found
-
getLastOccurence
public static int getLastOccurence(String filePath, String pattern, boolean matchBegin, boolean matchEnd, boolean ignoreCase)
Method getLastOccurence- Parameters:
filePath- the path of the file to searchpattern- the pattern to search formatchBegin- whether the pattern must match at the beginning of a linematchEnd- whether the pattern must match at the end of a lineignoreCase- whether the match should be case-insensitive- Returns:
- the line number of the last match, or -1 if none is found
-
addMapProperty
public static boolean addMapProperty(String configPath, String key, String name, String value)
Method addMapProperty Adds a map property like key[name]=value in PRODUCT Config.properties- Parameters:
key- key name of the propertyname- name of the propertyvalue- value of the property
-
addListProperty
public static boolean addListProperty(String fileName, String property, String value)
Adds an entry to a list property in Product Config properties- Parameters:
fileName- name of the config fileproperty- property name of the property for the listvalue- name of the list property to be added Ex: Adds a property like property[index] = value in Product Config properties- Returns:
- boolean true or false
-
getTotalLineNums
public static int getTotalLineNums(String fileName)
Method declaration- Parameters:
fileName- the path of the file whose lines are counted- Returns:
- the total number of lines in the file
-
insertLineByNumber
public static boolean insertLineByNumber(String filePath, int lineNum, String line)
Method insertLineByNumber- Parameters:
filePath- the path of the file to modifylineNum- the 1-based line number at which to insertline- the line of text to insert- Returns:
- true if the line was inserted successfully
-
removeLinesByNum
public static boolean removeLinesByNum(String filePath, int lineNum, int numLines)
Method removeLinesByNum- Parameters:
filePath- the path of the file to modifylineNum- begining line numbernumLines- total number of lines to remove- Returns:
- true if removal is successful
-
removeLinesByNumber
public static String removeLinesByNumber(String filePath, int lineNum, int numLines)
Method removeLinesByNumber- Parameters:
filePath- the path of the file to modifylineNum- the beginning line number (1-based)numLines- the number of lines to remove- Returns:
- the last removed line, or
nullif the removal failed
-
appendLinesToFile
public static boolean appendLinesToFile(String filePath, String[] linesToAppend)
Appends the given set of lines to the specified file.- Parameters:
filePath- the path of the file to append tolinesToAppend- the lines to append to the file- Returns:
- true for success, false otherwise
-
appendLinesToFile
public static boolean appendLinesToFile(String filePath, String[] linesToAppend, boolean create)
Appends the given set of lines to the specified file.- Parameters:
filePath- the path of the file to append tolinesToAppend- the lines to append to the filecreate- should the file be created if it does not exist- Returns:
- true for success, false otherwise
-
getProperties
public static Properties getProperties(String configPath)
Method getProperties Loads a properties file into memory and creates ajava.util.Propertiesobject- Parameters:
configPath- path to the properties file- Returns:
- prop a
java.util.Propertiesobject from the specified file ornullin case of an error in processing
-
getParentDirPath
public static String getParentDirPath(String path, int level)
Returns the parent folder upper level count to the current path- Parameters:
path- path to a file/folderlevel- how much should we go upper in the path- Returns:
- path to the parent upper the given level, for example: (/a/b/c/d, 3) will result in /a
-
-