簡體   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