问题
I tried everything to access to csv file when a run my jar. I put the csv in resources package in Eclipse, and it's fine when I run the code from this, but it doesn't work when I run the jar from an executable.
ClassLoader c = MyClass.class.getClassLoader();
URL url = c.getResource("com/mysoft/resources/");
String path = URLDecoder.decode(url.getPath(), "utf-8");
File f = new File(path+ "VAL.csv");
if(f.exists())
...
I don't want to put this file out of the jar. I just want read the file, how can I do ?
回答1:
If you just want to read the file, you can do this:
InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream(csvPath)));
BufferedReader br = new BufferedReader(isr);
回答2:
Use getResourceAsStream and you should specify the encoding whenever possible. Try this:
in = new BufferedReader(
new InputStreamReader(
new BufferedInputStream(
this.getResourceAsStream("myPackage/myFile.txt")),
"UTF-8"));
来源:https://stackoverflow.com/questions/20248906/access-to-csv-file-in-jar