0

I'm new to Java and I'm trying to use this class to return the content of "test.csv".This only works when I use the path of local files.

public class CSVtoArray2 {

    public static final String filename = "C:\\eclipse\\workspace\\project\\src\\main\\webapp\\resources\\csv\\test.csv";


    public String testMethod() throws IOException {
        BufferedReader wr = null;

        try {

            wr = new BufferedReader(new FileReader(new File(filename)));

            StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = wr.readLine()) != null) {
                stringBuffer.append(line);
                stringBuffer.append("\n");
            }
            wr.close();
            return stringBuffer.toString();
        } finally {
            wr.close();
        }
    }
}

When I change

"C:\eclipse\workspace\project\src\main\webapp\resources\csv\test .csv" to "/resources/csv/test.csv",

this class gets a null return.

Anyone here who can help?

Thanks!

UPDATE:

Solution:

Copying my CSV file to project resources folder:

ClassLoader classLoader = getClass().getClassLoader();
File filename = new File(classLoader.getResource("csv/test.csv").getFile());
Zeca Novaes
  • 379
  • 1
  • 2
  • 17
  • `src` folder does not exist relative to where the compiled class is ran – OneCricketeer Sep 21 '17 at 13:36
  • Still not working here. Any other idea? Thank you! – Zeca Novaes Sep 21 '17 at 13:39
  • https://stackoverflow.com/questions/13967307/reading-from-src-main-resources-gives-nullpointerexception – OneCricketeer Sep 21 '17 at 13:39
  • 1
    Using *Stream* instead of *File*: *CSVtoArray2.class.getResourceAsStream()*? – Oleg Cherednik Sep 21 '17 at 13:40
  • 1
    Where is the file? Are you running this in Eclipse? Eclipse looks at the project directory for a file – Tejal Sep 21 '17 at 13:40
  • I'm running in localhost. – Zeca Novaes Sep 21 '17 at 13:41
  • @Zeca Your "webapp" isn't running within Eclipse. The `src/main/webapp` folder doesn't exist when you run the code.. You need to load the file from the full path, or the class loader is needed – OneCricketeer Sep 21 '17 at 13:43
  • Thank you, cricket, I changed the path many times, including the "resources/csv/test.csv". This is the path to access my image files too (resources/img). Still not working for this class only – Zeca Novaes Sep 21 '17 at 13:46
  • Your java code needs to be in `src/main/java`. Move all other files to `src/main/resources`... Are you using `CSVtoArray2.class.getResource` methods as mentioned? You *should not* need to have "resources" or "src" be part of the file path you want to open – OneCricketeer Sep 21 '17 at 13:56
  • 1
    See [loading resources](https://stackoverflow.com/q/676250/3690024) for different ways to load files – AJNeufeld Sep 21 '17 at 13:57
  • There are plenty of example questions of similar effect https://stackoverflow.com/questions/38255146/correct-way-of-reading-a-file-in-src-main-resources-as-string-in-a-web-applicati – OneCricketeer Sep 21 '17 at 13:58

1 Answers1

1
org.apache.commons.io.IOUtils.toString(CSVtoArray2.class.getResourceAsStream("/csv/test.csv"), Charset.forName("UTF-8")))

It is tricky, what is root path for the resource. In Eclipse you can mark some folder (e.g. webapp/recource) as resource folder. And When you build jar file, resource folder content will be in the root. So if you start your resource path from /, it means root of jar (i.e. root of resource folder).

I do not use relative paths, so you could look at open resource with relative path in java to get more info about it.

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35