package com.test.mytest.study; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Demo001 { public static void writeFile(String strSrc) { FileWriter writer = null; FileReader reader = null; BufferedReader br = null; BufferedWriter bw = null; try { reader = new FileReader(strSrc); File filestrSrc = new File(strSrc); String str = null; br = new BufferedReader(reader); StringBuffer sb = new StringBuffer(""); while ((str = br.readLine()) != null) { String a= str.replaceAll(".ff(.*)}",""); a=a.replaceAll("\\@font-face\\{font-family",""); //空行不读入新文件里面。 if(!"".equals(a)){ sb.append(a + "\n"); } } filestrSrc.delete(); File file = new File(strSrc); if (!file.exists()) { file.createNewFile(); } // 这里的true,是以追加的方式写 writer = new FileWriter(strSrc,true); bw = new BufferedWriter(writer); bw.write(sb.toString()); br.close(); reader.close(); bw.close(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Demo001.writeFile("/home/xueqy/html.css"); } }