I don't know anything about Zookeeper. But it looks to me as though you're trying to keep a list of strings like "zookeeper", "HellowWorld", "Test/World"
, that you then want to use either to create Zookeeper nodes or to create a pathname in a file system. (I'm assuming that if you're working with a file system, you're going to have a subdirectory Test
and a file or subdirectory World
in the Test
subdirectory. If you're actually trying to create a single file or directory named Test/World
, give up. Both Linux and Windows will fight with you.)
If that's the case, then don't try to represent the "path" as a simple String
that you pass around in your program. Instead, represent it as a String[]
or ArrayList<String>
, and then convert it to a filesystem path name only when you need a filesystem path name. Or, better, define your own class with a getFilesystemPath
method. Converting your list of node names to a pathname String
too early, and then trying to reconstruct the list from the String
later, is a poor approach because you throw away data that you need later (in particular, you're throwing away information about which /
characters are separators and which ones are part of node names).
EDIT: If you also need a single path name for Zookeeper, as you mentioned in another comment, I can't help you since I don't know Zookeeper and haven't found anything in a quick look at the docs. If there is a way to escape the slash for Zookeeper, then I still recommend defining your own class, with a getFilesystemPath
method and a getZookeeperPath
method, since the two methods will probably return different String
s in certain cases. The class would internally keep the names as an array or ArrayList
.