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

时间:2015-01-23 06:41:09

标签: 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();   
        }

1 个答案:

答案 0 :(得分:0)

尝试这样的事情。代码读取文件的每一行并将其写入另一个文件。每次检查该行是否包含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();
        }