I have a xml log that is modified constantly by another application; however my Java code runs concurrently and checks for a couple of strings. I am using a SAX Parser. Now my question is will I have to have a new instance of a FileInputStream every time I loop through the file? How about the parser?
So let’s say:
while(notfound)
{
FileInputStream fis = new FileInputStream(new File("c:/tmp/123.xml"));
SaxParser.parse(fis, sampleHandler);
notFound = sampleHandler.checkIfFound();
}
Thanks 😀
In the example you provided, you will need a new
FileInputStreameach time you want to start reading from the beginning of the file.Streamclasses don’t often allow for manual positioning/resetting of the ‘location’, as since the name (‘Stream’) implies, it’s just a pipe with bits spewing out of it.Since you’re using the
SaxParser.parse()class method to initiate the parsing, it doesn’t seem as though you actually have a parser object to re-create. So you should be fine with just re-creating theFileInputStream.But! It seems like current versions of the
SaxParserclass support passing in aFileinstance as the first parameter, so you can just repeatedly use:Avoiding the re-creation of the
FileInputStreamaltogether, and allowing the parser to handle that.