目前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、查看结果:
二、通过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、查看结果:
三、通过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、查看结果:
更多java知识请关注java基础教程。
The above is the detailed content of How to get the 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.