项目使用 maven 构建及其文件结构如下。
fnlp
├── fnlp-app
├── fnlp-core
├── models
│ └── seg.m
└── tmp
└── lucene
现在,我将工作目录切换到子模块fnlp-app
并设置VM OPTIONS
-Duser.dir=$MODULE_DIR$
。
Directory dir = FSDirectory.open(new File("../tmp/lucene"));
使用FSDirectory.open
时,它可以获得预期的结果。
FileInputStream fileInputStream = new FileInputStream(new File("../models/seg.m"));
然而,执行上面的代码,它得到 java.io.FileNotFoundException:../ model / seg.m(没有这样的文件或目录)
简单的测试代码片段和日志。(感谢@ jon-skeet的建议。)
System.out.println(System.getProperty("user.dir"));
// Output: /home/xohozu/workbench/git/github/fnlp/fnlp-app
System.out.println(new File("./models/seg.m").getCanonicalPath());
// Output: /home/xohozu/workbench/git/github/fnlp/fnlp-app/models/seg.m
FileInputStream inSteam1 = new FileInputStream(new File("./models/seg.m"));
// Passed
FileInputStream inSteam11 = new FileInputStream(new File("/home/xohozu/workbench/git/github/fnlp/fnlp-app/models/seg.m"));
// Exception in thread "main" java.io.FileNotFoundException: /home/xohozu/workbench/git/github/fnlp/fnlp-app/models/seg.m (No such file or directory)
System.out.println(new File("../models/seg.m").getCanonicalPath());
// Output: /home/xohozu/workbench/git/github/fnlp/models/seg.m
FileInputStream inSteam2 = new FileInputStream(new File("../models/seg.m"));
// Exception in thread "main" java.io.FileNotFoundException: ../models/seg.m (No such file or directory)
FileInputStream inSteam22 = new FileInputStream(new File("/home/xohozu/workbench/git/github/fnlp/models/seg.m"));
// Passed