Java – how do I access the configuration files in the jar?
•
Java
I am using flatpack to parse and load data in flat files This requires loading the configuration file that stores the mapping of flat file columns
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";
I have a file datafile method for actual 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
}
When I can everything and run it in jar - it will bomb out on FileReader mapfilereader = new FileReader (mapping_file); Use FileNotFoundException Although the file is in jar
How did I do that?
I checked this question and this question about accessing the files in the jar. They both suggested extracting the files temporarily I don't want to do that
Solution
If it is in a jar, it is generally not a file You should use class Getresourceasstream (string) or something similar to load data
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
