这篇文章主要介绍了java获取文件大小的几种方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
目前Java获取文件大小的方法有两种:
1、通过file的length()方法获取;
2、通过流式方法获取;
通过流式方法又有两种,分别是旧的java.io.*中FileInputStream的available()方法和新的java..nio.*中的FileChannel
下面依次介绍这几种方法:
首先选择一个文件并查看这个文件在windows中显示的大小,为了测试准确性,我这里选取了一个大文件(超过2GB)
查看这个文件在windows中显示的大小:
使用在线转换工具将其转换成字节:
可以看出这个文件的实际大小是3265574912Byte,下面通过代码来获取文件大小,并进行比较:
一、通过length方法:
1、创建一个文件:
File file = new File("E:\\全部软件\\软件压缩包\\Windows7_W64_SP1_ent.iso");
2、获取文件大小:
/** * 获取文件长度 * @param file */ public static void getFileSize1(File file) { if (file.exists() && file.isFile()) { String fileName = file.getName(); System.out.println("文件"+fileName+"的大小是:"+file.length()); } }
3、查看结果:
可见,使用length方法获取的文件大小与windows中显示的大小一致!
二、通过file.io.*中的流式方法获取
1、创建一个文件
依旧使用上面的文件
File file = new File("E:\\全部软件\\软件压缩包\\Windows7_W64_SP1_ent.iso");
2、使用available方法获取:
/** * 根据java.io.*的流获取文件大小 * @param file */ public static void getFileSize2(File file){ FileInputStream fis = null; try { if(file.exists() && file.isFile()){ String fileName = file.getName(); fis = new FileInputStream(file); System.out.println("文件"+fileName+"的大小是:"+fis.available()+"\n"); } } catch (Exception e) { e.printStackTrace(); }finally{ if(null!=fis){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
3、查看结果:
通过这种方法获取的文件大小是2147483647,很明显,这是int类型所能表示的最大值2^31-1,究其原因是因为文件的大小超过了int所能表示的最大值!!!
file.length()方法返回的类型:
available()方法返回的类型:
三、通过file.nio.*中的FileChannel工具来获取文件大小:
1、创建一个文件
依旧使用相同的大文件:
File file1 = new File("E:\\全部软件\\软件程序\\httpwatch.exe");
2、使用FileChannel获取文件大小:
/** * 根据java.nio.*的流获取文件大小 * @param file */ public static void getFileSize3(File file){ FileChannel fc = null; try { if(file.exists() && file.isFile()){ String fileName = file.getName(); FileInputStream fis = new FileInputStream(file); fc = fis.getChannel(); System.out.println("文件"+fileName+"的大小是:"+fc.size()+"\n"); } } catch (Exception e) { e.printStackTrace(); }finally{ if(null!=fc){ try { fc.close(); } catch (IOException e) { e.printStackTrace(); } } } }
3、查看结果:
可见通过这种方法获取的文件大小和第一种一样,都能获取文件实际大小。
四、使用小文件测试以上三种方法:
以上情况中文件大小超过了available()返回类型int的最大值,下面使用一个没有超过int最大值的文件测试,来验证通过这三种方法获取的大小是否和windows中显示的一致:
1、获取文件,查看其在windows中的大小:
2、将该文件单位转成Byte:
可见该文件总共有28147712个字节。
3、查看通过三种方法获取的结果:
根据结果得知,三种方法获取的大小一致,只是跟文件的实际大小有一些误差!!
本以为是文件类型的原因,于是又试了大小差不多的不同类型文件,结果发现绝大多数情况下只有300M以上的文件才能获取最准确的文件大小,300M以下的都会有一定的误差!
参考了http://www.jb51.net/article/98843.htm这篇文章,但是里面没测试300M以下的文件。
下面是我使用不同大小的文件进行的测试,通过文件在windows中显示的大小、转化成单位Byte后与java获取的方式对比,并计算出误差值:
总结java获取文件大小:
1、三种方法获取小文件(300M以下)时结果一致,但是与windows显示的值有一定误差;
2. When obtaining a large file, in order to avoid that the file length is greater than the maximum value of the method return value type, try to use the length or FileChannel method to obtain it;
The above is the detailed content of Several methods for obtaining file size in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor