Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

I am trying to read a file into my Java program using java.util.Scanner and I get the above message when I enter the code below (I am new to java) - can anyone help? (I looked at a similar message someone got with their own code, but it was too complex for me to use in my example!). I have Windows 7.

BufferedReader job = new BufferedReader
               (new FileReader("\My Documents\JOBS\newfile.txt"));
like image 337
Kariuki Avatar asked Feb 25 '12 15:02

Kariuki


People also ask

Which one is an invalid escape sequence?

Placing a '\' (backslash) in front of the character in the regular expression generates an 'Invalid escape sequence' compilation error. This only occurs when the regular expression is used in the text of the script.

What is a valid escape sequence?

Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences.


2 Answers

You need to escape the "\" in the file path.

BufferedReader job = new BufferedReader 
               (new FileReader("\\My Documents\\JOBS\\newfile.txt"));
like image 108
GeoGriffin Avatar answered Sep 22 '22 05:09

GeoGriffin


\ is an escape character, use \\

like image 34
Karoly Horvath Avatar answered Sep 22 '22 05:09

Karoly Horvath