wood burning stoves 2.0*
The moose likes Java in General and the fly likes Illegal escape character error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Illegal escape character error?" Watch "Illegal escape character error?" New topic
Author

Illegal escape character error?

Julia Irving
Greenhorn

Joined: Jun 30, 2003
Posts: 17
I'm trying to open a directory and read its files (the directory is on a different server). I am getting an illegal escape character error on the following file assignment:
java.io.File tempdirec = new java.io.File("\\servername\folder1\folder2");

Any ideas on what's wrong? Thanks in advance.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

The '\' is a special character in Java, used to denote special characters. For example: '\n' is the new line character (ASCII-10), '\r' is the carriage return characte (ASCII-13), '\t' is the tab character (ASCII-9), etc.
So, in your code, you have an '\f' character in your string (at least that's how java interprets it), but '\f' doesn't mean anything to the compiler, so it trows that exception.
To fix this, use the '\\' character, which translates to a single backslash:
java.io.File tempdirec = new java.io.File("\\\\servername\\folder1\\folder2");
Note the four \s at the beginning; each blackslash that you use has to be double backslashed.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Illegal escape character error?