[英]How to force Java to use Unix file separators on Windows?
I have a simple code 我有一个简单的代码
String staticDir = f.getCanonicalPath() + "/src/main/webapp/static/";
On Windows it will return for me "C:\\Temp/src/main/webapp/static/". 在Windows上,它将为我返回“ C:\\ Temp / src / main / webapp / static /”。
How to force Java to use "/" instead of "\\"? 如何强制Java使用“ /”代替“ \\”?
I have tried 我努力了
System.setProperty("file.separator", "/");
String staticDir = f.getCanonicalPath() + "/src/main/webapp/static/";
but it doesn't solved issue for me. 但这并没有解决我的问题。
Thank you! 谢谢!
Just replace the path separator with the expected one: 只需将路径分隔符替换为预期的分隔符即可:
String dir = f.getCanonicalPath().replace(System.getProperty("file.separator"), "/")
+ "/src/main/webapp/static/";
If you can, favor Java 7+'s java.nio.file.Path
. 如果可以,请支持Java java.nio.file.Path
。 There, the notion of path separator disappears until you call toString()
. 在那里,路径分隔符的概念消失了,直到您调用toString()
为止。 But very few libraries use this and still use File
or even, worse, String
. 但是很少有库使用此功能,并且仍然使用File
或更糟糕的String
。
If the library you're using still does it by the mean of strings, it probably means they're splitting manually on /
. 如果您正在使用的库仍然通过字符串来实现,则可能意味着它们是在/
手动拆分的。 Maybe it's time to tell them to upgrade? 也许是时候告诉他们升级了吗?
In my case it was a bug in the library, that I use - https://github.com/yui/yuicompressor/issues/111 . 就我而言,这是我使用的库中的错误-https: //github.com/yui/yuicompressor/issues/111 。
That's why all suggested solution didn't solved issue for me. 这就是为什么所有建议的解决方案都没有为我解决问题的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.