Rsch_I
Rsch_I

Reputation: 21

Remove a line in a text if

Hi my problem is that I have a text with something like :

something1
[ something2
something3
[ something4

I want to write a code in java that reads that text and search for a character, in this case "[", when it finds it, remove the entire line, in this case "[ something2" , and then keep searching for that character so in the end I will be having something like this

something1
something3

I already search for something like this and I find this approach

File inputFile = new File("myFile.txt");
File tempFile = new File("myTempFile.txt");

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

String lineToRemove = "bbb";
String currentLine;

while((currentLine = reader.readLine()) != null) {
    // trim newline when comparing with lineToRemove
    String trimmedLine = currentLine.trim();
    if(trimmedLine.equals(lineToRemove)) continue;
    writer.write(currentLine);
}

boolean successful = tempFile.renameTo(inputFile);

That code removes the entire line that haves in it "bbb", the problem here is that erases only the line that has in it only and elusively bbb by example

something1
bbb
something3
bbb something

after run the code we have

something1
something3
bbb something

so I tried to modify the code to search for the character [ and then erases the entire line. but I couldn't, so I was hopping that someone could help me.

Upvotes: 1

Views: 275

Answers (4)

Technotronic
Technotronic

Reputation: 8943

You should use the contains method.

while((currentLine = reader.readLine()) != null) {
    // CHANGED EQUALS TO CONTAINS
    if(trimmedLine.contains(lineToRemove)) continue;
    writer.write(currentLine);
}

Good luck!

Upvotes: 0

Ingo
Ingo

Reputation: 36349

You don't need to write a Java program for this. Chances are you have an OS that knows tools like grep, awk, perl. For example

grep -v '^\[' infile >outfile

will copy the lines you want from file infile to outfile

Upvotes: 1

Mitesh Pathak
Mitesh Pathak

Reputation: 1211

that code removes the entire line that haves in it "bbb", the problem here is that erases only the line that has in it only and elusively bbb by example

  • COZ of if(trimmedLine.equals(lineToRemove)) continue;

  • "bbb something".equals("bbb") returns false and so the line is not skipped

  • Use regex or String.startsWith() or String.contains() method

Try the following:

if(trimmedLine.contains(lineToRemove)) continue;

[EDIT] What I recommend is to implement KMP [1] in java (Pattern Matching). Will help you in future.

[1] http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm

Upvotes: 1

elbuild
elbuild

Reputation: 4899

Try this:

File inputFile = new File("myFile.txt");
File tempFile = new File("myTempFile.txt");

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

String lineToRemove = "bbb";
String currentLine;

while((currentLine = reader.readLine()) != null) {
    // trim newline when comparing with lineToRemove
    String trimmedLine = currentLine.trim();
    if(!trimmedLine.contains(lineToRemove))
    { 
      writer.write(currentLine);
    }
}

boolean successful = tempFile.renameTo(inputFile);

Upvotes: 1

Related Questions