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

"本文探讨了如何在Java中正确处理字符串路径,以避免在创建Zookeeper节点时出现分离问题。重点介绍了使用`File.separator`确保路径兼容不同平台,并给出了示例代码:String fileP = "Test" + File.separator + "World",以生成单个节点。"
摘要由CSDN通过智能技术生成

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

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值