getAbsolutePath()
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sender_jones
Posted On:   Tuesday, December 11, 2007 02:20 AM

Can anybody tell me what is wrong with this?

			
File f = new File("my_file.htm");
System.out.println(f.getAbsolutePath().replaceAll("\", "/"));

It compiles OK, but when executed in Windows xp, hit an error:
			
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected in
ternal error near index 1

^

Re: getAbsolutePath()

Posted By:   Robert_Lybarger  
Posted On:   Tuesday, December 11, 2007 08:45 AM

To elaborate slightly more, the "" is meaningful to a regex as an escape character. A literal version of a special character must be escaped, in this case as "\". But when you then write it in Java code as a String literal, the "" is meaningful there (also as an escape character) so they must each be escaped again to "\\".

Re: getAbsolutePath()

Posted By:   Anonymous  
Posted On:   Tuesday, December 11, 2007 03:49 AM

you can set

System.out.println(f.getAbsolutePath().replaceAll("\\", "/"));

because '\' is special character on regular expression
About | Sitemap | Contact