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

前端 未结 3 388
清酒与你
清酒与你 2020-12-06 16:48

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 look

相关标签:
3条回答
  • 2020-12-06 17:06

    If you're using eclipse, there's a setting that inserts escape chars automatically when pasting:

    Window -> Preferences -> Java -> Editor -> Typing -> In String Literals -> Escape text when pasting into a string literal

    Then, when something like D:\Env\Images\image1.png is in your clipboard and you paste it into eclipse, it'll automatically look like this: D:\\Env\\Images\\image1.png

    0 讨论(0)
  • 2020-12-06 17:07

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

    BufferedReader job = new BufferedReader 
                   (new FileReader("\\My Documents\\JOBS\\newfile.txt"));
    
    0 讨论(0)
  • 2020-12-06 17:08

    \ is an escape character, use \\

    0 讨论(0)
提交回复
热议问题