获取文件大小

/**
 * 获取文件大小
 *
 * @param targetFile
 * @return
 */
private Long getFileSize(String targetFile) {

   FileChannel fc = null;
   long size = 0;
   try {
      File f = new File(targetFile);
      if (f.exists() && f.isFile()) {
         FileInputStream fis = new FileInputStream(f);
         fc = fis.getChannel();
         size = fc.size();
         log.debug(String.valueOf(size));
         log.debug(String.valueOf(size / 1024));
      } else {
         log.debug("file doesn't exist or is not a file");
      }
   } catch (FileNotFoundException e) {
      e.getStackTrace();
   } catch (IOException e) {
      e.getStackTrace();
   } finally {
      if (null != fc) {
         try {
            fc.close();
         } catch (IOException e) {
            e.getStackTrace();
         }
      }
   }
   return size;
}

猜你喜欢

转载自blog.csdn.net/flyer_tang/article/details/80269868