Access to csv file in jar

风格不统一 提交于 2019-12-24 06:38:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!