简体   繁体   English

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

[英]Windows escape sequence issue with file path in java

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 "\\". 系统仅接受“ \\\\”或“ /”,但是如果我从Windows复制文件路径,则使用“ \\”。 how can i solve this issue 我该如何解决这个问题

Use File.seperator in place of "\\". 使用File.seperator代替“ \\”。

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

File.seperator returns "\\" and it is not treated as an escape character. File.seperator返回“ \\”,并且不将其视为转义字符。

If your file test.txt is 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 = \\

\\ is the escape character in Java Strings. \\是Java字符串中的转义字符。 Use \\\\ instead. 使用\\\\代替。

"C:\\\\test" resolves to the String C:\\test "C:\\\\test"解析为字符串C:\\test

You can use \\\\ or / but / is better because it is OS-independent. 您可以使用\\\\/但是/更好,因为它与操作系统无关。

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会将其转换为操作系统的文件分隔符

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

You are not creating a File with the path "C:\\\\test" here. 您不在此处创建路径为“ C:\\\\ test”的文件。 You are creating a File with the path "C:\\test". 您正在创建路径为“ C:\\ test”的文件。 The \\\\-to-\\ conversion happens when you compile the program - by the time your program is running, the double backslashes are gone. \\\\-to \\\\转换是在编译程序时发生的-程序运行时,双反斜杠消失了。

The same for String - String s = "C:\\\\test"; 字符串相同- String s = "C:\\\\test"; does not create a string with two backslashes, only one. 不会创建带有两个反斜杠(只有一个)的字符串。

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 \\t in a string it inserts a tab, for example. 例如,当您在字符串中键入\\t ,它将插入一个制表符。 If you want to insert a backslash, then t, you type \\\\t . 如果要插入反斜杠,请输入\\\\t

您可以在Windows XP以后的路径中使用'/'(与Linux中一样),因此请忽略\\

Use java.nio.file.Path instead 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM