繁体   English   中英

Java:尝试从文本文件读取时获得FileNotFoundException,即使该文件存在

[英]Java: Getting FileNotFoundException when trying to read from a text file even though the file exists

我正在尝试从src\\main\\java\\props\\engprops.txt.txt文件读取。 我在这里使用反斜杠,因为我在使用Windows。

当我尝试读取文件时,出现FileNotFoundException

这是我的助手课。 它包含一种用于返回文件所在目录的字符串表示形式的方法。 这是它返回的内容C:\\Users\\SOME_USER\\workarea\\Myapp\\myapp\\src\\main\\props\\

private final static String APP_HOME = "\\workarea\\Translate\\translate\\src\\main";

private final static String PROPS_HOME = "\\props\\";

public static String getPropsPath() {
    StringBuilder sb = new StringBuilder();
    String userHome = System.getProperty("user.home");
    //userHome = userHome.replace("\\", "/");
    String propsHome = null;
    propsHome = userHome + APP_HOME + PROPS_HOME;

    return propsHome;
}

然后在我的主类中,我尝试读取文件:

private static String stringPath = AppHelper.getPropsPath();

public static void main(String[] args) {
    readFile();
}

public static void readFile() throws IOException {
    FileReader fReader = new FileReader(stringPath + "engprops.txt");
    BufferedReader bReader = new BufferedReader(fReader);

    String line = null;

    while((line = bReader.readLine()) != null) {
        System.out.println(line);
    }

    bReader.close();
}

readFile()方法内部,我将文件名附加到stringPath变量上,文件名为engprops.txt

这是程序执行后控制台显示的片段; java.io.FileNotFoundException: C:\\Users\\600010209\\workarea\\Translate\\translate\\src\\main\\props\\engprops.txt (The system cannot find the file specified)

您在问题中写道,您的文件位于src / main / java / props下,但您声明的props路径位于src / main / props下。

如果那不能回答您的问题,请发表评论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM