FileUtil工具类方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21325705/article/details/82994068

文件的复制导入到新的文件夹

 复制文件时 有时候需要将文件复制到一个新的自定义文件夹,这时候找不到指定路径,就需要提前创建一个新的目录。

file1.mkdirs() 利用这个创建好全目录后 再创建文件。再移动大量文件时,单线程导致效率低下,采用多线程的线程池,有效提高效率,先创建线程池,然后再循环中执行线程池的单个线程方法。
    /**
     * 读取某个文件夹下的所有文件
     */
    public static ArrayList<Map<String,Object>> readfile(String filepath)   {
        ArrayList<Map<String,Object>> list= new ArrayList();

            File file = new File(filepath);
            if (!file.isDirectory()) {
                System.out.println("文件");
                System.out.println("path=" + file.getPath());
                System.out.println("absolutepath=" + file.getAbsolutePath());
                System.out.println("name=" + file.getName());
                if(file.getPath().split("-").length>=3) {
                    Map map=new HashMap();
                    map.put("phone", file.getPath().split("_")[5].substring(1,12));
                    map.put("path", file.getAbsolutePath());
                    list.add(map);
                }

            } else if (file.isDirectory()) {
                System.out.println("文件夹");
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        System.out.println("path=" + readfile.getPath());
                        System.out.println("absolutepath="
                                + readfile.getAbsolutePath());
                        System.out.println("name=" + readfile.getName());
                        if(readfile.getPath().split("-").length>=3){
//                        if(readfile.getPath().indexOf("_") !=-1){
                            Map map=new HashMap();
//                            map.put("phone",readfile.getPath().split("_")[2].substring(0,readfile.getPath().split("_")[2].length()-4) );
                            map.put("phone",readfile.getPath().split("_")[5].substring(1,12));

                            map.put("path", readfile.getAbsolutePath());
                            list.add(map);
                        }


                    } else if (readfile.isDirectory()) {
                        readfile(filepath + "\\" + filelist[i]);
                    }
                }

            }


        return list;
    }
/**复制文件的方法*/
public static void copyFile(String oldPath, String filePath,String newPath) {
    try {
        int bytesum = 0;
        int byteread = 0;
        File oldfile = new File(oldPath);
        if (oldfile.exists()) { //文件存在时
            InputStream inStream = new FileInputStream(oldPath); //读入原文件

            File file1=new File(filePath);

            Boolean bbc=file1.mkdirs();// ture

            FileOutputStream fs = new FileOutputStream(newPath);
            byte[] buffer = new byte[1444];
            while ( (byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread; //字节数 文件大小
                System.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
            fs.close();
        }
    }
    catch (Exception e) {
        System.out.println("复制单个文件操作出错");
        e.printStackTrace();
    }
}
ExecutorService executorService = null;

List<Map<String,Object>> applys = Dao.selectList(MAPPER+"sele");
if(CollectionUtils.isEmpty(applys)){
    return 0;
}
executorService = Executors.newFixedThreadPool(applys.size() > 20 ? 20 : applys.size());
for(final Map<String,Object> apply : applys){

    final String path =  apply.get("PATH").toString();
    final String idcardno =  apply.get("IDCARDNO").toString();
    final String name=path.split("\\\\")[4];

    executorService.execute(new Runnable() {
        @Override
        public void run() {
             
            ICBCListAction.copyFile(path,"D:\\电核信审录音\\录音\\"+idcardno+"\\U-电核录音\\U1-电核录音","D:\\电核信审录音\\录音\\"+idcardno+"\\U-电核录音\\U1-电核录音\\"+name);
        }
    });

}

//删除指定文件夹下所有文件
       //param path 文件夹完整绝对路径
          public static boolean delAllFile(String path) {
              boolean flag = false;
              File file = new File(path);
              if (!file.exists()) {
                return flag;
              }
              if (!file.isDirectory()) {
                return flag;
              }
              String[] tempList = file.list();
              File temp = null;
              for (int i = 0; i < tempList.length; i++) {
                 if (path.endsWith(File.separator)) {
                    temp = new File(path + tempList[i]);
                 } else {
                     temp = new File(path + File.separator + tempList[i]);
                 }
                 if (temp.isFile()) {
                    temp.delete();
                 }
                 if (temp.isDirectory()) {
                    delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                    flag = true;
                 }
              }
              return flag;
            }

猜你喜欢

转载自blog.csdn.net/qq_21325705/article/details/82994068
今日推荐