简体   繁体   English

找不到文件位置

[英]Cannot locate file location

I'm trying to scan a text file. 我正在尝试扫描文本文件。 Yes, I know... this is simple. 是的,我知道......这很简单。 I've been trying to locate the path of my text file and nothing works, it gives me a NoSuchFileException 我一直试图找到我的文本文件的路径,没有任何作用,它给了我一个NoSuchFileException

for (String line :  Files.readAllLines(Paths.get("/com/panda/anagramsolver/handling/word_list.txt"))) {
    System.out.println(line);
}

The file I'm trying to scan is located at com.panda.anagramsolver.handling.word_list.txt . 我正在尝试扫描的文件位于com.panda.anagramsolver.handling.word_list.txt I know this question will get a lot of downvotes, I couldn't elaborate my question enough to find it on google. 我知道这个问题会得到很多支持,我无法详细阐述我的问题,以便在谷歌上找到它。 In advance, thanks. 提前谢谢。

The path starts with a / . 路径以/开头。 So unless you mean to search inside a directory named com in your root directory, you will fail to locate the file. 因此,除非您要在根目录中搜索名为com的目录,否则将无法找到该文件。

You might want to try executing something like System.out.println(new File(".")) to find the location at which the program is executed, and then build up a relative path from that location. 您可能想尝试执行类似System.out.println(new File("."))来查找执行程序的位置,然后从该位置构建相对路径。 But at a more fundamental level, if the program is intended to work with that file always (ie, if the file is a resource for the program), you might want to turn to Class#getResourceAsStream() . 但是在更基础的层面上,如果程序打算始终使用该文件(即,如果文件是程序的资源),您可能希望转向Class#getResourceAsStream() This link is to a tutorial. 这个链接是一个教程。

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

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