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


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

 

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

因为"\"会报错,所以把他写进txt文本读出来。

List<String> lines;
try {
  lines = Files.readAllLines(Paths.get("C:\\Users\\Shinelon\\Desktop\\test.txt"), StandardCharsets.UTF_8);

  StringBuilder sb = new StringBuilder();
  for (String line : lines) {
    if (line.contains("\\")) {
      String c = line.replaceAll("\\\\", "/");
      sb.append(c);
    }
  }
  String fromFile = sb.toString();
  System.out.println(fromFile);

} catch (IOException e) {
  e.printStackTrace();
}

 

这样就可以转换成功了!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM