java中路径符号“//”,"\"转换

发布时间:2019-06-12 14:00:02发布者:Mr.Zhang阅读(399)

之前遇到过"/",在esclipse中报错,只认识“//”,“\”符号,需要将string字符串“/”,转换成“//”或者“\”怎么转呢?

 

获取字符串是 String path = "/root/data/image";

StringBuilder sb = new StringBuilder();

if (path.contains("\\")) {

  String t = path.replaceAll("\\\\", "/");

  sb.append(t);

}

String fromFile = sb.toString();
System.out.println(fromFile);

 

这样就可以转换成功了!





本文转自博客园,原文地址:https://www.cnblogs.com/glory-xsl/p/11008228.html