0

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 :D

jlisam13
  • 93
  • 10

1 Answers1

1

In the example you provided, you will need a new FileInputStream each time you want to start reading from the beginning of the file. Stream classes 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 the FileInputStream.

But! It seems like current versions of the SaxParser class support passing in a File instance as the first parameter, so you can just repeatedly use:

while(notfound)
{
    SaxParser.parse(new File("c:/tmp/123.xml"), sampleHandler);
    notFound = sampleHandler.checkIfFound();
}

Avoiding the re-creation of the FileInputStream altogether, and allowing the parser to handle that.

Craig Otis
  • 31,257
  • 32
  • 136
  • 234