简体   繁体   English

读取文件时出现NoSuchFileException

[英]NoSuchFileException while reading a file

I am trying to read a file, but I get a NoSuchFileException . 我正在尝试读取文件,但出现了NoSuchFileException I know my code work, because it works in another program I have created, but it doesn't work now. 我知道我的代码有效,因为它可以在我创建的另一个程序中使用,但是现在不起作用。 Yes the directory is correct and there is a text file in the src folder. 是的,目录正确,并且src文件夹中有一个文本文件。 Please could someone tell me how to fix this. 请有人告诉我如何解决此问题。

String[] words = new String[5];

Path file = Paths.get("H:\\Varsity work\\Java Programming\\Programs\\HangMan\\build\\classes\\HangMan.txt");
InputStream  input = null;
try {
    input = Files.newInputStream(file);
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    String s = null;

    while((s=reader.readLine())!=null) {
        System.out.println(s);
    }
    input.close();
} catch(Exception e) {
    System.out.println(e);
}

尝试使用'/'而不是'\\',因此无需转义使用任何字符和路径字符串。

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

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