Java how to get file size

Source: Internet
Author: User

There are two ways in which Java obtains file sizes:

1, obtained by the length () method of file;

2, through the flow method to obtain;

There are two ways to pass the flow method, namely the FileInputStream available () method and the new Java in the old java.io.*. The FileChannel in nio.*

Here are some of these methods:

First select a file and see the size of the file displayed in Windows, in order to test the accuracy, I have selected a large file (more than 2GB)

To see the size of this file displayed in Windows:

Convert it to bytes using the online conversion tool:

You can see that the actual size of this file is 3265574912Byte, the following code to get the file size, and compare:

First, through the length method:

1. Create a file:

1 New File ("e:\\ all software \ \ Software compression package \\Windows7_W64_SP1_ent.iso");

2. Get File Size:

    /**      * Get file length      @param      Files*/public    Static  void  getFileSize1 (file file) {        if (file.exists () &&  File.isfile ()) {            = file.getname ();            SYSTEM.OUT.PRINTLN (the size of "file" +filename+ "is:" +file.length ());}    }

3. View the results:

As you can see, the size of the file obtained using the length method is the same as the size shown in Windows!

Second, through the file.io.* in the flow method to obtain

1. Create a file

Still use the file above

New File ("e:\\ all software \ \ Software compression package \\Windows7_W64_SP1_ent.iso");

2. Use the available method to obtain:

/*** Get file size according to Java.io.* 's stream *@paramfile*/     Public Static voidgetFileSize2 (file file) {FileInputStream fis=NULL; Try {            if(File.exists () &&File.isfile ()) {String FileName=File.getname (); FIS=Newfileinputstream (file); System.out.println (The size of the file "+filename+" is: "+fis.available () +" \ n "); }        } Catch(Exception e) {e.printstacktrace (); }finally{            if(NULL!=FIS) {                Try{fis.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }

3. View the results:

The file size obtained by this method is 2147483647, and it is obvious that this is the maximum value 2^31-1 the int type can represent because the size of the file exceeds the maximum value that int can represent!!!

The type returned by the File.length () method:

The type returned by the available () method:

Third, get the file size through the FileChannel tool in file.nio.*:

1. Create a file

Still use the same large file:

New File ("e:\\ all software \ \ software program \\httpwatch.exe");

2. Use FileChannel to get the file size:

    /*** Get file size according to Java.nio.* 's stream *@paramfile*/     Public Static voidgetFileSize3 (file file) {FileChannel FC=NULL; Try {            if(File.exists () &&File.isfile ()) {String FileName=File.getname (); FileInputStream FIS=Newfileinputstream (file); FC=Fis.getchannel (); System.out.println (The size of the file "+filename+" is: "+fc.size () +" \ n "); }        } Catch(Exception e) {e.printstacktrace (); }finally{            if(NULL!=FC) {                Try{fc.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }

3. View the results:

It is visible that the file size obtained by this method is the same as the first one, and the actual file size can be obtained.

Iv. use small files to test the above three methods:

The file size above available () returns the maximum value of the type int, using a file test that does not exceed the int maximum to verify that the size obtained by these three methods is consistent with the display in Windows:

1. Get the file to see its size in Windows:

2. Turn the file unit into byte:

It is visible that the file has a total of 28,147,712 bytes.

3. View the results obtained by three methods:

According to the results, three kinds of methods get the same size, but there are some errors with the actual size of the file!!

This thought is the file type reason, and then tried the size of the different types of files, the results found that in most cases only more than 300M files to obtain the most accurate file size, 300M or less will have a certain error!

Reference to http://blog.csdn.net/chaijunkun/article/details/22387305 this article, but the inside did not test the files below 300M.

Here is the test I made with a different size file, which compares the size of the file displayed in Windows to the way Java was obtained, and calculates the error value:

Summarize Java get file size:

1, three ways to obtain small files (300M or less) when the results are consistent, but with Windows display the value of a certain error;

2, when obtaining large files, to avoid the length of the file is greater than the maximum value of the method return value type, try to use the length or FileChannel method to obtain;

If you see this article you have questions about this article, welcome to ask questions ~

Java how to get file size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.