0

How to load csv file using classpath? Before this I tried loading file using FileReader which works fine when running locally but when deployed into external server throws exceptions like 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. 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]); 

this is the exception

CodeNinja
  • 75
  • 2
  • 11
  • 1
    you need to post your code for us to see what is going on, and full exception stacktrace, did you use getClass().getResource().. and do you have file in your classpath? – Sendi_t Jun 07 '18 at 03:45
  • You could check this topic. You may find a solution to your problem. https://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java – AntiqTech Jun 07 '18 at 04:39

2 Answers2

3

Since you tagged the question with spring I would like to point you to the Resource Implementations that the Spring Framework provide. The documentation can be found under:

Spring Support for Resources

For your csv file something along the lines of

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

C. Weber
  • 907
  • 5
  • 18
  • I can read the csv file under src/test/resources from unit test case using ClassPathResource, but I am not able to read the same file under src/main/resources from src/main/java code – CodeNinja Jun 07 '18 at 15:09
  • @Sid How do you run your code? Maybe this is the answer to your problem https://stackoverflow.com/a/10171213/7634201 – C. Weber Jun 07 '18 at 15:16
  • I use gradle to build and run the jar – CodeNinja Jun 07 '18 at 15:27
  • Could you post the path you use to reference your file in the ClasspathResource? – C. Weber Jun 07 '18 at 15:37
1

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.

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.

The correct approach here will be using getClass().getResourceAsStream() / getClass().getClassLoader().getResourceAsStream()

  1. CSV files can be complicated although their structure looks dead simple. 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:

...to name a few

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • I can read the csv file under src/test/resources from unit test case using ClassPathResource, but I am not able to read the same file under src/main/resources from src/main/java code – CodeNinja Jun 07 '18 at 15:09