tags:

views:

916

answers:

6

hi everyone:

As we all know,we can use

string aa=@"E:\dev_workspace1\AccessCore\WebRoot\DataFile"

in c# in order not to double the '\'.

But how to do in java?

+18  A: 

Unfortunately, there is no full-string escape operator in Java. You need to write the code as:

String aa = "E:\\dev_workspace1\\AccessCore\\WebRoot\\DataFile";
notnoop
+11  A: 

There is no whole string escape operator but, if it's for file access, you can use a forward slash:

String aa="E:/dev_workspace1/AccessCore/WebRoot/DataFile";

Windows allows both forward and backward slashes as a path separator. It won't work if you pass the path to an external program that mangles with it and fails, but that's pretty rare.

Vinko Vrsalovic
Is it actually Java that's being clever? I thought this was a consequence of Windows permitting / as a file separator as well as \.
Simon Nickerson
No idea - I always thought it was Java but it's possible it might be the Windows API calls themselves. Adjusted just in case.
paxdiablo
Heh, I love how people generously work for incresing my rep! Thanks Pax for improving the answer! :-)
Vinko Vrsalovic
Yes, that's windows allowing / as a path seperator, not Java
nos
Java has only the / as file-separator. Under windows additionally the \ is allowed. So the / is the correct way under Java, \ is platform-dependant.
Mnementh
An abstract path has 2 components: 1. An optional system-dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or "\\" for a Microsoft Windows UNC pathname, and 2. A sequence of zero or more string names. Each name in an abstract pathname except for the last denotes a directory; the last name may denote either a directory or a file. The empty abstract pathname has no prefix and an empty name sequence. The conversion of a pathname string to or from an abstract pathname is inherently system-dependent. http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
Vinko Vrsalovic
While \ is platform-dependant, E: is more platform dependant. And, as the spec says, Java does use both separators depending on the platform
Vinko Vrsalovic
+4  A: 

Might not be a direct answer to your question, but I feel this should be pointed out:

There's a system-dependent default name-separator character.

Flo
So there is, but as Vinko pointed out, you don't really need to bother with that property; forward slashes work just fine.
Alan Moore
+3  A: 

The really system-independent way is to do this:

String aa = "E:/dev_workspace1/AccessCore/WebRoot/DataFile";
String output = aa.replace('/', File.separatorChar);

It will give you "E:\dev_workspace1\AccessCore\WebRoot\DataFile" on Windows and "E:/dev_workspace1/AccessCore/WebRoot/DataFile" just about everywhere else.

Robert Petermeier
Why the replacing? The '/' works always on Java and is converted automatically to the system-dependant separator-char.
Mnementh
Because then it is really, really, really safe to use this string. You can even pass it as parameter to let's say an obscure DOS-era command-line program on windows or feed it to really ancient WinAPI calls or whatever that can't handle slashes as separator.
Robert Petermeier
Wrap you replace is a if(File.seperatorChar!='/') as an optimization for unix platforms.
KitsuneYMG
A: 

If you write a path, you should use the '/' as path-separator under Java. The '/' is the official path-separator under Java and will be converted to the appropriate separator for the platform (\ under windows, / under unix). The rest of the string is unchanged if passed to the system, so the '\' also works under windows. But the correct way to represent this path is "E:/dev_workspace1/AccessCore/WebRoot/DataFile".

If you want to represent a '\' in a Java-String you have to escape it with another one: "This String contains a \".

Mnementh
A: 

@rob: if you specify "e:" at beginning of path it's not system-independent. ;-)

ignasi35