I cant print "\".....

String file = "c:\xxx\xxx\xxx.doc";the compiler says illegal use of escape character...how do I solve it?
[151 byte] By [caspar50708a] at [2007-10-1 22:00:14]
# 1
escape that bad boy\\or \\\\ to escape the escape
thestonefoxa at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 2
I am just intending to do some io with a file.so, you mean, in order to locate a file via a String, do something like:"C:\\xxx\\xxx\\xxx.doc"?
caspar50708a at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 3

> I am just intending to do some io with a file.

>

> so, you mean, in order to locate a file via a String,

> do something like:

>

> "C:\\xxx\\xxx\\xxx.doc"?

Not necessarily. For instance, if you're using the File class, then you can do "C:/x/y/z.doc"

and the slashes will be converted for you.

jverda at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 4
I'm pretty sure this also applies to FileReader/Writer/InputStream/OuputStream.But, if you want a backslash in a string literal, then yes, you have to escape it with another backslash.
jverda at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 5
yesif you want to use a \ in your string or anywherethen just sayc:\\myfile\\files\\temp.txtbut windows (and linux)both accept /which doesnt need escpaingc:/myfile/files/temp.txt
thestonefoxa at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 6
but what if I really need to have this string?"C:\xxx\xxx\xxx.doc"?because the server side comunication I am testing to POST only accept that format..any help?
caspar50708a at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 7
>but what if I really need to have this string?Sir,You've already been given the answer to that one
smokedOuta at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 8

You do get that string, but the backslash can be used to create non-printable characters (among others) in string literals and thus needs to escape.

So the string literal "\\" will result in a string with 1 character length and that character beeing \. So even if it doesn't look like it you do indeed create a string that contains c:\xxx\ if your string literal looks like that: "c:\\xxx\\"

JoachimSauera at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 9

The text you had in your first reply (reply 2 of 6) will work in any situation that requires "\". "\" is an escape character that allows you to represent characters that would normally have other meanings as themselves. For example, how do you have a double quote in a literal string? Precede it with "\":String str = "Arm the \"laser beam\", please";

This would put the string Arm the "laser beam", please into str. Now, the escape can also escape itself. That's why you need two. The first \ is the escape character the second one is thus interpreted as a simple \ rather than an escape.

Does that clear things up?

DougDeana at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 10
ahhhh..sorryy..I tested with "C://xxx//xxx.doc"my fault.thanks
caspar50708a at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...
# 11
String str="c:\\testdir\\testfile.txt";System.out.println(str);//will output c:\testdir\testfile.txt
thestonefoxa at 2007-7-13 8:04:23 > top of Java-index,Java Essentials,Java Programming...