[英]How can I read my file which is in a resources folder in Java?
I have created a new folder in "scr" with the name "resources". 我在“ scr”中创建了一个名为“ resources”的新文件夹。 There I have put my file "Test.txt". 我已经把文件“ Test.txt”放在那里了。 Now I would like to read in this file with a BufferedReader. 现在,我想使用BufferedReader读取此文件。 This is my current code: 这是我当前的代码:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("/resources/Test.txt").getFile());
in = new BufferedReader(new FileReader(file));
But it doesn't work! 但这不起作用! The file is not found. 找不到文件。 Why? 为什么?
Make sure you have added the resources folder to your Build Path , and change 确保已将resources文件夹添加到Build Path ,然后进行更改
classLoader.getResource("/resources/Test.txt").getFile()
to 至
classLoader.getResource("Test.txt").getFile()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.