之前遇到過"/",在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();
}
這樣就可以轉換成功了!