Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illegal escape character error?

 
Julia Irving
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic