• Post Reply Bookmark Topic Watch Topic
  • New Topic

resetting buffered reader to beginning of file

 
DC Dalton
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little stuck here. Im building a small website search tool for my site. First let me tell you I store all content for the site in .txt files. So what Im doing is opening the directory, grabbing the file names, then opening a stream using a buffered reader. I gues the code would be easier than explaining:

java.net.URL url = getServletContext().getResource("/sitePages/"+fileToRead);
java.io.InputStream is = url.openStream();
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(is));

so then Im looping thru the lines of the file cleaning out any html tags etc & searching for matches to the inputted keywords. Works like a charm actually BUT then I went one more step to allow muliple words to be entered into the search ...... thats where the problems began.

I have searched high and low to find a method that will reset the reader back to the beginning of the file to re-search it with another keyword. Of course I can get it to work by closing the br & is and then re-opening them but thats seems like a horrible use of IO and Im afraid it will beat the heck out of the server in the process.

Anyone out there have something that I have missed here? Im a memory / server load fanatic so these type thing weigh heavily on my mind! Thanks in advance.
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no experience of using them, but take a look at the mark() and reset() methods in InputStream. Perhaps you can call mark() before you read the first line and then reset() when you get EOF
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!