java讀取文件夾下所有文件並替換文件每一行中指定的字符串

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();
		}
	}
}

 

適用:如服務器崩潰 導致文件丟失,還原後類文件在每一行的開頭都加了很多註釋(如下)

/*     */ 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;
.........很多很多......

 

如果你又成百上千個這樣的文件替換那就要讀取文件夾下的所有文件

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();
		}
	}
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章