Java中获取文件大小的正确方法

xiaoxiao2021-02-28  97

本文出处:http://blog.csdn.NET/chaijunkun/article/details/22387305,转载请注明。由于本人不定期会整理相关博文,会对相应内容作出完善。因此强烈建议在原始出处查看此文。

今天写代码时需要实现获取文件大小的功能,目前有两种实现方法,一种是使用File的length()方法;另外一种是使用FileInputStream的available()方法,当InputStream未进行read操作时,available()的大小应该是等于文件大小的。但是在处理大文件时,后者会发生问题。我们来看一下:

在例子中,我使用了CentOS 6.5 的安装镜像文件,主要是考虑到这个文件足够大(大于2GB)。

1.使用File的length()方法

[java] view plain copy print ? public static void main(String[] args) {      File f= new File(“D:\\CentOS-6.5-x86_64-bin-DVD1.iso”);      if (f.exists() && f.isFile()){          logger.info(f.length());      }else{          logger.info(”file doesn’t exist or is not a file”);      }  }   public static void main(String[] args) { File f= new File("D:\\CentOS-6.5-x86_64-bin-DVD1.iso"); if (f.exists() && f.isFile()){ logger.info(f.length()); }else{ logger.info("file doesn't exist or is not a file"); } }我们看一下输出结果:

[plain] view plain copy print ? 4467982336   4467982336

结果是4.16GB,与Windows上显示的结果一致。

接下来我们看一下通过FileInputStream来获取的文件大小:

[java] view plain copy print ? public static void main(String[] args) {      FileInputStream fis= null;      try{          File f= new File(“D:\\CentOS-6.5-x86_64-bin-DVD1.iso”);          fis= new FileInputStream(f);          logger.info(fis.available());      }catch(Exception e){          logger.error(e);      } finally{          if (null!=fis){              try {                  fis.close();              } catch (IOException e) {                  logger.error(e);              }          }      }  }   public static void main(String[] args) { FileInputStream fis= null; try{ File f= new File("D:\\CentOS-6.5-x86_64-bin-DVD1.iso"); fis= new FileInputStream(f); logger.info(fis.available()); }catch(Exception e){ logger.error(e); } finally{ if (null!=fis){ try { fis.close(); } catch (IOException e) { logger.error(e); } } } }下面是运行结果:

[plain] view plain copy print ? 2147483647   2147483647这个结果是不是很眼熟?它是Integer.MAX_VALUE,也就是有符号整型能表示的最大数值。

那么换算成熟悉的单位,这种方式获取的文件大小是多大呢?

约等于2GB,这显然不是正确的结果。 究其原因,File的length()方法返回的类型为long,long型能表示的正数最大值为:9223372036854775807,折算成最大能支持的文件大小为:8954730132868714 EB字节,这个量级将在人类IT发展史上受用很多很多年,而FileInputStream的avaliable()方法返回值是int,在之前也介绍了最大的表示范围,所能支持的最大文件大小为:1.99GB,而这个量级我们现在很容易就达到了。

2014年3月31日补充:

针对流式方法读取大文件大小也不是不可行,只是不能再使用传统的Java.io.*下的包了,这里要用到java.nio.*下的新工具——FileChannel。下面我们来看下示例代码:

[java] view plain copy print ? public static void main(String[] args) {      FileChannel fc= null;      try {          File f= new File(“D:\\CentOS-6.5-x86_64-bin-DVD1.iso”);          if (f.exists() && f.isFile()){              FileInputStream fis= new FileInputStream(f);              fc= fis.getChannel();              logger.info(fc.size());          }else{              logger.info(”file doesn’t exist or is not a file”);          }      } catch (FileNotFoundException e) {          logger.error(e);      } catch (IOException e) {          logger.error(e);      } finally {          if (null!=fc)){              try{                  fc.close();              }catch(IOException e){                  logger.error(e);              }          }       }  }   public static void main(String[] args) { FileChannel fc= null; try { File f= new File("D:\\CentOS-6.5-x86_64-bin-DVD1.iso"); if (f.exists() && f.isFile()){ FileInputStream fis= new FileInputStream(f); fc= fis.getChannel(); logger.info(fc.size()); }else{ logger.info("file doesn't exist or is not a file"); } } catch (FileNotFoundException e) { logger.error(e); } catch (IOException e) { logger.error(e); } finally { if (null!=fc)){ try{ fc.close(); }catch(IOException e){ logger.error(e); } } } } 使用FileChannel后得到的结果与第一种情况吻合,准确地描述了文件的准确大小。

这里也同样提醒各位技术同仁,涉及到大文件读取的时候,对int类型的数据一定要留个心,以免出现隐藏的bug,定位起来很困难。

转载请注明原文地址: https://www.6miu.com/read-78373.html

最新回复(0)