Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a path platform independent?

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("/"); 
like image 479
Janusz Avatar asked Jul 08 '09 18:07

Janusz


People also ask

How do I split a folder in path?

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.

Why is Java path independent?

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.


2 Answers

Literalizing pattern strings

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 specified String. This method produces a String that can be used to create a Pattern that would match the string s 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));

Literalizing replacement strings

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 specified String. This method produces a String that will work as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s 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. Use Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.


Examples

    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"
like image 128
polygenelubricants Avatar answered Sep 22 '22 19:09

polygenelubricants


Use path.getParentFile() repeatedly to get all components of a path.

Discouraged way would be to path.replaceAll("\\", "/").split("/").

like image 45
akarnokd Avatar answered Sep 18 '22 19:09

akarnokd