简体   繁体   English

在jar中访问csv文件

[英]Access to csv file in jar

I tried everything to access to csv file when a run my jar. 当我运行我的jar时,我尝试了访问csv文件的所有内容。 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. 我将csv放在Eclipse中的资源包中,当我从中运行代码时它很好,但是当我从可执行文件运行jar时它不起作用。

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. 我不想把这个文件放在jar里面。 I just want read the file, how can I do ? 我只想读取文件,我该怎么办?

If you just want to read the file, you can do this: 如果您只想阅读文件,可以这样做:

InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream(csvPath)));
BufferedReader br = new BufferedReader(isr);

Use getResourceAsStream and you should specify the encoding whenever possible. 使用getResourceAsStream,您应该尽可能指定编码。 Try this: 尝试这个:

in = new BufferedReader(
       new InputStreamReader(
         new BufferedInputStream(
           this.getResourceAsStream("myPackage/myFile.txt")),
         "UTF-8"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM