检索文件内容,并将含有要检索内容的文件及目录结构拷贝到指定目录下(源代码)...

在网上找了好半天都没有找到相关的小工具,最后没有办法了,自己动手写了一个,留作以后用。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.duxiu.app;

import  java.io.BufferedInputStream;
import  java.io.BufferedOutputStream;
import  java.io.BufferedReader;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.io.OutputStream;
import  java.nio.channels.FileChannel;
import  java.util.regex.Matcher;
import  java.util.regex.Pattern;

public   class  FileUtil {

    
private  String fileName; //  要检索的文件名称,可以是正则表达式
     private  String path; //  源文件(要检索的)路径
     private  String copyToPath; //  检索后,要复制到目的路径
     private  String fileEncode; //  要检索文件的编码
     private  String searchContent; //  要检索文件的内容,可以是正则表达式

    
public  FileUtil(String fileName, String fileEncode, String searchContent, String path, String copyToPath) {
        
this .fileName  =  fileName;
        
this .fileEncode  =  fileEncode;
        
this .path  =  path;
        
this .searchContent  =  searchContent;
        
if  ( ! copyToPath.endsWith( " \\ " &&   ! copyToPath.endsWith( " / " )) {
            copyToPath 
+=   " \\ " ;
        }
        
this .copyToPath  =  copyToPath;
    }

    
public   void  DrawFiles() {
        File file 
=   new  File(path);
        
if  ( ! file.exists()) {
            
return ;
        }

        File[] fileLists 
=  file.listFiles();
        searchFile(fileLists);
    }

    
/**
     * 递归检索文件
     * 
     * 
@param  fileLists
     
*/
    
protected   void  searchFile(File[] fileLists) {
        
if  (fileLists  ==   null   ||  fileLists.length  <   1 ) {
            
return ;
        }

        
for  (File file : fileLists) {
            
if  (file.isDirectory()) { //  是目录,进行递归循环
                File[] files  =  file.listFiles();
                searchFile(files);
            }

            
if  (file.isFile()) { //  是文件,匹配要检索的文件名
                String fn  =  file.getName();
                Pattern pattern 
=  Pattern.compile(fileName, Pattern.CASE_INSENSITIVE);
                Matcher matcher 
=  pattern.matcher(fn);
                
if  (matcher.find()) {
                    
if  (findFileContent(file)) {
                        String srcPath 
=  file.getAbsolutePath();
                        srcPath 
=  srcPath.substring(srcPath.indexOf( " \\ " +   1 , srcPath.lastIndexOf( " \\ " +   1 );
                        File dstFile 
=   new  File(copyToPath  +  srcPath);
                        
if  ( ! dstFile.exists()) {
                            dstFile.mkdirs();
                        }
                        dstFile 
=   new  File(copyToPath  +  srcPath  +  file.getName());
                        copyFile2(file, dstFile);
                    }
                }
            }
        }
    }

    
/**
     * 检索文件内容
     * 
     * 
@param  file
     * 
@return
     
*/
    
protected   boolean  findFileContent(File file) {
        
boolean  isFind  =   false ;
        
try  {
            FileInputStream fileInput 
=   new  FileInputStream(file);
            InputStreamReader inputStrReader 
=   new  InputStreamReader(fileInput, fileEncode);
            BufferedReader buffereReader 
=   new  BufferedReader(inputStrReader);
            StringBuilder sb 
=   new  StringBuilder();
            String line 
=   "" ;
            
while  ((line  =  buffereReader.readLine())  !=   null ) {
                sb.append(line).append(
" \r\n " );
            }
            buffereReader.close();
            inputStrReader.close();
            fileInput.close();

            Pattern pattern 
=  Pattern.compile(searchContent, Pattern.CASE_INSENSITIVE);
            Matcher matcher 
=  pattern.matcher(sb);
            
if  (matcher.find()) {
                isFind 
=   true ;
            }
        } 
catch  (Exception e) {
            System.out.println(file.getAbsolutePath() 
+   " / "   +  file.getName()  +   "   read error: "   +  e.getMessage());
        }

        
return  isFind;
    }

    
/**
     * 拷贝文件
     * 
     * 
@param  src
     * 
@param  dest
     
*/
    
public   void  copyFile2(File src, File dest) {
        
try  {
            
//  Create channel on the source
            FileChannel srcChannel  =   new  FileInputStream(src).getChannel();

            
//  Create channel on the destination
            FileChannel dstChannel  =   new  FileOutputStream(dest).getChannel();

            
//  Copy file contents from source to destination
            dstChannel.transferFrom(srcChannel,  0 , srcChannel.size());

            
//  Close the channels
            srcChannel.close();
            dstChannel.close();
        } 
catch  (IOException e) {
            e.printStackTrace();
        }

    }

    
/**
     * 对UTF-8编辑的文件拷贝有问题
     * 
     * 
@param  src
     * 
@param  dst
     
*/
    @Deprecated
    
public   void  copyFile(File src, File dst) {
        
int  BUFFER_SIZE  =   1024   *   2 ;
        
try  {
            InputStream in 
=   null ;
            OutputStream out 
=   null ;
            
try  {
                in 
=   new  BufferedInputStream( new  FileInputStream(src), BUFFER_SIZE);
                out 
=   new  BufferedOutputStream( new  FileOutputStream(dst), BUFFER_SIZE);
                
byte [] buffer  =   new   byte [BUFFER_SIZE];
                
while  (in.read(buffer)  >   0 ) {
                    out.write(buffer);
                }
            } 
finally  {
                
if  ( null   !=  in) {
                    in.close();
                }
                
if  ( null   !=  out) {
                    out.close();
                }
            }
        } 
catch  (Exception e) {
            e.printStackTrace();
        }
    }

    
public  String getFileName() {
        
return  fileName;
    }

    
public   void  setFileName(String fileName) {
        
this .fileName  =  fileName;
    }

    
public  String getPath() {
        
return  path;
    }

    
public   void  setPath(String path) {
        
this .path  =  path;
    }

    
public  String getCopyToPath() {
        
return  copyToPath;
    }

    
public   void  setCopyToPath(String copyToPath) {
        
this .copyToPath  =  copyToPath;
    }

    
public   static   void  main(String[] args) {
        String fileName 
=   " index.jsp " ;
        String path 
=   " F:\\project\\eclipse33workspace\\DuxiuAbo\\ROOT\\areas " ;
        String copyToPath 
=   " D:\\simone " ;
        String fileEncode 
=   " utf-8 " ;
        String searchContent 
=   " /javascript/area/inc.js " ;
        FileUtil fileUtil 
=   new  FileUtil(fileName, fileEncode, searchContent, path, copyToPath);
        fileUtil.DrawFiles();

    }
}

 

摘自:http://www.blogjava.net/wangxinsh55/archive/2009/11/11/301993.html

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值