java - Error: illegal escape character - when trying to create a path -


i'm new java , trying learn how create path. below code wrote:

import java.io.ioexception; import java.nio.file.paths; import java.nio.file.path;  public class copybytes {     public static void main(string[] args) throws ioexception {          path p1 = paths.get("c:\users\justin\documents\netbeansprojects\javaapplication\xanadu1.txt");     } } 

however, when run code, ide output error:

illegal escape character. 

why happening?

certain characters have special meaning when used in string in java (and many other languages).

a backward slash \ can used escape character. valid escape characters in java \t tabs , \n newlines.

hence if use single \. compiler assume trying create escape sequence for:

\u, \j, \d, \n, \x   

and these escape sequences not exist, hence giving error.


if using \ have escape \\.

but if use / forward slash, don't have to.

so can have path this:

"c:\\users\\justin\\documents\\netbeansprojects\\javaapplication\\xanadu1.txt" 

or this:

"c:/users/justin/documents/netbeansprojects/javaapplication/xanadu1.txt" 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -