Results 1 to 4 of 4
  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default Scanner/BufferedReader?

    Hi,

    I am working on a project where I am recursing through a directory and checking all files/subdirectory files for a particular string. If the file contains the string I need to remove it.

    Currently the way I handle this situation is by using Scanner on each file, taking the file in Line by Line and checking if the line.contains("mystring"). If it does, I remove it, and write the file, etc.

    This seems to be very inefficent if a file does not contain mystring, as we still going through the file line by line.

    With that being said, does anyone have any brighter ways to determine whether the file contains my string without having to parse through the file line by line to determine that?

    Thanks.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    Well, if you read the file into a String, you could then use indexOf("mystring") and that would tell you if the file contained that word, but you still have to read the file into a string and indexOf still looks at every word, you just wouldnt have to have a big for loop.

  3. #3
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Yea that doesn't really sound like it would cut down on the efficency. How can I read an entire file into a single string curiously? Or what is the easiest way...

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    Yeah, you would still have to read it line by line, or, to be more efficient if you could use a buffer so you don't use too much memory.

Similar Threads

  1. How to use BufferedReader?
    By ProgramBeginner in forum New To Java
    Replies: 4
    Last Post: 10-11-2011, 03:56 PM
  2. BufferedReader, need help!
    By zacharyrod in forum New To Java
    Replies: 10
    Last Post: 11-19-2009, 11:56 AM
  3. scanner vs. BufferedReader? printstream vs. FileWriter?
    By diggitydoggz in forum New To Java
    Replies: 1
    Last Post: 01-09-2009, 02:58 AM
  4. BufferedReader
    By vidhya.sk in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 02:57 PM
  5. BufferedReader empty
    By Peter in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 07:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •