java中文件路径的Windows转义序列问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20324962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:05:30  来源:igfitidea点击:

Windows escape sequence issue with file path in java

javaeclipsefile-ioio

提问by user2614607

I need to use windows file path to do some operation on files but i am getting invalid escape sequence error.

我需要使用 Windows 文件路径对文件进行一些操作,但我收到无效的转义序列错误。

File f = new File("C:\test");

the system accepts only " \\ " or "/" but if I copy file path from windows it is with "\". how can i solve this issue

系统只接受“\\”或“/”,但如果我从windows复制文件路径,它是“\”。我该如何解决这个问题

回答by Dragondraikk

\is the escape character in Java Strings. Use \\instead.

\是 Java 字符串中的转义字符。使用\\来代替。

"C:\\test"resolves to the String C:\test

"C:\\test"解析为字符串 C:\test

回答by Little Child

You can use \\or /but /is better because it is OS-independent.

您可以使用\\or /but/更好,因为它与操作系统无关。

Replace the single backslash in the path with a double backslash or a single forward slash to solve your issue.

将路径中的单个反斜杠替换为双反斜杠或单个正斜杠以解决您的问题。

Internally, Java will convert it to the file seperator of the OS

在内部,Java 会将其转换为操作系统的文件分隔符

回答by Prasad

Use File.seperator in place of "\".

使用 File.separator 代替“\”。

File f = new File("C:"+File.seperator+"test");

File.seperator returns "\" and it is not treated as an escapecharacter.

File.seperator 返回“\”,它不被视为转义字符。

If your file test.txtis saved in folder D:/MyFloder/MyPrograms you can do something like this

如果您的文件test.txt保存在文件夹 D:/MyFloder/MyPrograms 中,您可以执行以下操作

File f = new File("D:"+File.seperator+"MyFloder"+File.seperator+"MyPrograms"+File.seperator+"test.txt");

EDIT

编辑

You don't need to worry about OS

您无需担心操作系统

For Unix : File.separator = /

对于 Unix : File.separator = /

For Windows : File.separator = \

对于 Windows: File.separator = \

回答by Fr0z3n7

Use java.nio.file.Pathinstead of java.io, you'll not have problem with escape sequence character :

使用java.nio.file.Path而不是java.io,你不会有转义序列字符的问题:

import java.nio.file.Path;
import java.nio.file.Paths;
    Path path = Paths.get("C:\test");

回答by user253751

File f = new File("C:\\test");is correct.

File f = new File("C:\\test");是正确的。

You are not creating a File with the path "C:\\test" here. You are creating a File with the path "C:\test". The \\-to-\ conversion happens when you compile the program - by the time your program is running, the double backslashes are gone.

您不是在此处创建路径为“C:\\test”的文件。您正在创建一个路径为“C:\test”的文件。\\-to-\ 转换发生在编译程序时 - 当程序运行时,双反斜杠消失了。

The same for String - String s = "C:\\test";does not create a string with two backslashes, only one.

StringString s = "C:\\test";也一样 -不会创建一个带有两个反斜杠的字符串,只有一个。

You can think of it this way: the string does not actually have two backslashes, but you have to write it that way to put it in your code.

你可以这样想:字符串实际上没有两个反斜杠,但你必须这样写才能把它放在你的代码中。

You might be wondering why that is - it's because backslashes are used to insert special characters in strings. When you type \tin a string it inserts a tab, for example. If you want to insert a backslash, then t, you type \\t.

您可能想知道这是为什么 - 这是因为反斜杠用于在字符串中插入特殊字符。例如,当您输入\t字符串时,它会插入一个制表符。如果要插入反斜杠,则键入 t,然后键入\\t

回答by Yura

you can use '/' (as in Linux) in paths since Windows XP, so forget about \

自 Windows XP 以来,您可以在路径中使用“/”(如在 Linux 中),所以忘记\