日期:2014-05-16  浏览次数:20683 次

文件名包含空格,在Windows和Linux上JDK的File表现不同

? ?今天在将本地的项目部署到测试服务器上,出现一个Error。追查很久,最终发现原因是配置文件多写了一个空格。这个错误在windows上不会出现,在Linux上会找不到文件。

?

? ?验证代码如下(realPath是一个以空格结尾的字符串

?

logger.info("A: " + realPath + "," + realPath.length() + "," + new File(realPath).exists());
String path = realPath.trim();
logger.info("B: " + path + "," + path.length() + "," + new File(path).exists());
logger.info("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

Linux,运行结果

A: /opt/wf/dsweb/swap.config ,26,false
B: /opt/wf/dsweb/swap.config,25,true
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Windows,运行结果

A: E:\opt\wf\dsweb/swap.config ,28,true
B: E:\opt\wf\dsweb/swap.config,27,true
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++