Reputation: 745
E.g. When I use
C:\\a.txt
It works fine, but when I use
C:\a.txt
It does not.
Anyone explain the difference between the two except from saying one works and the other doesn't.
Thanks
Upvotes: 1
Views: 103
Reputation: 1312
Its also good use File.separator to get the separator '/' or '\' (if there are others then I dont know about them) necessary for the OS that is running the JVM.
Upvotes: 0
Reputation: 44740
Just a quote to remember : Backslash is NOT a path separator!
Upvotes: 0
Reputation: 42040
A single \
means an escape sequence which has a specific meaning for the compiler. \\
basicly escapes the escape sequence. So, if you type C:\a
the compiler treats \a
as a escape sequence, something that you did not intend.
http://docs.oracle.com/javase/tutorial/java/data/characters.html
Upvotes: 2
Reputation: 4284
In java the \
symbol is reserved to use with other char to make special symbols, for example,
\n is new line
\t is a tab
So if you use one \
like in C:\a.txt
it will think that \a
is an special char, while using \\
will be transformed in one \
Upvotes: 2
Reputation: 62439
\
is a special character used for escaping other special characters. As such, if a single \
is present it is interpreted as a special character in the string, but since there is nothing to escape, it's considered a "wrong usage".
Therefore, the \
has to be escaped with a second \
to give it its literal meaning.
Upvotes: 6