How to escape forward slash in java so that to use it in path

apache-zookeeper, hadoop, java

Solution

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";

Problem

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

Original source

Related problems