-
Escape Sequence
Hi,
I want to add extra '\' to file path
eg
User selects directory like C:\ABC\XYZ say, through Dialog Box
But I want to have the path as C:\\ABC\\XYZ
so that I can use the file path as shown below
new FileInputStream("C:\\ABC\\XYZ\\somefile")
How do I add the extra '\' ??
-
Why don't you use JFileChooser, directly?
-
I'm curious as to why you want to use two backslashes like that, but to answer your question it's as simple as using the escape sequence twice in succession. Like so:
-
First, Masijade made a *really* good suggestion.
Second, you only escape characters in a String *literal*, for example, "This is a String literal."
The escape character is backslash. If you want a backslash in your String literal, use two backslashes. "This is a literal with a \\." comes out
This is a literal with a \.
In your example, the user already entered the String, so there is no need to escape any characters; escape is for literals only.
In addition, Java allows slashes for Windows file paths. Use slashes unless you absolutely have to use backslashes; file paths don't need them.
-
Firstly.
you can always do this manually
Secondly.
you may analyze the string by code to get any "\" and add another "\" to it )
Thirdly.
you can use JFileChooser which gets an absolute file path )
ANd remember that Java can read or write files starting right from it's mother folder ) if you write code like a:
new FileInputStream("MyApplicationFolder/MyFile.txt"); that won't be mistake )
But maybe you don't want GUI for your program. Am I right?