如何访问 jar 中的配置文件? [英] How do I access a config file inside the jar?

查看:24
本文介绍了如何访问 jar 中的配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 FlatPack 从平面文件中解析和加载数据.这需要加载一个配置文件,该文件存储平面文件的列的映射.

I'm using FlatPack to parse and load data from flat files. This requires loading a config file that stores mappings of the columns of the flat file.

我有一个常量来定义映射文件的位置:

I have a constant to define the location of the mapping file:

private static final String MAPPING_FILE = "src/com/company/config/Maping.pzmap.xml";

我有一个 parse(File dataFile) 方法来实际进行解析:

I have a parse(File dataFile) method that actually does the parsing:

private void parse(File dataFile) throws FileNotFoundException, SQLException {
        Parser parser;

        log.info("Parsing " + dataFile.getName());

        FileReader mappingFileReader = new FileReader(MAPPING_FILE);
        FileReader dataFileReader = new FileReader(dataFile);

        parser = DefaultParserFactory.getInstance().newFixedLengthParser(mappingFileReader, dataFileReader);
        parser.setHandlingShortLines(true);

        DataSet dataSet = parser.parse();

        //process the data
}

当我将所有内容打包并作为 jar 运行时 - 它会在 FileReader mappingFileReader = new FileReader(MAPPING_FILE); 上爆炸并带有 FileNotFoundException.不过那个文件在 jar 里面.

When I jar up everything and run it as a jar - it bombs out on FileReader mappingFileReader = new FileReader(MAPPING_FILE); with a FileNotFoundException. That file is inside the jar though.

我如何获得它?

我看过这个问题这个问题 关于访问 jars 中的文件和它们两者都建议暂时提取文件.不过我不想那样做.

I've looked at this question and this question about accessing files inside jars and they both recommend temporarily extracting the file. I don't want to do that though.

推荐答案

如果它在 JAR 中,一般来说它不是文件.您应该使用 Class.getResourceAsStream(String) 或类似的东西.

if it's inside a JAR, it's not a File, generally speaking. You should load the data using Class.getResourceAsStream(String), or something similar.

这篇关于如何访问 jar 中的配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆