繁体   English   中英

如何在Java中读取文本文件时删除特定行

[英]how to remove particular lines when reading text file in java

我想删除找到“ RxTracker”的行,这是我的文本文件

INFO http-bio-80-exec-1 root - Received data;863071018134228;12.964624;77.523682;NE;23.22376;3.82;0;2013-01-06^08:41:00;
INFO http-bio-80-exec-1 root - RxTracker; IMEINO is 863071018134228
INFO http-bio-80-exec-2 root - RxTracker Invoked. Reading Parameters 
INFO http-bio-80-exec-2 root - Received data;863071018134228;12.964624;77.523682;NE;37.66936;3.82;0;2013-01-06^08:42:52;
INFO http-bio-80-exec-2 root - RxTracker; IMEINO is 863071018134228
INFO http-bio-80-exec-5 root - RxTracker Invoked. Reading Parameters 
INFO http-bio-80-exec-5 root - Received data;863071018134228;12.964624;77.523682;NE;20.92728;3.82;0;2013-01-06^08:44:51;
INFO http-bio-80-exec-5 root - RxTracker; IMEINO is 863071018134228
INFO http-bio-80-exec-3 root - RxTracker Invoked. Reading Parameters 

这是我的Java代码

public void Insert1()  
    {

        try{



            File f=new File("E:/c.txt");
            FileReader fr=new FileReader(f);    
            BufferedReader br=new BufferedReader(fr);

            int lineNum = 0;
            String line = null;
            while ( (line = br.readLine() ) != null ) {
               lineNum++;
               if(br.readLine().equalsIgnoreCase("RxTracker"))
               {
                   System.out.println("ji");
               }
               else
               {
                   System.out.println("ji");
               }
             //if ( lineNum %2  == 0 ) continue;
               //else deal with it
             System.out.println(br.readLine());

            }
        }
        catch(FileNotFoundException e){
            e.printStackTrace();
        }
        catch(IOException e1){
            e1.printStackTrace();   
        }

尝试这样的事情。 代码读取文件的每一行并将其写入另一个文件。每次它检查该行是否包含RxTracker。如果该行不包含RxTracker,则它将该行写入新文件,并且如果该行包含该字符串,则它将跳过该行,

例如

String lineToRemove = "RxTracker ;

 if(!trimmedLine.contains(lineToRemove)) { // check if t he line does not contains it then write it to another file 
            writer.write(trimmedLine);
            writer.newLine();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM