java 폴더 아래의 모든 파일을 읽고 줄마다 지정한 문자열 바꾸기

6877 단어 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();

		}

	}

}

 

좋은 웹페이지 즐겨찾기