This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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.
 
 
subject: Illegal escape character error?
 
Similar Threads
Help needed in file reading.
SortNames FileNotFoundException???
Regular expression replacing [ ]
unicode
Query regarding String.split