简体   繁体   English

如何在与.java文件相同的包中获取.txt文件?

[英]How to get a .txt file in the same package as my .java file?

I need to get the path of the .txt file in my package on startup, but I don't find a way to do so. 我需要在启动时在我的程序包中获取.txt文件的路径,但是我找不到解决方法。 Google does not help me with solutions that work. Google无法为我提供有效的解决方案。

It's Test/src/test/main.java and Test/src/test/file.txt in Eclipse. 在Eclipse中是Test/src/test/main.javaTest/src/test/file.txt

Everything is in the main method because the do something is so short that an extra function or class is not worth it. 一切都在main方法中,因为do something太短,以至于额外的函数或类都不值得。

try (Stream<String> lines = Files.lines(Paths.get(???) {
    lines.forEach(line -> {
        // "do something with the line"
    }
}

如果您要读取文件,请使用文件读取器,并以字符串形式传递文件路径。

Scanner fileScanner = new Scanner(new BufferedReader(new FileReader("your file");

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

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