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

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

问题描述

我试图转义字符串中的正斜杠,它可以在使用 Java 的路径中使用.
例如:String::"Test/World"
现在我想使用上面的字符串路径.同时我必须确保 "Test/World" 会在路径中出现.对不起,如果它重复,但我找不到任何令人满意的解决方案.我的目的是使用上面的字符串在 Zookeeper 中创建节点.
示例:
如果我使用以下字符串在 Zokkeeper 中创建节点,那么我应该将测试/世界"作为单个节点而不是分开的.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天全站免登陆