取得文件夹下的所有文件的文件名和文件大小

来源:互联网 发布:excel表格数据统计分类 编辑:程序博客网 时间:2024/05/30 04:59

 /**
  * 取得备份文件名称
  */
 public List<DbBackupDto> getFileList(){
  String achievePath = PropertyUtil.getProperty(Constants.FIBER_AUTODBBACKUP_FILEPATH);
  List<FileDto> fileDtoList = new ArrayList<FileDto>();
  File file = new File(achievePath);
  String[] fileList = file.list();
  
  if(fileList!=null&&fileList.length>0){
   for(String str : fileList){
    FileDto dto = new FileDto();
    dto.setFileName(str);

    File f = new File(achievePath + str);
    if(f.exists()){
     FileInputStream fis;
     try {
      fis = new FileInputStream(f);
      dto.setFileSize(String.valueOf(fis.available()));
     } catch (FileNotFoundException e) {
      e.printStackTrace();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
    fileDtoList.add(dto);
   }   
  }
  
  // 倒序排序
  Collections.sort(fileDtoList, new FileListSortDto());
  return fileDtoList;
 }

 /**
  * list排序 倒序 按照文件名称
  * @author weij
  */
 private class FileListSortDto implements Comparator<Cat>{
  public int compare(Cat o1, Cat o2) {
   return o2.getAge().compareToIgnoreCase(o1.getAge());
  }
 }

原创粉丝点击