简体   繁体   English

使用类路径在Java中加载CSV

[英]Load CSV in java using classpath

How to load csv file using classpath? 如何使用classpath加载csv文件? Before this I tried loading file using FileReader which works fine when running locally but when deployed into external server throws exceptions like FileNotFound. 在此之前,我尝试使用FileReader加载文件,该文件在本地运行时工作正常,但在部署到外部服务器时会抛出FileNotFound之类的异常。 Below code works in local, but when deployed the jar on tomcat folder www/[jar] throws this exception Unfortunately, I cannot provide the original code. 下面的代码在本地工作,但是将jar部署在tomcat文件夹www / [jar]上时,将引发此异常。不幸的是,我无法提供原始代码。 here is the gist: 要点是:

BufferedReader br = new BufferedReader(new FileReader("src/SSR.csv"));
    while ((line = br.readLine()) != null) {
        // use comma as separator
        String[] cols = line.split(",");
        map.put(cols[1],cols[0]); 

这是例外

There are two things you should consider here 您应该在这里考虑两件事

  1. As our colleagues have already stated, when the code runs in jar and you want to load a classpath resource you cannot go with FileReader approach that you've stated. 正如我们的同事已经说过的那样,当代码在jar中运行并且您想要加载类路径资源时,您就不能使用您所说的FileReader方法。

Your current code assumes that the CSV is somewhere in FileSystem outside the artifact, which is probably not true, so it just won't find the file. 您当前的代码假定CSV位于工件之外FileSystem中的某个位置,这可能不是正确的,因此它将找不到该文件。

The correct approach here will be using getClass().getResourceAsStream() / getClass().getClassLoader().getResourceAsStream() 正确的方法是使用getClass().getResourceAsStream() / getClass().getClassLoader().getResourceAsStream()

  1. CSV files can be complicated although their structure looks dead simple. CSV文件可能很复杂,尽管它们的结构看起来非常简单。 So if you work on a real-life project and not on some kind of homework for studying purposes, I strongly suggest you use 3rdparty libraries for working with CSV: 因此,如果您从事的是现实生活中的项目,而不是出于学习目的进行某种家庭作业,那么我强烈建议您使用3rdparty库来处理CSV:

...to name a few ...仅举几例

Since you tagged the question with spring I would like to point you to the Resource Implementations that the Spring Framework provide. 既然您用spring标记了问题,我想向您指出Spring框架提供的资源实现。 The documentation can be found under: 该文档可以在以下位置找到:

Spring Support for Resources 春季资源支持

For your csv file something along the lines of 对于您的csv文件,有些类似

Resource csv = context.getResource("classpath:some/resource/path/my.csv");
InputStream inputstream = csv.getInputStream();

should be a starting point. 应该是一个起点。 The Resource Interface provides a 资源接口提供了一个

File getFile() throws IOException;

method, but this does only work when the File resides in the filesystem. 方法,但这仅在文件位于文件系统中时才起作用。 So for your case you should use the approach with the inputstream I think. 因此,对于您的情况,您应该在我认为的输入流中使用该方法。

EDIT: Here you can find another answer with the use of the ClassPathResource https://stackoverflow.com/a/44412189/7634201 编辑:在这里您可以找到使用ClassPathResource https://stackoverflow.com/a/44412189/7634201的另一个答案

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

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