如何在Java中转义正斜杠以便在路径中使用它 [英] How to escape forward slash in java so that to use it in path

查看:1170
本文介绍了如何在Java中转义正斜杠以便在路径中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

谢谢

推荐答案

您应该了解File.separator ...,这比\/更安全,因为Linux和Windows使用不同的文件分隔符.毕竟,使用File.separator将使您的程序无论在什么平台上运行都可以运行,这就是JVM的意义所在. -正斜杠将起作用,但是File.separator将使您最终用户更加有信心.

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.

不需要逃脱 ...您应该也请参阅此问题的答案

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

这篇关于如何在Java中转义正斜杠以便在路径中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆