Results 1 to 5 of 5
- 06-28-2010, 09:00 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 1
- Rep Power
- 0
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
- 06-28-2010, 09:03 PM #2
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.
- 06-28-2010, 10:16 PM #3
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.
- 06-28-2010, 11:51 PM #4
What does it mean to "delete" a line in a file?delete that line repeatedly
- 06-29-2010, 09:12 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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:
And the Filter interface is:Java Code:List<Filter> filters= new ArrayList<Filter>();
One of the possible Filter implementations is:Java Code:public interface Filter { public boolean isAllowed(String line); }
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.Java Code:public class EndStartFilter implements Filter { private boolean noEndStartSeen= true; public boolean isAllowed(String line) { if (noEndStartSeen) noEndStartSeen= line.equals("***EndingStart"); return noEndStart; } }
kind regards,
Jos
Similar Threads
-
[SOLVED] deleting a line from a text file
By mokonji in forum New To JavaReplies: 0Last Post: 03-10-2009, 01:35 PM -
How to remove 2 last lines in a text file?
By Marius in forum New To JavaReplies: 2Last Post: 11-30-2008, 03:54 PM -
[SOLVED] Writing ArrayList to Text File on seperate Lines
By shinjitsunohana in forum New To JavaReplies: 9Last Post: 08-27-2008, 05:53 PM -
Adding lines at start and end of the text file
By phani532 in forum New To JavaReplies: 5Last Post: 08-27-2008, 03:12 PM -
Remove duplicate lines from a text file
By Dirt.Diver in forum New To JavaReplies: 15Last Post: 06-25-2008, 02:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks