- import java.io.*;
- import java.util.Date;
- import java.text.SimpleDateFormat;
- public class TestFindFile{
- // 目标目录
- private static String DPATH = "C:\\Users\\XJ\\Desktop\\__xujian__";
- // 源目录
- private static String SPATH = "D:\\Workspace\\bigboss";
- // 检索在此时间之后的文件
- private static String DTIME = "2013-06-27 11:10:00";
- public static void main(String [] args) throws Exception{
- if (SPATH.equals(DPATH)) {return;}
- File fs = new File(SPATH);
- File fd = new File(DPATH);
- TestFindFile.DPATH = DPATH + "\\" +fs.getName();
- TestFindFile.deleteDir(fd, true);
- if (fd.mkdirs()) {
- System.out.println("create root dir ok!");
- }
- try{
- TestFindFile.createDirAndFile(fs);
- } catch (Exception ex) {
- System.out.println("create error!");
- }
- System.out.println("create ok!");
- try{
- TestFindFile.deleteDir(fd, false);
- } catch (Exception ex) {
- System.out.println("delete error!");
- }
- System.out.println("delete ok!");
- }
- private static boolean createDirAndFile(File dir) throws Exception {
- if (dir.isDirectory()) {
- // 如果是目录,首先创建这个目录,然后递归调用该方法于这个目录下的所有文件
- String need_create_dir = DPATH + dir.getAbsolutePath().replace(SPATH,"");
- new File(need_create_dir).mkdirs();
- File[] children = dir.listFiles();
- for (int i=0; i<children.length; i++) {
- TestFindFile.createDirAndFile(children[i]);
- }
- } else {
- // 如果是文件则按要求创建(java中lastModified也能返回创建时间)
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date filedate =new Date(dir.lastModified());
- Date searchDate = df.parse(TestFindFile.DTIME);
- long diff = filedate.getTime() - searchDate.getTime();
- if (diff >= 0) {
- System.out.println("searchDate:" + searchDate);
- System.out.println("src:" + filedate);
- String need_create_file = DPATH + dir.getAbsolutePath().replace(SPATH,"");
- try{
- TestFindFile.copyFile(dir, new File(need_create_file));
- }catch(IOException ex){}
- }
- }
- return true;
- }
- private static boolean deleteDir(File dir, boolean removeALl) {
- if (dir.isDirectory()) {
- String[] children = dir.list();
- // 本身就是空目录--删去
- if (children.length == 0) {
- return dir.delete();
- }
- // 1、处理dir的子文件和目录
- for (int i=0; i<children.length; i++) {
- boolean success = deleteDir(new File(dir, children[i]), removeALl);
- if (!success) {
- return false;
- }
- }
- // 2、回到当前目录,如果经过第一步处理后,本目录是空的,那么也要删去
- if (dir.list().length == 0) {
- return dir.delete();
- }
- }
- if (removeALl) {
- return dir.delete();
- } else {
- return true;
- }
- }
- // 复制文件
- public static void copyFile(File sourceFile, File targetFile) throws IOException {
- BufferedInputStream inBuff = null;
- BufferedOutputStream outBuff = null;
- try {
- // 新建文件输入流并对它进行缓冲
- inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
- // 新建文件输出流并对它进行缓冲
- outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
- // 缓冲数组
- byte[] b = new byte[1024 * 5];
- int len;
- while ((len = inBuff.read(b)) != -1) {
- outBuff.write(b, 0, len);
- }
- // 刷新此缓冲的输出流
- outBuff.flush();
- } finally {
- // 关闭流
- if (inBuff != null) {
- inBuff.close();
- }
- if (outBuff != null) {
- outBuff.close();
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2010201410717.html
来源: http://www.codesnippet.cn/detail/2010201410717.html