Open In App

Java program to delete certain text from a file

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : PrintWriter , BufferedReader

Given two files input.txt and delete.txt. Our Task is to perform file extraction(Input-Delete) and save the output in file say output.txt

Example :


 

Naive Algorithm :

1. Create PrintWriter object for output.txt
2. Open BufferedReader for input.txt
3. Run a loop for each line of input.txt
   3.1 flag = false
   3.2 Open BufferedReader for delete.txt
   3.3 Run a loop for each line of delete.txt
      ->  If  line of delete.txt is equal to current line of input.txt 
            -> flag = true
            -> break loop

4. Check flag, if false
     -> write current line of input.txt to output.txt
5. Flush PrintWriter stream and close resources.

To successfully run the below program input.txt and delete.txt must exits in same folder OR provide full path for them.




// Java program to perform file
// operation output = input-delete
  
import java.io.*;
  
public class FileOperation
{
    public static void main(String[] args) throws IOException 
    {
        // PrintWriter object for output.txt
        PrintWriter pw = new PrintWriter("output.txt");
          
        // BufferedReader object for input.txt
        BufferedReader br1 = new BufferedReader(new FileReader("input.txt"));
          
        String line1 = br1.readLine();
          
        // loop for each line of input.txt
        while(line1 != null)
        {
            boolean flag = false;
              
            // BufferedReader object for delete.txt
            BufferedReader br2 = new BufferedReader(new FileReader("delete.txt"));
              
            String line2 = br2.readLine();
              
            // loop for each line of delete.txt
            while(line2 != null)
            {
                if(line1.equals(line2))
                {
                    flag = true;
                    break;
                }
                  
                line2 = br2.readLine();
            }
              
            // if flag = false
            // write line of input.txt to output.txt
            if(!flag)
                pw.println(line1);
              
            line1 = br1.readLine();
              
        }
          
        pw.flush();
          
        // closing resources
        br1.close();
        pw.close();
          
        System.out.println("File operation performed successfully");
    }
}


Output:

File operation performed successfully

Note : If output.txt exist in cwd(current working directory) then it will be overwritten by above program otherwise new file will be created.

A better solution is to use HashSet to store each line of delete.txt and then while looping through lines of input.txt ,check if it is in hashset. If not present, write that line into output.txt.

To successfully run the below program input.txt and delete.txt must exits in same folder OR provide full path for them.




// Efficient Java program to perform file
// operation output = input-delete
  
import java.io.*;
import java.util.HashSet;
  
public class FileOperation
{
    public static void main(String[] args) throws IOException 
    {
        // PrintWriter object for output.txt
        PrintWriter pw = new PrintWriter("output.txt");
          
        // BufferedReader object for delete.txt
        BufferedReader br2 = new BufferedReader(new FileReader("delete.txt"));
          
        String line2 = br2.readLine();
          
        // hashset for storing lines of delete.txt
        HashSet<String> hs = new HashSet<String>();
          
        // loop for each line of delete.txt
        while(line2 != null)
        {
            hs.add(line2);
            line2 = br2.readLine();
        }
                      
        // BufferedReader object for input.txt
        BufferedReader br1 = new BufferedReader(new FileReader("input.txt"));
          
        String line1 = br1.readLine();
          
        // loop for each line of input.txt
        while(line1 != null)
        {
            // if line is not present in delete.txt
            // write it to output.txt
            if(!hs.contains(line1))
                pw.println(line1);
              
            line1 = br1.readLine();
        }
          
        pw.flush();
          
        // closing resources
        br1.close();
        br2.close();
        pw.close();
          
        System.out.println("File operation performed successfully");
    }
}


Output:

File operation performed successfully

Note : If output.txt exist in cwd(current working directory) then it will be overwritten by above program otherwise new file will be created.



Last Updated : 30 May, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads