How to escape forward slash in java so that to use it in path
Solution 1
You should know about File.separator
... This is safer than \
or /
because Linux and Windows use different file separators. Using File.separator
will make your program run regardless of the platform it is being run on, after all, that is the point of the JVM. -- forward slash will work, however, File.separator
will make you end users more confident that it will.
And you don't need to escape "/
... you should also see the answer to this question
String fileP = "Test" + File.separator + "World";
Solution 2
In order to escape a character in Java use "\" for example:
String strPath = "directory\\file.txt".
I believe you do not need to escape forward slashes such as: "/"
Solution 3
We are trying to solve exactly the same problem (using filesystem path as node name in zookeeper) a we haven't found a way how to have '/' in node name.
Solution would be either to replace '/' with some character, that cannot appear in your node name. For paths that would be '/' or '\0', which wont help us in this case.
Other possibility is to replace '/' with string of characters allowed in node name, e.g. "Test/World" -> "Test%@World", "Test%World" -> "Test%%World" and add escaping/de-escaping to saving and loading.
If there is any more straightforward way, I'd love to hear it.
Solution 4
Let me rephrase your question. You are trying to create a node in zookeeper and it should be /zookeeper/HelloWorld/NodeName. But the last part "NodeName" is actually "Test/World", and you are looking for ways to escape "/" so the node name can be "Test/World".
I don't think it would work escaping the char, unless you tried with unicode.
Try \u002F which is the equivalent for /.
Related videos on Youtube

Frank
Updated on July 09, 2022Comments
-
Frank 6 months
I am trying to escape forward slash in String which can be used in path using Java.
For example:String:: "Test/World"
Now I want to use above string path.At the same time I have to make sure that"Test/World"
will come as it is in path. Sorry if its duplicate but I couldn't find any satisfactory solution for this. My purpose is to use above string to create nodes in Zookeeper.
Example:
If I use following string to create node in Zokkeeper then I should get "Test/World" as a single node not separate. Zookeeper accepts "/" as path separator which in some cases I dont require./zookeeper/HellowWorld/Test/World
Thanks
-
sushain97 over 8 yearspossible duplicate of Java - Forward Slash Escape Character
-
Frank over 8 yearsHi Sushain. I am not sure how it is duplicate.
-
-
sushain97 over 8 yearsYour belief is correct. It would be nice to include a source though. :)
-
Frank over 8 yearsYes I need to use this path in Zookeeper as well as normal file system.
-
Frank over 8 yearsUsing File.Separator also Zookeeper created Test and World as two separate entities(As part of Path). What I am trying to find is how to escape it so that I will get node as "Test/World"
-
J-Dizzle over 8 yearsHave you tried that? I believe File.Separator will return the string for you... Are you trying to iterate through the elements of the path and concatenate to a single string?
-
Frank over 8 yearsYes I have to form it as single String other wise it will not be accepted as path in Zookeeper. Yes I tried your approach.
-
J-Dizzle over 8 yearscheck the second example here, see if getResource() can help you qualitybrain.com/?p=3
-
Frank over 8 yearsI tries but at zookeeper level it takes it as "/" and forms separate node.
-
Alexandre Santos over 8 yearsIs there a way to rename nodes once created? Maybe the / could work for renaming a node.
-
P.Ellis 11 monthsThis sorted my situation. I wanted to write "n/a" and ended up with "\"n\u002Fa\"". Thank you.