I'm using the following code to get an array with all sub directories from a given path.
String[] subDirs = path.split(File.separator);
I need the array to check if certain folders are at the right place in this path. This looked like a good solution until findBugs complains that File.separator is used as a regular expression. It seems that passing the windows file separator to a function that is building a regex from it is a bad idea because the backslash being an escape character.
How can I split the path in a cross platform way without using File.separator? Or is code like this okay?
String[] subDirs = path.split("/");
split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that. In the above example 'file.
Let's make it independent of the platform. Unlike other programming languages, the bytecode produced by the javac compiler may be run on a variety of Operating Systems. In fact, Java's produced bytecode simply requires the JVM. The operating system has no impact on it.
Whenever you need to literalize an arbitraryString
to be used as a regex pattern, use Pattern.quote
:
From the API:
public static String quote(String s)
Returns a literal pattern
String
for the specifiedString
. This method produces aString
that can be used to create aPattern
that would match the strings
as if it were a literal pattern. Metacharacters or escape sequences in the input sequence will be given no special meaning.Parameters:
s
- The string to be literalized
Returns: A literal string replacement
This means that you can do the following:
String[] subDirs = path.split(Pattern.quote(File.separator));
If you need to literalize an arbitrary replacement String
, use Matcher.quoteReplacement
.
From the API:
public static String quoteReplacement(String s)
Returns a literal replacement
String
for the specifiedString
. This method produces aString
that will work as a literal replacements
in theappendReplacement
method of theMatcher
class. TheString
produced will match the sequence of characters ins
treated as a literal sequence. Slashes ('\'
) and dollar signs ('$'
) will be given no special meaning.Parameters:
s
- The string to be literalized
Returns: A literal string replacement
This quoted replacement String
is also useful in String.replaceFirst
and String.replaceAll
:
Note that backslashes (
\
) and dollar signs ($
) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. UseMatcher.quoteReplacement
to suppress the special meaning of these characters, if desired.
System.out.println(
"O.M.G.".replaceAll(".", "!")
); // prints "!!!!!!"
System.out.println(
"O.M.G.".replaceAll(Pattern.quote("."), "!")
); // prints "O!M!G!"
System.out.println(
"Microsoft software".replaceAll("so", "$0")
); // prints "Microsoft software"
System.out.println(
"Microsoft software".replaceAll("so", Matcher.quoteReplacement("$0"))
); // prints "Micro$0ft $0ftware"
Use path.getParentFile()
repeatedly to get all components of a path.
Discouraged way would be to path.replaceAll("\\", "/").split("/")
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With