java - New file in linux environment with absolute path
问题描述
This is the problem:
- Path to the folder we want to analyse is correctly retrieved.
- We create the file for that folder(with the suposely AbsolutePath). Printing the AbsolutePath now adds a prefix to the original path.
- What's in getPath is what should be in getAbsolutePath.
- What's being added to the path is the directory where the JVM was run... The absolute path we provided is being wrongly interpreted as a relative path.
- We can't detect the files in Ep because the file path doesn't exist.
I've tried creating using Paths.get(string)
, Paths.get(URI)
, Paths.get(".").relativize(Path other)
, etc. Always with the same result.
logger.info("referencePattern.getLocation().getYearFolder(year): "
+ referencePattern.getLocation().getYearFolder(year));
File rootFolder = new File(referencePattern.getLocation().getYearFolder(year));
logger.info("rootFolder.getAbsolutePath(): " + rootFolder.getAbsolutePath());
logger.info("rootFolder.getPath(): " + rootFolder.getPath());
logger.info("System.getProperty(\"user.dir\"): " + System.getProperty("user.dir"));
File[] files = rootFolder.listFiles(filter);
Output:
referencePattern.getLocation().getYearFolder(year): /dvl/app/srs/tomcat/data/dg/ep/docs_autres/commeenne/com/2018/
rootFolder.getAbsolutePath(): /dvl/app/srs/tomcat/temp//dvl/app/srs/tomcat/data/dg/ep/docs_autres/commeenne/com/2018
rootFolder.getPath(): /dvl/app/srs/tomcat/data/dg/ep/docs_autres/commeenne/com/2018
System.getProperty("user.dir"): /dvl/app/srs/tomcat/temp
解决方案
来自评论:
getYearFolder(year)
将以下内容写入日志:
/dvl/app/srs/tomcat/data/ep/docs_autres/commeenne/com/2018/
getYearFolder(year).getBytes("UTF-8")
写道:
[36, 123, 115, 121, 115, 58, 117, 115, 101, 114, 46, 104, 111, 109, 101, 125, 47, 100, 97, 116, 97, 47, ...]
显示的字节代表字符串:
${sys:user.home}/data/
似乎logger.info(...)
正在做替换,${sys:user.home}
在/dvl/app/srs/tomcat
记录的输出中替换为。
在 Java 代码中看到的实际值是${sys:user.home}/data/ep/docs_autres/commeenne/com/2018/
,它不以 a 开头,/
因此被认为是相对路径,因此getAbsolutePath()
在当前目录 ( /dvl/app/srs/tomcat/temp
) 前加上前缀,结果是:
/dvl/app/srs/tomcat/temp/${sys:user.home}/data/ep/docs_autres/commeenne/com/2018/
然后记录器执行替换为:
/dvl/app/srs/tomcat/temp//dvl/app/srs/tomcat/data/ep/docs_autres/commeenne/com/2018/
需要考虑的事项:
如果
getYearFolder(year)
应该返回文件路径,为什么要返回${sys:user.home}
?在什么时候(代码中的什么地方)
${sys:user.home}
应该被替换?它肯定需要在被调用之前。getAbsolutePath()
getAbsolutePath()
如果返回的路径getYearFolder(year)
已经应该是绝对的,为什么会调用它?
这些问题的答案应该为如何解决问题提供指导。
推荐阅读
- php - 调用字符串上的成员函数 getClientOrginalName()
- java - java浮点格式说明符
- html - 为什么当我在 li 菜单的 id 中添加斜杠时动画不起作用?
- excel - 使用带有 sheet.name 的不同工作表的单元格公式
- ios - 缺少必需的模块“libtesseract libleptonica”
- ruby - 如何从代码中守护 ruby 服务器?
- java - 如何将文本字段值添加到选择框?
- excel - 运行时错误 1004 使用 vba 的 vlookup 中的应用程序定义或对象定义错误
- reactjs - 如果 initialValue 更改,则更新 antd 表单
- java - 如何使用内部变量制作 String.format 标志