File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Illegal escape character error?" Watch "Illegal escape character error?" New topic

Illegal escape character error?

Julia Irving

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: tempdirec = new"\\servername\folder1\folder2");

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

Joined: Aug 20, 2001
Posts: 1824

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: tempdirec = new"\\\\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.
subject: Illegal escape character error?
It's not a secret anymore!