3

Could you please help me find a solution for this problem?

I am trying to make a java GUI with Swing components

The interface is supposed to accept a Windows path to a certain file and trigger a set of function on the file after pressing the submit button

Right now I have created the component to get the user input as:

JTextField introducedPath1 = new JTextField(50);

I tried to change the default Windows path obtained by copy pasting the path from explorer into an accepted File path:

File file;
String makeCanonicalPath=introducedPath1.getText().toString();
            makeCanonicalPath=makeCanonicalPath.replaceAll("\\", "/");
            file = new File(makeCanonicalPath);

But I keep getting this error:

Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1

\
 ^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)

Which I assume it is caused by the default path in windows(ex:L:\practice\test) getting its '\' interpreted as escape sequences.

Any help or suggestion will be appreciated

  • If you use drag and drop, then you can get a FileList and get the actual File object. No need to monkey with Strings. For example please check out my code here: [drag and drop example](http://stackoverflow.com/a/13597312/522444) – Hovercraft Full Of Eels Jan 03 '13 at 19:00

1 Answers1

3

With replaceAll, you need to use java escaping for regular expressions, so

makeCanonicalPath.replaceAll("\\\\", "/");

Because it is not a text search-replace, but a regex one.

If you want text-based search-replace, use apache commons string replace, for example.

Also, one alternative is to use replace() - http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replace(char, char) instead of replaceAll - it would replace all occurrences without using regex, and since you only have one char to replace, it'd work.

eis
  • 51,991
  • 13
  • 150
  • 199
  • Why is it a better ideea to use apache commons string replace, when makeCanonicalPath.replaceAll("\\\\", "/"); worked? Is apache commons string replace better practice? – user1944955 Jan 03 '13 at 19:04
  • @user1944955 it is better idea to use some kind of text-replacement mechanism if you want to do text-based replacements. If you really want regular expressions, then sure, use that. But it didn't seem like you do? – eis Jan 03 '13 at 19:06
  • with replace() my solution will look like: String makeCanonicalPath1 = introducedPath1.getText().replace("\\","/").toString(); file = new File(makeCanonicalPath1); Since File requests String as input. So I am not really sure that it is a better soulution. Is it? – user1944955 Jan 03 '13 at 19:17
  • @user1944955 `String.replaceAll()` treats the parameter as `regex` and it treat that parameter completely different (specially for backslashes "\" and dollar signs "$"). [See the Java Doc for replaceAll](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)) – Smit Jan 03 '13 at 19:23
  • 1
    Just as another option, you could also use a Swing FileChooser and allow the user to navigate to the file he wants that way. This does away with string and regex expressions entirely. – arcy Jan 03 '13 at 20:37
  • @rcook hear, hear, I'd put that as an answer too :) – eis Jan 04 '13 at 08:16