Results 1 to 5 of 5
  1. #1
    barman is offline Member
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Question Deleting multiple lines of text from a file

    I have a huge load of text files from which I need to strip out text.

    I have good "rules" for the program to follow, but I don't know how to execute them.

    For example: Everything between "***TempStart" and "TempEnd***" should be deleted. And, everything after "***EndingStart" (right up until the end of the file) should be deleted also.

    How do I do it!?

    This: [SOLVED] Delete Current line from file looks like it might help me, but that's only for deleting one line.

    I'd appreciate any and all help I can get.

    Thanks!
    Barman

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The basic logic is to read the input file and copy to output file up to where the delete point starts. Then continue reading but not copying up to the end of the delete point and copy the rest of the file to the output.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Alternatively to what Norm suggested, you could also find the first line after "***TempStart", then delete that line repeatedly until it equals "TempEnd***". Then, continue iterating until you're after "***EndingStart" and delete that line until you reach the EOF.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    delete that line repeatedly
    What does it mean to "delete" a line in a file?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,365
    Blog Entries
    7
    Rep Power
    20

    Default

    There's a pattern for this: the Chain Of Responsibility; imagine a file reader, it reads line by line; before writing each line it consults a bunch of, say, Filers; if at least one of the Filters forbids writing the line, the line isn't written. If all Filters allow the line to be written it is written.

    Given a line and a bunch of Filters, they can be checked as follows:

    Java Code:
    boolean isWritable(String line) {
    
       for (Filter filter: filters)
          if (!filter.isAllowed(line))
             return false;
       return true;
    }



    The Filters object is simply a list of Filters:

    Java Code:
    List<Filter> filters= new ArrayList<Filter>();
    And the Filter interface is:

    Java Code:
    public interface Filter {
       public boolean isAllowed(String line);
    }
    One of the possible Filter implementations is:

    Java Code:
    public class EndStartFilter implements Filter {
    
       private boolean noEndStartSeen= true;
    
       public boolean isAllowed(String line) {
          if (noEndStartSeen)
             noEndStartSeen= line.equals("***EndingStart");
          return noEndStart;
       }
    }
    I'm sure you can implement the other Filters yourself. All you have to do is populate the filters List and give it a go.

    kind regards,

    Jos

Similar Threads

  1. [SOLVED] deleting a line from a text file
    By mokonji in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 01:35 PM
  2. How to remove 2 last lines in a text file?
    By Marius in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 03:54 PM
  3. [SOLVED] Writing ArrayList to Text File on seperate Lines
    By shinjitsunohana in forum New To Java
    Replies: 9
    Last Post: 08-27-2008, 05:53 PM
  4. Adding lines at start and end of the text file
    By phani532 in forum New To Java
    Replies: 5
    Last Post: 08-27-2008, 03:12 PM
  5. Remove duplicate lines from a text file
    By Dirt.Diver in forum New To Java
    Replies: 15
    Last Post: 06-25-2008, 02:08 PM

Tags for this Thread

Posting Permissions

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