How to access the configuration file inside the bank?

I use FlatPack to analyze and load data from flat files. This requires loading a configuration file that stores the column mappings of the flat file.

I have a constant for locating the mapping file:

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

I have a parse (File dataFile) method that actually parses:

 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 crack everything and run it like a jar - it explodes on FileReader mappingFileReader = new FileReader(MAPPING_FILE); using FileNotFoundException . However, this file is inside the jar.

How do I get to it?

I looked at this question and this question about accessing files inside jars, and they both recommend temporarily extracting the file. I do not want to do this.

+6
java jar
source share
4 answers

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

+12
source share

If I remember correctly, getResourceAsStream () may behave differently depending on which web server is being deployed in your webapp, for example, I think this may be a problem when deploying as a war against an instance of Websphere. But I'm not sure if this applies to you.

But I'm not sure that you are trying to solve the "right" problem: if it is a configuration file, then it depends on the data? Does it depend on the code (your bank)? When the flat file changes, your configuration file must also change, right? If true, it sounds as if the configuration should be better saved elsewhere or even transferred as a parameter to your bank.

But maybe I did not fully understand your problem ...

+1
source share

Use the Apache Commons configuration, then you can read / write XML, automatically update, find the configuration file in the path or bank, without any problems.

0
source share

All Articles