当前位置:编程学习 > JAVA >>

java读取文件夹下所有文件并替换文件每一行中指定的字符串

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

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

替换之后就是这样的:

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

如果你又成百上千个这样的文件替换那就要读取文件夹下的所有文件:

[java] 
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++) { 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,