[英]How to get Maven to read a file from my resources folder
I have been happily using JUnit to run my tests and everything has been fine. 我一直很高兴地使用JUnit运行测试,一切都很好。
However, I now need to use Maven but for some reason it cannot find any of my resource files. 但是,我现在需要使用Maven,但由于某种原因,它找不到我的任何资源文件。
The files are in the expected place: src/main/resources 文件在预期的位置:src / main / resources
I am using the following code to try to read a file: 我正在使用以下代码尝试读取文件:
public Map<String, String> readCsv(String filename) {
Map<String, String> headersAsMap;
CSVDataManipulator csvDataManipulator = new CSVDataManipulator();
ClassLoader classLoader = getClass().getClassLoader();
String wrkbook = new File(classLoader.getResource(filename).getFile()).toString();
headersAsMap = csvDataManipulator.getAllRecordsAsMap(wrkbook);
return headersAsMap;
}
However, try as I might it cannot find the file. 但是,请尝试尝试,因为找不到文件。 I've tried lots of different code and tried moving the files to different locations but I cannot get Maven to find my resource files.
我尝试了很多不同的代码,并尝试将文件移动到不同的位置,但是我无法让Maven找到我的资源文件。
Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks
谢谢
To my understanding classLoader.getResource(..) expects the file to be in a folder structure matching the package of the class. 据我所知,classLoader.getResource(..)期望文件位于与该类的包匹配的文件夹结构中。 So if the package of your class is com.matt.stuff, then you'll have to put the csv file in src/main/resources/com/matt/stuff.
因此,如果您的类的软件包是com.matt.stuff,则必须将csv文件放入src / main / resources / com / matt / stuff。
Or you could just use this to grab your csv file: 或者,您可以使用它来获取您的csv文件:
private static String readFile(String fileName) throws IOException {
//filename can be src/main/resources/my-csv.csv
return new String(Files.readAllBytes(Paths.get(fileName)));
}
Here's my file structure of a Maven project built from the quick-start-archetype, with comons-io added as a dependency: 这是我从快速启动原型构建的Maven项目的文件结构,其中添加了comons-io作为依赖项:
src
src/main
src/main/java
src/main/java/com
src/main/java/com/essexboy
src/main/java/com/essexboy/App.java
src/main/resources
src/main/resources/dir1
src/main/resources/dir1/test.txt
src/main/resources/dir2
src/main/resources/dir2/test.txt
src/main/resources/test.txt
src/test
src/test/java
src/test/java/com
src/test/java/com/essexboy
src/test/java/com/essexboy/AppTest.java
Here's my test 这是我的测试
public class AppTest {
@Test
public void shouldAnswerWithTrue() throws Exception {
StringWriter writer = new StringWriter();
IOUtils.copy(getClass().getResourceAsStream("/test.txt"), writer, Charset.defaultCharset());
assertEquals("from root", writer.toString().trim());
writer = new StringWriter();
IOUtils.copy(getClass().getResourceAsStream("/dir1/test.txt"), writer, Charset.defaultCharset());
assertEquals("from dir1", writer.toString().trim());
writer = new StringWriter();
IOUtils.copy(getClass().getResourceAsStream("/dir2/test.txt"), writer, Charset.defaultCharset());
assertEquals("from dir2", writer.toString().trim());
}
}
A File
is indeed a file on the file system. File
确实是文件系统上的文件。 For a resource which might be a file zipped in a jar, and has a path on the class path, you need something else. 对于可能在一个罐子里是一个文件拉上,并具有类路径上的路径的资源 ,你需要别的东西。
Traditionally one would use a more general InputStream instead of a File. 传统上,人们会使用更通用的InputStream代替File。
InputStream in = getClass().getResourceAsStream("/.../x.csv"); // Path on the class path
With the new class Path
, more general than File
, you can deal with several (virtual) file systems: 使用比
File
更通用的新类Path
,您可以处理多个(虚拟)文件系统:
URL url = getClass().getResource("/.../x.csv"); // Path on the class path
Path path = Paths.get(url.toURI());
Files.copy(path, Paths.get("..."));
With a bit of luck your CSVManipulator should besides being parametrized with a File, also with an InputStream
or Reader
( new InputStreamReader(in, "UTF-8")
) 幸运的是,您的CSVManipulator除了应使用文件进行参数设置外,还应使用
InputStream
或Reader
( new InputStreamReader(in, "UTF-8")
)进行参数设置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.