簡單的FileUtil工具類方法

 

IO模型

阻塞IO(BIO)同步堵塞I/O模式,數據的讀取寫入必須堵塞在一個線程內等待其完成

    一排水壺燒水,一個線程停留在一個水壺那知道燒開,纔去處理下一個水壺。期間等待

非堵塞IO(NIO)同步支持堵塞與費堵塞模式,但是主要使用同步費堵塞IO

    一個線程不斷的輪詢每個水壺的狀態,看是否水壺狀態發生了改變,從而進行下一步操作

異步IO(AIO)異步非堵塞I/O模型

    爲每個水壺上裝了一個開關,燒開之後自動通知。

 

 

文件的複製導入到新的文件夾

 複製文件時 有時候需要將文件複製到一個新的自定義文件夾,這時候找不到指定路徑,就需要提前創建一個新的目錄。

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;
            }

 

 

文件導出方法(1)

 public void exec() {
      OutputStream outputStream = null;
      try{
          outputStream = getResponseStream(fileName);
          workbook.write(outputStream);
          outputStream.flush();
      }catch (IOException e){
          logger.error("", e);
          throw new ActionException("文件導出失敗");
      } finally {
          IOUtils.closeQuietly(outputStream);
      }
  }

  public static OutputStream getResponseStream(String fileName) throws IOException {
String file = URLEncoder.encode(fileName + ".xls", "UTF-8").replaceAll("\\+", "%20");
HttpServletResponse response = SkyEye.getResponse();
      response.reset();
      response.setCharacterEncoding("UTF-8");
      response.addHeader("Content-Disposition",
              "attachment; filename=\"" + file + "\"; filename*=utf-8''" + file);
      response.setContentType("application/vnd.ms-excel");
      return response.getOutputStream();
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章