向路径添加双斜杠

Add double slash to a path

方法 System.getProperty("user.dir") returns 是这样的:

C:\Users\myuser\path

但是在我的程序中我需要有双斜线,像这样:

C:\\Users\\myuser\\path

有添加这些双斜杠的简单方法吗?

使用string.replace函数。

System.out.println("C:\Users\myuser\path".replace("\", "\\"));

System.out.println(System.getProperty("user.dir").replace("\", "\\"));

更新:

String s = "C:\Users\myuser\path";
System.out.println(s.replace("\", "\\\"));

输出:

C:\\Users\\myuser\\path