如何在java中读取文本文件时删除特定行
我想删除找到“RxTracker”的那一行 这是我的文本文件如何在java中读取文本文件时删除特定行,java,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 -
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();
}
不能从文件读取器中删除行。您必须编写一个新文件,其中包含不应删除的行。您想在读取文件时从同一文件中删除它吗?您不能这样做。读取文件,并将读取的每一行写入新文件(除非它有“RxTracker”)。完成后,关闭两个文件并重命名新文件以覆盖旧文件。只需运行
sed”/RxTracker/d“c.txt