2

My input file has numerous records and for sample, let us say it has (here line numbers are just for your reference)

 1. end 
 2. endline
 3. endofstory

I expect my output as:

 1. 
 2. endline
 3. endofstory

But when I use this code:

import java.io.*;
public class DeleteTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
        File file = new File("D:/mypath/file.txt");
        File temp = File.createTempFile("file1", ".txt", file.getParentFile());
        String charset = "UTF-8";
        String delete = "end";
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(temp), charset));
        for (String line; (line = reader.readLine()) != null;) {
            line = line.replace(delete, "");
            writer.println(line);
        }
        reader.close();
        writer.close();
        }
        catch (Exception e) {
            System.out.println("Something went Wrong");
        }
    }

}

I get my output as:

 1. 
 2. line
 3. ofstory

Can you guys help me out with what I expect as output?

venk
  • 55
  • 8

1 Answers1

1

First, you'll need to replace the line with the new string List item not an empty string. You can do that using line = line.replace(delete, "List item"); but since you want to replace end only when it is the only string on a line you'll have to use something like this:

line = line.replaceAll("^"+delete+"$", "List item");

Based on your edits it seems that you indeed what to replace the line that contains end with an empty string. You can do that using something like this:

line = line.replaceAll("^"+delete+"$", "");

Here, the first parameter of replaceAll is a regular expression, ^ means the start of the string and $ the end. This will replace end only if it is the only thing on that line.

You can also check if the current line is the line you want to delete and just write an empty line to the file.

Eg:

if(line.equals(delete)){
     writer.println();
}else{
     writer.println(line);
}

And to do this process for multiple strings you can use something like this:

Set<String> toDelete = new HashSet<>();
toDelete.add("end");
toDelete.add("something");
toDelete.add("another thing");

if(toDelete.contains(line)){
     writer.println();
}else{
     writer.println(line);
}

Here I'm using a set of strings I want to delete and then check if the current line is one of those strings.

Titus
  • 22,031
  • 1
  • 23
  • 33
  • Thanks for your time. But, there was a typo in my expected output. I have edited now. So, kindly provide me solutions according to it. – venk May 07 '17 at 15:00
  • Got it. Thanks for the answer Titus!! I just replaced List item with empty spaces, line=line.replaceAll("^"+delete+"$", ""); It worked out. – venk May 07 '17 at 15:04
  • Awesome response! Is there a way to include a list of words I wish to delete using an array and delete those words in the line that matches with the array list? – venk May 07 '17 at 15:13
  • @venk I've added an example in my answer for that. – Titus May 07 '17 at 15:16
  • Thanks a lot, buddy! Works fine :) – venk May 07 '17 at 15:30
  • @venk Great, I'm glad I could help. Good luck. – Titus May 07 '17 at 15:30
  • Hey, Just a quick question, what if there is a line in the text file with "end (with some word between) end". In this case, the above code doesn't replace any of the word "end" on the above line. Have a solution for this? – venk May 07 '17 at 18:04
  • You can use `line = line.replaceAll("\bend\b", "")`. `\b` stands for word boundary which means that this will remove all `end` words from the line. – Titus May 07 '17 at 18:19
  • If I use '\b', java interprets this as back-space, hence I have put '\\b' which interprets as the boundary regex. I still have a bug. Refer the below answer and provide me some solution – venk May 08 '17 at 06:00
  • http://stackoverflow.com/questions/43841813/issue-with-below-snippet-on-boundary-matchers-regex-b Follow this link to answer – venk May 08 '17 at 07:15