手机版
你好,游客 登录 注册
背景:
阅读新闻

Java写的检索文件&合并文件功能

[日期:2013-02-01] 来源:Linux社区  作者:lamar511 [字体:大 中 小]

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.

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
  */
 public long fileCopy(File f1,File f2) throws Exception{
  long time=new Date().getTime();
  int length=2097152;
  FileInputStream in=new FileInputStream(f1);
  FileOutputStream out=new FileOutputStream(f2);
  byte[] buffer=new byte[length];
  while(true){
   int ins=in.read(buffer);
   if(ins==-1){
   in.close();
   out.flush();
   out.close();
   return new Date().getTime()-time;
  }else
   out.write(buffer,0,ins);
  }
 }
 
}

linux
相关资讯       Java合并文件  Java文件检索 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/2/1 14:46:34
尼玛。。有没有搞错,我在CSDN上发表的博客怎么这边也有了?你们COPY的速度也太快了吧!!!!!!