• Tags
  •         
  • www.breakyizhan.com
  •    

    先决条件: PrintWriterBufferedReader

    给出两个文件input.txt和delete.txt。我们的任务是执行文件提取(Input-Delete)并将输出保存在文件say output.txt中

    示例:

    Java程序从文件中删除某些文本
    Naive 算法:

    1.为output.txt创建PrintWriter对象
    2.为input.txt打开BufferedReader
    3.为input.txt的每一行运行一个循环
       3.1 flag = false
       3.2打开bufferedReader for delete.txt
       3.3为delete.txt的每一行运行一个循环
          - >如果delete.txt的行等于input.txt的当前行 
                - > flag = true
                - >打破循环
    
    4.检查标志,如果为false
         - >将input.txt的当前行写入output.txt
    5.刷新PrintWriter流并关闭资源。
    

    Java程序从文件中删除某些文本的示例

    要成功运行以下程序,input.txt和delete.txt必须退出到同一文件夹中,或者为它们提供完整路径。

    // 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");
        }
    }
    

    输出:

    File operation performed successfully

    注意:如果output.txt存在于cwd(当前工作目录)中,那么它将被上面的程序覆盖,否则将创建新文件。

    一个更好的解决方案是使用HashSet存储delete.txt的每一行,然后在循环input.txt行时,检查它是否在hashset中。如果不存在,请将该行写入output.txt。

    要成功运行以下程序,input.txt和delete.txt必须退出到同一文件夹中,或者为它们提供完整路径。

    // 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");
        }
    }
    

    输出:

    File operation performed successfully

    注意:如果output.txt存在于cwd(当前工作目录)中,那么它将被上面的程序覆盖,否则将创建新文件。

     
    转载请保留页面地址:https://www.breakyizhan.com/java/5022.html