检索文件内容,并将含有要检索内容的文件及目录结构拷贝到指定目录下(源代码)
在网上找了好半天都没有找到相关的小工具,最后没有办法了,自己动手写了一个,留作以后用。
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();
}
}
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();
}
}