简体   繁体   English

如何强制Java在Windows上使用Unix文件分隔符?

[英]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.

相关问题 java中的文件类型(Windows,unix) - File type in java (Windows,unix) 如何在Windows上处理Windows和UNIX上的不同file.separator - How to deal with different file.separator on Windows and UNIX in java 如何使用Java将文件从Windows文件夹写入UNIX文件夹 - How to write a file into unix folder from windows folder using java 如何强制Java 7在Windows上创建“目录符号链接”而不是“文件符号链接”? - How to force Java 7 to create “directory symlinks” on Windows instead of “file symlinks”? 使用File for Unix / Windows创建Java目录 - Java create directory using File for Unix / Windows Java共享属性文件:Windows + Unix(通用) - Java shared Property File: Windows + Unix (generic) 从Windows OS到Java中的Unix文件迁移 - file treansfer from windows OS to unix in Java Windows 与 Java 中 Unix 的文件路径问题 - File path issue on windows vs Unix in java 如何在Java中使用“ scp”命令或任何UNIX命令,即,如何在Java中将文件从一个Unix盒文件传输到另一个Unix盒 - How to make use of “scp” command or any UNIX command in java , ie how to sfp a file from one Unix box to other in java 如何通过使用Java判断txt文件是Windows格式还是Unix格式文件 - How to judge a txt file is a Windows format or Unix format file by using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM