Java 替换文件夹下所有文件中指定的内容

来源:互联网 发布:同花顺mac版功能全吗 编辑:程序博客网 时间:2024/05/02 04:36

我的上一篇博客提到了找到文件中的中文。之前的代码还可以做一个扩展。可以作为一个强大的查询功能使用。关键字查询,这个功能大家可以思考一下,今后我的博客也会去贴上这样的关键字查询的功能的代码,能跟大家讨论分析一下。
今天的替换的功能也是基于上篇博客的代码做的一个修改,谈不上什么强大,这个功能性的需求也不是很常见。今天也是跟大家做一个分享吧!

public static void main(String[] args) {        //读取指定文件夹下的所有文件        String filepath = "F:/wk/";//给我你的目录文件夹路径        File file = new File(filepath);        if (!file.isDirectory()) {            System.out.println("请输入一个目录文件路径");        } else if (file.isDirectory()) {            try {                refreshFileList(filepath);            } catch (IOException e) {                e.printStackTrace();            }          }    }    //递归查找文件      private static void refreshFileList(String strPath) throws IOException {         File files = new File(strPath);          File[] filelist = files.listFiles();        if (filelist == null)              return;        for (int i = 0; i < filelist.length; i++) {            if (filelist[i].isDirectory()) {                  refreshFileList(filelist[i].getAbsolutePath());              } else {                String filename = files.getName();//读到的文件名                String strFileName = filelist[i].getAbsolutePath().toLowerCase();                  String FileNamePath = strFileName.substring(6, strFileName.length());                //截取文件格式                  String  SufName = strFileName.substring(strFileName.lastIndexOf(".")+1,strFileName.length());                  //排除不需要扫描的文件                if(SufName.equals("rar") || SufName.equals("jpg") || SufName.equals("png") || SufName.equals("jar") || SufName.equals("doc") || SufName.equals("xls") || SufName.equals("gif") || SufName.equals("wmz")){                    continue;                }                //不知为何  两种方法判断的时候都会吧class文件和jar文件当做是含有中文字符的文件                  //所以此处排除掉这class文件和jar文件不参与判断                  if(!"class".equals(SufName.toLowerCase())){                      //开始输入文件流,检查文件                      String enCode = getFileEncode(filelist[i].getAbsolutePath());                      if("void".equals(enCode)){                          enCode="UTF-8";                      }if("windows-1252".equals(enCode)){                          enCode="GBK";                      }                      FileInputStream fis = new FileInputStream(filelist[i].getAbsolutePath());                      InputStreamReader in = new InputStreamReader(fis,enCode);                      BufferedReader br = new BufferedReader(in);                     StringBuffer strBuffer = new StringBuffer();                    String line = null;                       while((line = br.readLine())!=null){                           Pattern p = Pattern.compile("([\u4e00-\u9fa5]+)");                             String mv = "";                      //正则判断                      Matcher m = p.matcher(line);                       //遍历含有中文的行。并取出中文                      while (m.find()) {                             mv += m.group(0);                          }                         Map<String, String> map = new HashMap<String, String>();                        map.put("小康", "xk");                        Set<Entry<String, String>> entries = map.entrySet();                        for (Entry<String, String> mapKey : entries) {                              if(line.indexOf(mapKey.getKey()) != -1){ //判断当前行是否存在想要替换掉的字符 -1表示存在                                line = line.replace(mv, mapKey.getValue());//替换为你想替换的内容                            }                        }                        strBuffer.append(line);                        strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割                     }                     br.close();                     //判断文件夹是否存在。没有就新建文件夹                     File file=new File("F:/wangkang/"+filename);                         if(!file.exists())                         {                             file.mkdirs();                        }                      PrintWriter printWriter = new PrintWriter("F:/xiaokang/"+FileNamePath);//替换后输出的文件位置                     printWriter.write(strBuffer.toString().toCharArray());                     printWriter.flush();                     printWriter.close();                     System.out.println("ok 第 " + (i+1) +" 个文件操作成功!");                }            }        }    }       //检查文件类型      public static String getFileEncode(String path) {          /*          * detector是探测器,它把探测任务交给具体的探测实现类的实例完成。          * cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、          * JChardetFacade、ASCIIDetector、UnicodeDetector。          * detector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的          * 字符集编码。使用需要用到三个第三方JAR包:antlr.jar、chardet.jar和cpdetector.jar          * cpDetector是基于统计学原理的,不保证完全正确。          */          CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();          /*          * ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于          * 指示是否显示探测过程的详细信息,为false不显示。          */          detector.add(new ParsingDetector(false));          /*          * JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码          * 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以          * 再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。          */          detector.add(JChardetFacade.getInstance());// 用到antlr.jar、chardet.jar          // ASCIIDetector用于ASCII编码测定          detector.add(ASCIIDetector.getInstance());          // UnicodeDetector用于Unicode家族编码的测定          detector.add(UnicodeDetector.getInstance());          java.nio.charset.Charset charset = null;          File f = new File(path);          try {              charset = detector.detectCodepage(f.toURI().toURL());          } catch (Exception ex) {              ex.printStackTrace();          }          if (charset != null)              return charset.name();          else              return null;      }  

在结尾在加一句,这里运用了Java基础的小知识。Map的循环。
最开始做Map的循环是将Map的Key给取出来。然后在从Key中找Value,最近才发现Map里面还有一个Entry接口。这个接口很好的完成一个Map集合的循环以及对Key-Value的取值工作。
本文中所需的Jar包在我的上一篇博客评论中有链接。

2 0
原创粉丝点击