简体   繁体   中英

how to remove particular lines when reading text file in java

i want to delete that line where it finds "RxTracker" this is my text file

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 

this is my java code

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

Try something like this. The code reads each line of a file and writes it to another file.Every time it checks if that line contains RxTracker If that line doesn't contain the RxTracker then it will write that line to the new file and if it contains that string then it will skip that line,

for eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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