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?

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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.
Got it. Thanks for the answer Titus!! I just replaced List item with empty spaces, line=line.replaceAll("^"+delete+"$", ""); It worked out.
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 I've added an example in my answer for that.
Thanks a lot, buddy! Works fine :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.