Java写的检索文件&合并文件功能
This give you some example for scanning files with specified string or specified file type from your pointed folder directory.You can scan the files and copy them to a temporary folder, also you can merge them together into one file.[java]package com_2013;import java.io.*;import java.util.*;public class FileScan {static String fromdir = "Input the folder directory where you need to scan the files.";static String todir = "Input the folder directory where you need copy the scanned files to.";static String findstr = "Input the string that is specified scanned condition.";static String targetFileName = "D:/targetFileName.sql";static String filetype = ".sql";static int filenum = 1;static File outFile;static RandomAccessFile outt;public static void main(String[] args) {File filedir = new File(fromdir);FileScan FS = new FileScan();// Function 1: Scan the files and copy them to a folder.// File toDir = new File(todir);// if (!toDir.isDirectory()) {// toDir.mkdirs();// }// FS.fileScan(filedir);// Function 2: Scan the files and merge them into one file.try {outFile = new File(targetFileName);if (!outFile.exists())outFile.createNewFile();outt = new RandomAccessFile(outFile,"rw");FS.fileScanUnite(filedir);outt.close();} catch (Exception e) {e.printStackTrace();}}public FileScan() {}/*** Scan the specified files from the specified folder and unite them together into one file.* @param fileDir*/public void fileScanUnite(File fileDir) throws Exception {File[] file = fileDir.listFiles();for(int i = 0; i < file.length; i ++) {File subfile = file[i];if (subfile.isDirectory()) {fileScanUnite(subfile);}else {String temp = subfile.getName().toUpperCase();if ( temp.indexOf(findstr.toUpperCase())>0 && temp.endsWith(filetype.toUpperCase()) ) {RandomAccessFile inn = new RandomAccessFile(subfile,"r");int c;while ((c=inn.read()) != -1)outt.write(c);System.out.println("Merge file: " + subfile.getPath());}}}}/*** Scan the files from the specified folder* @param filedir*/public void fileScan(File filedir) {File[] file = filedir.listFiles();for(int i = 0; i < file.length; i ++) {File subfile = file[i];if (subfile.isDirectory()) {fileScan(subfile);}else {String temp = subfile.getName().toUpperCase();if ( temp.indexOf(findstr.toUpperCase())>0 && temp.endsWith(filetype.toUpperCase()) ) {try {fileCopy(subfile, new File(todir + findstr + "_" + String.valueOf(filenum++) + filetype ));} catch (Exception e) {e.printStackTrace();}finally {System.out.println("Copy file: " + subfile.getPath());}}}}}/*** Copy the file from f1 to f2sa* @param f1* @param f2* @return* @throws Exception补充:软件开发 , Java ,
上一个:java 类的声明
下一个:用eclipse创建多模块maven项目
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值