package com.albertyy.other;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class RemoveId {
public static void main(String[] args) {
changeContent("F://tag.html", "tag.html");
}
/**
* 读取HTML文件并替换其中的特定内容,然后保存文件
* @param filePath读取文件路径
* @param savePath保存文件路径
*/
private static void changeContent(String filePath, String savePath) {
// filePath 要读取的文件 savePath 要写入的文件
BufferedReader br = null;
BufferedWriter bw = null;
try {
// 以下读取和写入都转成UTF-8 防止乱码
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savePath), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null && (line != "")) {
bw.write(line.replaceAll("id=\"\\S+\"", ""));
}
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null){
br.close();
}
if (bw != null){
bw.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}