Package org.technologybrewery.baton.util
Class FileUtils
- java.lang.Object
-
- org.technologybrewery.baton.util.FileUtils
-
public final class FileUtils extends Object
Common file-related logic used when performing a migration.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringgetIndent(String line, int level)Infers the indentation style from the given line.static List<String>getRegExCaptureGroups(String regex, File file)Retrieves capture groups from a regular expression match inside a file.static List<String>getRegExCaptureGroups(String regex, String input)Retrieves capture groups from a regular expression match inside a String.static booleanhasRegExMatch(String regex, File file)Evaluates a regex pattern against a file to determine if at least one regex match existsstatic voidindentValues(List<String> values, int numSpaces)Indent the values the desired number of tabs with a variable tab size.static booleanmodifyRegexMatchInFile(File file, String regex, String substring, String replacement)Evaluates a file against a regex pattern and replaces a substring of each regex match with a specified replacement.static List<String>readAllFileLines(File file)static booleanreplaceInFile(File file, String regex, String replacement)Evaluates a file against a regex pattern and replaces the captured string(s) of each regex match with a specified replacementstatic booleanreplaceLiteralInFile(File file, String toReplace, String replacement)Evaluates a file against a literal string and replaces each match with a specified replacement.static voidwriteFile(File file, List<String> contents)
-
-
-
Method Detail
-
replaceLiteralInFile
public static boolean replaceLiteralInFile(File file, String toReplace, String replacement) throws IOException
Evaluates a file against a literal string and replaces each match with a specified replacement.- Parameters:
file- the FiletoReplace- a string representing the text to replacereplacement- the replacement text to substitute the toReplace string- Returns:
- a boolean set to true if at least one replacement was performed in the file
- Throws:
IOException
-
replaceInFile
public static boolean replaceInFile(File file, String regex, String replacement) throws IOException
Evaluates a file against a regex pattern and replaces the captured string(s) of each regex match with a specified replacement- Parameters:
file- the Fileregex- a regex representing the text to replace, as a Stringreplacement- the replacement text to substitute the regex- Returns:
- a boolean set to true if at least one replacement was performed in the file
- Throws:
IOException
-
modifyRegexMatchInFile
public static boolean modifyRegexMatchInFile(File file, String regex, String substring, String replacement)
Evaluates a file against a regex pattern and replaces a substring of each regex match with a specified replacement.- Parameters:
file- the Fileregex- a regex representing the text to replace a substring ofsubstring- the substring of the regex match that will be replacedreplacement- the replacement of the match substring- Returns:
- a boolean set to true if at least one modification was performed in the file
-
readAllFileLines
public static List<String> readAllFileLines(File file) throws IOException
- Parameters:
file-Fileto read- Returns:
Listof the contents- Throws:
IOException
-
writeFile
public static void writeFile(File file, List<String> contents) throws IOException
- Parameters:
file-Fileto writecontents-Listof the contents- Throws:
IOException
-
getRegExCaptureGroups
public static List<String> getRegExCaptureGroups(String regex, File file) throws IOException
Retrieves capture groups from a regular expression match inside a file.- Parameters:
regex- a regex containing capture groups, as a Stringfile- the file to search for matching capture groups- Returns:
- An ArrayList of Strings representing each capture group in the regex that was matched
- Throws:
IOException- See Also:
getRegExCaptureGroups(String, String)
-
getRegExCaptureGroups
public static List<String> getRegExCaptureGroups(String regex, String input)
Retrieves capture groups from a regular expression match inside a String.- Parameters:
regex- a regex containing capture groups, as a Stringinput- the string to search for matching capture groups- Returns:
- An ArrayList of Strings representing each capture group in the regex that was matched
-
hasRegExMatch
public static boolean hasRegExMatch(String regex, File file) throws IOException
Evaluates a regex pattern against a file to determine if at least one regex match exists- Parameters:
regex- a regex pattern, as a Stringfile- the file to search for matching substrings- Returns:
- true if there is at least one regex match, otherwise false
- Throws:
IOException
-
getIndent
public static String getIndent(String line, int level)
Infers the indentation style from the given line.- Parameters:
line- the line to infer the indentation style fromlevel- the level of indentation of the line- Returns:
- a single indent in the inferred style
-
-