Okay, I am writing a small program, and I have found that getting images from within a compiled jar is easy. Though I need to grab some xml files. I'm a little confused that I have to interpret it as an input stream.
Anyway, I have googled around and found that I have to read the file within a jar as an input stream, the problem being is I don't know how to interpret it with the way I'm reading in the file now.
This is how I'm doing it now within eclipse (and works perfectly) but as soon as i compile it can't find the file. If I put a hard link to where the file is on my computer it works.
file = new File(this.getClass().getResource("/resources/maps/"+map.getMapName()).toURI());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
What I have been trying to do is read the inputstream into a file that is in memory. I don't want to actually create the file.
Here is what I have so far:
File file = new File("data.xml");//Creates unwanted file
InputStream input = this.getClass().getResourceAsStream("/resources/maps/"+map.getMapName());
OutputStream output = new FileOutputStream(file);
byte buf[] = new byte[1024];
int len;
while((len=input.read(buf))>0){
output.write(buf,0,len);
output.close();
input.close();
Log.msg("Finished reading input stream");
}
This method partially works, it creates a file (i dont want it too) and then in the file there is only part of the actual XML file.
I would like to thank everyone in advance for the help.
Cheers
Metric

New Topic/Question
Reply


MultiQuote



|