问题描述
我需要使用 windows 文件路径对文件进行一些操作,但我收到了无效的转义序列错误.
I need to use windows file path to do some operation on files but i am getting invalid escape sequence error.
File f = new File("C: est");
系统只接受\"或/",但如果我从windows复制文件路径,它是".我该如何解决这个问题
the system accepts only " \ " or "/" but if I copy file path from windows it is with "".how can i solve this issue
推荐答案
使用 File.seperator 代替".
Use File.seperator in place of "".
File f = new File("C:"+File.seperator+"test");
File.seperator 返回"并且它不被视为转义字符.
File.seperator returns "" and it is not treated as an escape character.
如果您的文件 test.txt
保存在文件夹 D:/MyFloder/MyPrograms 中,您可以执行以下操作
If your file test.txt
is saved in folder D:/MyFloder/MyPrograms you can do something like this
File f = new File("D:"+File.seperator+"MyFloder"+File.seperator+"MyPrograms"+File.seperator+"test.txt");
编辑
您无需担心操作系统
对于 Unix:File.separator =/
对于 Windows:File.separator =
For Windows : File.separator =
这篇关于java中文件路径的Windows转义序列问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!