删除挺麻烦的,这里其实只是把需要的行存到arraylist中再存到另一个文件中
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class domainRealse {
public static void main(String[] args) throws IOException {
readFileByLines("E:\\wewew.xml");
}
private static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
ArrayList<String> arrStr = new ArrayList<String>();
ArrayList<String> errStr = new ArrayList<String>();
try {
System.out.println("以行读文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;// 每次读入都存在一个temp中
int line = 1;
while ((tempString = reader.readLine()) != null) {
if (-1 != tempString.indexOf("zhaoyueplc")) {
//如果此行有“zhaoyueplc”的字样,将其存入errStr
System.err.println("line " + line + ": " + tempString);
errStr.add(tempString);
} else {
//没有的话,存到arrStr
arrStr.add(tempString);
}
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
BufferedWriter output = null;
try {
// arrStr写到result.xml
output = new BufferedWriter(new FileWriter("E:\\result.xml"));
for (String str : arrStr) {
output.write(str + "\t");
}
output.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e1) {
}
}
}
BufferedWriter output1 = null;
try {
//errStr写到”deletedItem中“
output1 = new BufferedWriter(new FileWriter("E:\\deletedItem.txt"));
for (String str : errStr) {
output1.write(str + "\n\t");
}
output1.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output1 != null) {
try {
output1.close();
} catch (IOException e1) {
}
}
}
}
}