java java中如何处理Windows和UNIX上不同的file.separator
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11241762/
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
How to deal with different file.separator on Windows and UNIX in java
提问by Paul Taylor
I'm reviewing my code
我正在我的代码
My Java application runs on Windows and Unix using Java 6 and manipulates files. I know I can use the file.separator to get the file separator in a filesystem independent way but if I ever use specify windows file separator char directly because is the '\' which is also the Java escape character I can't do manipulation of the file path. So in my code I've always stored the filepath in unix notation by replacing \' with '/' ,and these paths are stored in a database so I thought there was an escaping problem there as well. So I was under the illusion that trying to use file.separator would also fail because it would return '\' rather than '\' but now realise only need \ if I actually explicity specify it myself in quotes.
我的 Java 应用程序使用 Java 6 在 Windows 和 Unix 上运行并操作文件。我知道我可以使用 file.separator 以独立于文件系统的方式获取文件分隔符,但是如果我直接使用指定 windows 文件分隔符字符,因为 '\' 也是 Java 转义字符,我无法操作文件路径。因此,在我的代码中,我总是通过将 \' 替换为 '/' 来以 unix 符号存储文件路径,并且这些路径存储在数据库中,所以我认为那里也存在转义问题。所以我幻想着尝试使用 file.separator 也会失败,因为它会返回 '\' 而不是 '\' 但现在意识到只需要 \ 如果我自己在引号中明确指定它。
Now I'm thinking this is all unnecessary and as long as I always use file.separator I don't need to do this conversion, am I right ?
现在我认为这一切都是不必要的,只要我总是使用 file.separator 我就不需要进行这种转换,对吗?
EDIT: Found a case where it seems to be a problem
编辑:发现一个似乎有问题的案例
"C:\Fred\test1.txt".split("\\");
"C:\Fred\test1.txt".split(System.getProperty("file.separator"));
if I want to split the string by \ I have double it up because \ has special meaning in a regular expression, so the line using file.separator fails with
如果我想用 \ 分割字符串,我将它加倍,因为 \ 在正则表达式中具有特殊含义,因此使用 file.separator 的行失败
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.error(Pattern.java:1713)
at java.util.regex.Pattern.compile(Pattern.java:1466)
at java.util.regex.Pattern.<init>(Pattern.java:1133)
at java.util.regex.Pattern.compile(Pattern.java:823)
but on unix no such requirement the corresponding '/' should not be escaped
但在 unix 上没有这样的要求,相应的“/”不应该被转义
EDIT 2: This question has been asked before Splitting filenames using system file separator symbolthe solution boils down to using Pattern.quote() around the input or trying to use file methods rather regular expressions. Both a little uneccessary I would prefer it if files could be viewed ins a system independent way, I not that Path in Java 7 has the same problem.
编辑 2:在使用系统文件分隔符拆分文件名之前已经问过这个问题,解决方案归结为在输入周围使用 Pattern.quote() 或尝试使用文件方法而不是正则表达式。如果可以以独立于系统的方式查看文件,我会更喜欢它,我不认为 Java 7 中的 Path 有同样的问题。
EDIT 3: Also seeing a problem with reading/writing from db, created a separate question on that Storing Windows Path in Database and retrieving with Hibernate using java
编辑 3:还看到从 db 读取/写入的问题,创建了一个单独的问题,即在数据库中存储 Windows 路径并使用 Java 使用 Hibernate 检索
回答by kjp
Yes, you are right - if you use File.Seperator you dont need any additional escaping because in Windows it is already escaped for you. From the java documentation.
是的,你是对的 - 如果你使用 File.Seperator 你不需要任何额外的转义,因为在 Windows 中它已经为你转义了。来自 java文档。
separatorChar
public static final char separatorChar
The system-dependent default name-separator character.
This field is initialized to contain the first character of the value of the system property file.separator.
On UNIX systems the value of this field is '/';
on Microsoft Windows systems it is '\'.