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

复制自http://www.jeromechan.com/java/java读取文件夹下所有文件并替换文件每一行中指定/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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("/tmp/source.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("/tmp/source.txt");//替换后输出的文件位置
            printWriter.write(strBuffer.toString().toCharArray());
            printWriter.flush();
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

适用:如服务器崩溃 导致文件丢失,还原后类文件在每一行的开头都加了很多注释(如下)

1
2
3
4
5
/*     */ package com.jeromechan.iesap.starquery.dao;
/*     */
/*     */ import com.jeromechan.framework.impl.ThreadContext;
/*     */ import com.jeromechan.framework.persistence.AbstractDao;
.........很多很多.....

替换之后就是这样的:

1
2
3
4
package com.jeromechan.iesap.starquery.dao;
import com.jeromechan.framework.impl.ThreadContext;
import com.jeromechan.framework.persistence.AbstractDao;
.........很多很多......

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 = "/tmp/source/";//给我你的目录文件夹路径
            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("/tmp/source/"+filename);//替换后输出的文件位置(切记这里的/tmp/source 在你的本地必须有这个文件夹)
                    printWriter.write(strBuffer.toString().toCharArray());
                    printWriter.flush();
                    printWriter.close();
                    System.out.println("ok 第 " + (i+1) +" 个文件操作成功!");
                    //////// 读取兵输出一个文件结束  ////////
                }
                System.out.println("---------- 所有文件操作完毕 ----------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这样更加清晰明了些:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class ReadFile {
    public static void main(String[] args) {
        String filePath = "/tmp/source"; //给我你要读取的文件夹路径
        File outPath = new File("/tmp/destination/"); //随便给一个输出文件夹的路径(不存在也可以)
        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();
        }
    }
}

 

posted @ 2014-03-25 21:53  天行健小区  阅读(434)  评论(0编辑  收藏  举报