How to split a path platform independent?

Posted by Janusz on Stack Overflow See other posts from Stack Overflow or by Janusz
Published on 2009-07-08T18:40:24Z Indexed on 2010/04/29 9:57 UTC
Read the original article Hit count: 396

Filed under:
|
|
|

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("/");

© Stack Overflow or respective owner

Related posts about java

Related posts about files