Java如何将"\"转换成"\\"

1         FileChooser fileChooser = new FileChooser();
2         fileChooser.setTitle("选择你要查看的文件");
3         File file = fileChooser.showOpenDialog(null);
4         String path = file.getPath().toString();
5         String str = path.replaceAll("\\\\","\\\\\\\\");

      当我去想要通过FileChooser去打开一个图片,并且通过Imageview显示的时候,发现它的   file.getPath();   不能直接使用,需要转换成这样的格式才能使用file:D:\\*.png   ,file.getPath()获取到的路径是 D:\*.png,这明显是不符合要求的。因此通过String 里面的replaceAll这个方法把 "\" 替换成 "\\"  ,但是发现不能直接使用。后来百度知道,一个   "\"   需要用四个   "\"   才可以正常替换。

猜你喜欢

转载自www.cnblogs.com/jdr-gbl/p/12037800.html