如何在java中转换默认文件路径?
我正在尝试从文件中获取文件路径,并将文件路径保存在文件中,然后将其回调,以便在邮件中作为附件发送如何在java中转换默认文件路径?,java,file,Java,File,我正在尝试从文件中获取文件路径,并将文件路径保存在文件中,然后将其回调,以便在邮件中作为附件发送 File file = fc.getSelectedFile(); String source = file.getAbsolutePath(); println(source); 但输出将是这样的 C:\Documents and Settings\TheFile.txt 但我正在寻找一种方法来获取源代码 C:\\Documents and Settings\\TheFile.txt 有什么
File file = fc.getSelectedFile();
String source = file.getAbsolutePath();
println(source);
但输出将是这样的
C:\Documents and Settings\TheFile.txt
但我正在寻找一种方法来获取源代码
C:\\Documents and Settings\\TheFile.txt
有什么方法可以转换吗?谢谢你的帮助 这里有一个方法:
String source = file.getAbsolutePath();
String sourceConverted = source.replaceAll("\\", "\\\\");
这里有一个方法:
String source = file.getAbsolutePath();
String sourceConverted = source.replaceAll("\\", "\\\\");
更简单的方法应该是编写一个
String getPath(){
File file = fc.getSelectedFile();
String source = file.getAbsolutePath();
source .replace("\\","\\\\");
return source;
}
更简单的方法应该是编写一个
String getPath(){
File file = fc.getSelectedFile();
String source = file.getAbsolutePath();
source .replace("\\","\\\\");
return source;
}
试试这个:
File f = [your file]
String filename = f.getAbsolutePath().replaceAll("\\", "\\\\");
将“文件名”写回文件,单斜杠将替换为双斜杠。试试这个:
File f = [your file]
String filename = f.getAbsolutePath().replaceAll("\\", "\\\\");
将“文件名”写回文件,单斜杠将替换为双斜杠。我觉得两个斜杠看起来都一样。如果需要双反斜杠,可以使用
stirng.replace(“\\”,“\\\”)代码>路径正确。也许你想把它复制并粘贴到一个Java字符串中,然后你需要引用斜杠,在我看来两者都是一样的。如果需要双反斜杠,可以使用stirng.replace(“\\”,“\\\”)代码>路径正确。也许您想将其复制并粘贴到Java字符串中,然后需要引用斜杠。