Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FileInputStream无法使用特定的“user.dir”值正确打开“相对路径”文件_Java_File_Maven_Fileinputstream - Fatal编程技术网

Java FileInputStream无法使用特定的“user.dir”值正确打开“相对路径”文件

Java FileInputStream无法使用特定的“user.dir”值正确打开“相对路径”文件,java,file,maven,fileinputstream,Java,File,Maven,Fileinputstream,该项目使用maven构建其文件结构,如下所示 fnlp ├── fnlp-app ├── fnlp-core ├── models │   └── seg.m └── tmp └── lucene 现在,我将工作目录切换到子模块fnlp-app,并设置VM选项 使用-Duser.dir=$MODULE\u dir$ Directory dir = FSDirectory.open(new File("../tmp/lucene")); 当使用FSDirectory.open时,它可以

该项目使用maven构建其文件结构,如下所示

fnlp
├── fnlp-app
├── fnlp-core
├── models
│   └── seg.m
└── tmp
    └── lucene
现在,我将工作目录切换到子模块
fnlp-app
,并设置VM选项
使用
-Duser.dir=$MODULE\u dir$

Directory dir = FSDirectory.open(new File("../tmp/lucene"));
当使用
FSDirectory.open
时,它可以得到预期的结果

FileInputStream fileInputStream = new FileInputStream(new File("../models/seg.m"));  
然而,执行上述代码后,它得到了java.io.FileNotFoundException:../models/seg.m(没有这样的文件或目录)

简单的测试代码片段和日志。(感谢@jonskeet的建议。)


如果你看到我现在删除的评论,请忽略它。我建议您登录
新文件(“../models/seg.m”).getCanonicalPath()
以了解发生了什么事情。/path/to/fnlp/models/seg.m这是正确的。对,该文件确实存在吗?如果您将确切的输出放入字符串而不是相对文件名中,这是否有效?如果是这样,那就很奇怪了……它确实存在,而不是
newfileinputstream(新文件(“../models/seg.m”)
新文件输入流(新文件(“./models/seg.m”)无异常地传递。但是
新文件(“./models/seg.m”).getCanonicalPath()
printed/path/to/fnlp/fnlp-app/models/seg.m,太疯狂了。哦,我明白了-我把你之前的评论解释为对我第一条评论的回应。请使用
新文件(“../models/seg.m”).getCanonicalPath()和
新文件(“../models/seg.m”).getCanonicalPath()的记录结果编辑您的问题
    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