简体   繁体   English

如何读取Java资源文件夹中的文件?

[英]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.

相关问题 如何在项目文件夹下而不是源文件夹下读取资源 - How can I read resources under my project folder but not on my source folder 如何读取资源文件夹中的文件 - How to read file in resources folder 如何写入/读取/删除位于资源文件夹中的文本文件的内容? - How do I write/read/delete contents of a text file located in my resources folder? 如何让Maven从我的资源文件夹中读取文件 - How to get Maven to read a file from my resources folder Java:如何使用扫描仪类读取资源文件夹中的文本文件 - Java: How to use Scanner class to read text file in resources folder 为什么 java 在我的资源文件夹中找不到文件? - Why java can't find the file in my resources folder? 如何在 Eclipse 中向我的 Java 项目添加资源文件夹 - How do I add a resources folder to my Java project in Eclipse Java:如何从资源文件夹中加载.properties? - Java: how can I load .properties from resources folder? 如何读取从Maven的POM.xml中的资源文件夹中排除的文件 - How to read a file which has been excluded from resources folder in Maven's POM.xml 如何在运行时从JSF控制器访问位于“ src / main / resources”文件夹中的css文件 - How can I access a css file located in my “src/main/resources” folder from my JSF controller at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM