一、前言
定义文件替换ReplaceFileUtil工具类,实现对指定文件格式内容查找标记位置打印 、指定文件内容格式替换、指定文件格式的删除处理、删除文件指定格式所属行内容,详情参见代码示例。
二、代码示例
import java.io.BufferedReader;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStreamReader;@b@import java.io.PrintWriter;@b@import java.util.concurrent.atomic.AtomicInteger;@b@@b@public class ReplaceFileUtil {@b@ @b@ private static AtomicInteger atomic = new AtomicInteger(0);@b@@b@ public static void printlnFile(File file, String format) throws IOException {@b@ File[] arr$;@b@ int i$;@b@ if (file.isDirectory()) {@b@ File[] files = file.listFiles();@b@ arr$ = files;@b@ int len$ = arr$.length;@b@ for (i$ = 0; i$ < len$; ++i$) {@b@ File tempFile = arr$[i$];@b@ printlnFile(tempFile, format);@b@ }@b@ } else if ((file.isFile()) && (file.getName().endsWith(".java"))) {@b@ int index;@b@ String tmp1;@b@ BufferedReader bufReader = null;@b@ try {@b@ bufReader = new BufferedReader(new InputStreamReader(@b@ new FileInputStream(file)));@b@@b@ index = 1;@b@ for (tmp1 = null; (tmp1 = bufReader.readLine()) != null; tmp1 = null) {@b@ String tmp = new String(tmp1.toString().getBytes("UTF-8"));@b@@b@ if (tmp.contains(format)) {@b@ atomic.incrementAndGet();@b@ if (index > 3)@b@ System.out.print("index:" + index + "--");@b@@b@ ++index;@b@ System.out.println(file.getAbsolutePath() + ",content:"@b@ + tmp);@b@ break;@b@ }@b@ ++index;@b@ }@b@ } finally {@b@ if (bufReader != null)@b@ bufReader.close();@b@ }@b@ }@b@ }@b@@b@ public static void replace(File file, String format, String replace)@b@ throws IOException {@b@ File[] arr$;@b@ int i$;@b@ if (file.isDirectory()) {@b@ File[] files = file.listFiles();@b@ arr$ = files;@b@ int len$ = arr$.length;@b@ for (i$ = 0; i$ < len$; ++i$) {@b@ File tempFile = arr$[i$];@b@ replace(tempFile, format, replace);@b@ }@b@ } else if ((file.isFile()) && (file.getName().endsWith(".java"))) {@b@ replaceFile(file, format, replace);@b@ }@b@ }@b@@b@ public static void deleteLineOfDirectory(File file, String format)@b@ throws IOException {@b@ File[] arr$;@b@ int i$;@b@ if (file.isDirectory()) {@b@ File[] files = file.listFiles();@b@ arr$ = files;@b@ int len$ = arr$.length;@b@ for (i$ = 0; i$ < len$; ++i$) {@b@ File tempFile = arr$[i$];@b@ deleteLineOfDirectory(tempFile, format);@b@ }@b@ } else if ((file.isFile()) && (file.getName().endsWith(".java"))) {@b@ deleteLine(file, format);@b@ }@b@ }@b@@b@ public static void deleteLine(File file, String format) throws IOException {@b@ BufferedReader bufReader = null;@b@ PrintWriter printWriter = null;@b@ FileOutputStream fos = null;@b@ try {@b@ bufReader = new BufferedReader(new InputStreamReader(@b@ new FileInputStream(file)));@b@@b@ StringBuffer strBuf = new StringBuffer();@b@@b@ for (String tmp1 = null; (tmp1 = bufReader.readLine()) != null; tmp1 = null) {@b@ String tmp = new String(tmp1.toString().getBytes("UTF-8"));@b@ if (tmp.contains(format)) {@b@ System.out.println("del:" + file.getName());@b@ atomic.incrementAndGet();@b@ } else {@b@ strBuf.append(tmp);@b@ strBuf.append(System.getProperty("line.separator"));@b@ }@b@ }@b@ String outputStrBuf = new String(strBuf.toString()@b@ .getBytes("UTF-8"));@b@@b@ file.delete();@b@ file.createNewFile();@b@ fos = new FileOutputStream(file);@b@ printWriter = new PrintWriter(fos);@b@ printWriter.write(outputStrBuf.toString().toCharArray());@b@ printWriter.flush();@b@ } finally {@b@ if (printWriter != null)@b@ printWriter.close();@b@@b@ if (fos != null)@b@ fos.close();@b@ if (bufReader != null)@b@ bufReader.close();@b@ }@b@ }@b@@b@ public static void replaceFile(File file, String format, String replace)@b@ throws IOException {@b@ BufferedReader bufReader = null;@b@ PrintWriter printWriter = null;@b@ FileOutputStream fos = null;@b@ try {@b@ bufReader = new BufferedReader(new InputStreamReader(@b@ new FileInputStream(file)));@b@@b@ StringBuffer strBuf = new StringBuffer();@b@ for (String tmp1 = null; (tmp1 = bufReader.readLine()) != null; tmp1 = null) {@b@ String tmp = new String(tmp1.toString().getBytes("UTF-8"));@b@ tmp = tmp.replaceAll(format, replace);@b@ strBuf.append(tmp);@b@ strBuf.append(System.getProperty("line.separator"));@b@ atomic.incrementAndGet();@b@ }@b@@b@ String outputStrBuf = new String(strBuf.toString()@b@ .getBytes("UTF-8"));@b@@b@ file.delete();@b@ file.createNewFile();@b@ fos = new FileOutputStream(file);@b@ printWriter = new PrintWriter(fos);@b@ printWriter.write(outputStrBuf.toString().toCharArray());@b@ printWriter.flush();@b@ } finally {@b@ if (printWriter != null)@b@ printWriter.close();@b@@b@ if (fos != null)@b@ fos.close();@b@ if (bufReader != null)@b@ bufReader.close();@b@ }@b@ }@b@ @b@}