import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class ChangeFile { public static void main(String[] args) { try { BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/EntNatureDistributionDAO.txt"))));//數據流讀取文件 StringBuffer strBuffer = new StringBuffer(); String empty = ""; String tihuan = ""; for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) { if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判斷當前行是否存在想要替換掉的字符 -1表示存在 tihuan = temp.substring(0, 10); temp = temp.replace(tihuan, empty);//替換爲你想要的東東 } strBuffer.append(temp); strBuffer.append(System.getProperty("line.separator"));//行與行之間的分割 } bufReader.close(); PrintWriter printWriter = new PrintWriter("E:/EntNatureDistributionDAO.txt");//替換後輸出的文件位置 printWriter.write(strBuffer.toString().toCharArray()); printWriter.flush(); printWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
適用:如服務器崩潰 致使文件丟失,還原後類文件在每一行的開頭都加了不少註釋(以下)java
/* */ package com.itown.iesap.starquery.dao; /* */ /* */ import com.itown.framework.impl.ThreadContext; /* */ import com.itown.framework.persistence.AbstractDao; .........不少不少.....
替換以後就是這樣的:服務器
package com.itown.iesap.starquery.dao; import com.itown.framework.impl.ThreadContext; import com.itown.framework.persistence.AbstractDao; .........不少不少......
若是你又成百上千個這樣的文件替換那就要讀取文件夾下的全部文件:app
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.PrintWriter; public class ChangeFile { public static void main(String[] args) { try { //讀取指定文件夾下的全部文件 String filepath = "D:/AOE/abc/";//給我你的目錄文件夾路徑 File file = new File(filepath); if (!file.isDirectory()) { System.out.println("---------- 該文件不是一個目錄文件 ----------"); } else if (file.isDirectory()) { System.out.println("---------- 很好,這是一個目錄文件夾 ----------"); String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filepath + "\\" + filelist[i]); //String path = readfile.getPath();//文件路徑 String absolutepath = readfile.getAbsolutePath();//文件的絕對路徑 String filename = readfile.getName();//讀到的文件名 //////// 開始挨個的讀取文件 //////// BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(absolutepath)));//數據流讀取文件 StringBuffer strBuffer = new StringBuffer(); String empty = ""; String tihuan = ""; for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) { if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判斷當前行是否存在想要替換掉的字符 -1表示存在 tihuan = temp.substring(0, 9);//這裏截取多長本身改 temp = temp.replace(tihuan, empty);//替換爲你想要的東東 } strBuffer.append(temp); strBuffer.append(System.getProperty("line.separator"));//行與行之間的分割 } bufReader.close(); PrintWriter printWriter = new PrintWriter("E:/ttt/"+filename);//替換後輸出的文件位置(切記這裏的E:/ttt 在你的本地必須有這個文件夾) printWriter.write(strBuffer.toString().toCharArray()); printWriter.flush(); printWriter.close(); System.out.println("ok 第 " + (i+1) +" 個文件操做成功!"); //////// 讀取兵輸出一個文件結束 //////// } System.out.println("---------- 全部文件操做完畢 ----------"); } } catch (Exception e) { e.printStackTrace(); } } }
這樣更加清晰明瞭些:測試
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.PrintWriter; public class ReadFile { /** * 主方法測試 * @param args * @author 杜文俊 * @update 2013-6-26 下午1:36:31 */ public static void main(String[] args) { String filePath = "D:/AOE/abc/"; //給我你要讀取的文件夾路徑 File outPath = new File("E:/AOE/abc/"); //隨便給一個輸出文件夾的路徑(不存在也能夠) readFolder(filePath,outPath); } /** * 讀取文件夾 * @return */ public static void readFolder(String filePath,File outPath){ try { //讀取指定文件夾下的全部文件 File file = new File(filePath); if (!file.isDirectory()) { System.out.println("---------- 該文件不是一個目錄文件 ----------"); } else if (file.isDirectory()) { System.out.println("---------- 很好,這是一個目錄文件夾 ----------"); String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++) { File readfile = new File(filePath + "\\" + filelist[i]); //String path = readfile.getPath();//文件路徑 String absolutepath = readfile.getAbsolutePath();//文件的絕對路徑 String filename = readfile.getName();//讀到的文件名 readFile(absolutepath,filename,i,outPath);//調用 readFile 方法讀取文件夾下全部文件 } System.out.println("---------- 全部文件操做完畢 ----------"); } } catch (Exception e) { e.printStackTrace(); } } /** * 讀取文件夾下的文件 * @return */ public static void readFile(String absolutepath,String filename,int index,File outPath){ try{ BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(absolutepath)));//數據流讀取文件 StringBuffer strBuffer = new StringBuffer(); String empty = ""; String tihuan = ""; for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) { if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判斷當前行是否存在想要替換掉的字符 -1表示存在 tihuan = temp.substring(0, 9);//這裏截取多長本身改 temp = temp.replace(tihuan, empty);//替換爲你想要的東東 } strBuffer.append(temp); strBuffer.append(System.getProperty("line.separator"));//行與行之間的分割 } bufReader.close(); if(outPath.exists() == false){ //檢查輸出文件夾是否存在,若不存在先建立 outPath.mkdirs(); System.out.println("已成功建立輸出文件夾:" + outPath); } PrintWriter printWriter = new PrintWriter(outPath+"\\"+filename);//替換後輸出的文件位置(切記這裏的E:/ttt 在你的本地必須有這個文件夾) printWriter.write(strBuffer.toString().toCharArray()); printWriter.flush(); printWriter.close(); System.out.println("第 " + (index+1) +" 個文件 "+ absolutepath +" 已成功輸出到 " +outPath+"\\"+filename); }catch(Exception e){ e.printStackTrace(); } } }