您的位置:首页 > 运维架构

将IE缓存中的文件copy到其它目录下

2007-11-06 17:36 239 查看
/**
 *
 */
package myweb.web;

/**
 * @author sxc 石新春
 *@Create date 2007-11-6
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class CopyFiles {
    // 源路径
    private static String SRC_DIREC = "C://Documents and Settings//zhangl//Local Settings//Temporary Internet Files";

    //文件类型
    private static String[] FILE_TYPE = {"mp3","bmp","gif","jpg","js","png","swf","asx","wma","wmv","css"};
    private static String CURR_FILE_TYPE;
    // 目的路径
    private static String DEST_DIREC_ROOT = "E://";
    private static String DEST_DIREC = "";

    private static int num = 0;
    // main方法
    public static void main(String[] args) {
        for(int i =0;i<FILE_TYPE.length;i++){
            DEST_DIREC = DEST_DIREC_ROOT+FILE_TYPE[i];
            CURR_FILE_TYPE = FILE_TYPE[i];
            copyDelete(args);//copy
        }
        System.out.println("操作完成");
    }

    /**
     * @param args
     */
    private static void copyDelete(String[] args) {
        // System.out.println("请输入要拷贝的文件路径及目的路径");
        if (args.length == 0) {
            args = new String[] { SRC_DIREC, DEST_DIREC };
        }
        File pathName = new File(args[0]);
        String[] fileNames = pathName.list();
        // 遍历文件夹,找出所要文件
        int i = 0;
        for (; i < fileNames.length; i++) {
            File f = new File(pathName.getPath(), fileNames[i]);
            if (f.isDirectory()) {
                //
                copyDelete(new String[] { f.getPath(), DEST_DIREC });
            } else if (f.getName().endsWith("."+CURR_FILE_TYPE)){
                num++;
                makeDirIfNotExist(args[1]);
                System.out.print(num+"、正在复制文件:" + f.getName() + " 到目录" + args[1]    + ".../t");
                copyfile(f.getAbsolutePath(), args[1]+"//" + f.getName());
                f.delete();
                System.out.println("复制成功。源文件删除成功。");
            }
        }
    }

    /**
     * 复制文件到新路径下
     *
     * @param src
     * @param dest
     */
    private static void copyfile(String src, String dest) {
        try {
            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 (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void makeDirIfNotExist(String adir){
        File dir = new File(adir);
        if(!dir.exists()){
            System.out.println("文件夹 "+adir+" 不存在,创建之");
            dir.mkdir();
            }
    }
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息