MG lolenstine
MG lolenstine

Reputation: 969

How to use File.separator in Windows

Every time I use File.separator in Java code, I get the error because '\' is an escape character in Windows and Java doesn't recognise "quotes".

I tried doing this: String[] split = strData.toString().split(File.pathSeparator);, but it crashes with following error message:

Caused by: java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^

File.pathSeparator = ;
File.separator = \
strData.toString() = C:\Users\server\Desktop\minecraft\plugins\krneki

Upvotes: 1

Views: 652

Answers (1)

Andy Turner
Andy Turner

Reputation: 140326

Since the argument of String.split is a regular expression, you need to quote the separator for it to be treated as a literal:

String[] split = strData.toString().split(Pattern.quote(File.pathSeparator));

Upvotes: 4

Related Questions