如何在java中转义正斜杠以便在路径中使用它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24643025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:39:27  来源:igfitidea点击:

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

javahadoopapache-zookeeper

提问by Frank

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

我试图转义字符串中的正斜杠,它可以在使用 Java 的路径中使用。
例如:String:: "Test/World"
现在我想使用上面的字符串"Test/World"路径。同时我必须确保它会在路径中出现。对不起,如果它重复,但我找不到任何令人满意的解决方案。我的目的是使用上面的字符串在 Zookeeper 中创建节点。
示例:
如果我使用以下字符串在 Zokkeeper 中创建节点,那么我应该将“Test/World”作为单个节点而不是分开的。Zookeeper 接受“/”作为路径分隔符,在某些情况下我不需要。 /zookeeper/HellowWorld/Test/World

Thanks

谢谢

回答by rozi

In order to escape a character in Java use "\" for example:

为了在 Java 中转义字符,请使用“\”,例如:

String strPath = "directory\file.txt".

I believe you do not need to escape forward slashes such as: "/"

我相信您不需要转义正斜杠,例如:“/”

回答by J-Dizzle

You should know about File.separator... This is safer than \or /because Linux and Windows use different file separators. Using File.separatorwill 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.separatorwill make you end users more confident that it will.

你应该知道File.separator... 这比Linux 和 Windows 使用不同的文件分隔符更安全,\或者/因为 Linux 和 Windows 使用不同的文件分隔符。使用File.separator将使您的程序无论运行在哪个平台上都可以运行,毕竟这是 JVM 的重点。-- 正斜杠会起作用,但是,File.separator会让您的最终用户更加确信它会起作用。

And you don't need to escape"/... you should also see the answer to this question

而且你不需要逃避"/……你应该看到这个问题的答案

String fileP = "Test" + File.separator + "World";

回答by ajb

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 orto 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 Testand a file or subdirectory Worldin the Testsubdirectory. 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.)

我对 Zookeeper 一无所知。但在我看来,好像您试图保留一个字符串列表,例如"zookeeper", "HellowWorld", "Test/World",然后您想使用它来创建 Zookeeper 节点在文件系统中创建路径名。(我假设如果您使用的是文件系统,您将有一个子目录Test和一个文件或子目录World中的Test子目录。如果您实际上是在尝试创建一个名为 的文件或目录Test/World,请放弃.Linux 和 Windows 都会和你战斗。)

If that's the case, then don't try to represent the "path" as a simple Stringthat 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 getFilesystemPathmethod. Converting your list of node names to a pathname Stringtoo early, and then trying to reconstruct the list from the Stringlater, 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).

如果是这种情况,那么不要试图将“路径”表示为String您在程序中传递的简单形式。相反,将其表示为 aString[]ArrayList<String>,然后仅在需要文件系统路径名时才将其转换为文件系统路径名。或者,更好的是,用一个getFilesystemPath方法定义你自己的类。String过早将节点名称列表转换为路径名,然后尝试从String后者重建列表,这是一种糟糕的方法,因为您丢弃了以后需要的数据(特别是,您丢弃了有关哪些/字符是分隔符以及哪些是节点名称的一部分)。

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 getFilesystemPathmethod and a getZookeeperPathmethod, since the two methods will probably return different Strings in certain cases. The class would internally keep the names as an array or ArrayList.

编辑:如果您还需要 Zookeeper 的单个路径名,正如您在另一条评论中提到的,我无法帮助您,因为我不了解 Zookeeper 并且在快速查看文档时没有找到任何内容。如果Zookeeper有办法转义斜杠,那么我还是建议自己定义一个类,一个getFilesystemPath方法一个getZookeeperPath方法,因为String在某些情况下这两个方法可能会返回不同的s。该类将在内部将名称保留为数组或ArrayList.

回答by Alexandre Santos

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

让我重新表述你的问题。您正在尝试在 zookeeper 中创建一个节点,它应该是 /zookeeper/HelloWorld/NodeName。但是最后一部分“NodeName”实际上是“Test/World”,您正在寻找转义“/”的方法,因此节点名称可以是“Test/World”。

I don't think it would work escaping the char, unless you tried with unicode.

我不认为它会转义字符,除非您尝试使用 unicode。

Try \u002F which is the equivalent for /.

尝试 \u002F 相当于 /。

回答by jka

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.

我们正在尝试解决完全相同的问题(在 zookeeper 中使用文件系统路径作为节点名称),我们还没有找到如何在节点名称中包含 '/' 的方法。

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.

解决方案是用某些字符替换“/”,该字符不能出现在您的节点名称中。对于 '/' 或 '\0' 的路径,在这种情况下对我们没有帮助。

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.

其他可能性是用节点名称中允许的字符串替换 '/',例如 "Test/World" -> "Test%@World", "Test%World" -> "Test%%World" 并添加 escaping/de - 转义保存和加载。

If there is any more straightforward way, I'd love to hear it.

如果有更直接的方法,我很乐意听到。