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());