The source of this article: http://blog.csdn.net/chaijunkun/article/details/22387305, reprint please specify. Because I do not regularly collate the relevant blog posts, the corresponding content will be improved. It is therefore highly recommended to view this article in the original source.
Today, when writing code to achieve the function of getting file size, there are two implementations, one is to use the file length () method, and the other is to use the FileInputStream available () method, when InputStream does not perform a read operation , the size of the available () should be equal to the file size. However, when working with large files, the latter problem occurs. Let's take a look:
In the example, I used the CentOS 6.5 installation image file, mainly considering that this file is large enough (more than 2GB).
1. Use the length () method of File
Public Static void Main (string[] args) { file Fnew 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");} }
Let's look at the output:
4467982336
The result is 4.16GB, which is consistent with the results displayed on Windows.
Next we look at the size of the file obtained by FileInputStream:
Public Static voidMain (string[] args) {FileInputStream fis=NULL; Try{File F=NewFile ("D:\\centos-6.5-x86_64-bin-dvd1.iso"); FIS=NewFileInputStream (f); Logger.info (Fis.available ()); }Catch(Exception e) {logger.error (e); } finally{ if(NULL!=FIS) { Try{fis.close (); } Catch(IOException e) {logger.error (e); } } }}
Here is the result of the operation:
2147483647
Does this result look familiar? It is a integer.max_value, which is the maximum number that a signed integer can represent.
So what is the size of the file to be converted into a familiar unit, the way it gets?
About equal to 2GB, which is obviously not the right result.
The reason for this is that the length () method of file returns a positive value of type Long,long that can be represented by: 9223372036854775807, converted to the maximum supported file size: 8954730132868714 EB Byte, This magnitude will be useful for many years in the history of human it, and FileInputStream's avaliable () method return value is int, which describes the largest range of representations before, The maximum supported file size is 1.99GB, and this magnitude is now easy to reach.
March 31, 2014 supplement:
It is not possible to read large file sizes for streaming methods, but it is no longer possible to use the traditional java.io.* package, where a new tool--filechannel under java.nio.* is used. Let's look at the sample code below:
Public Static voidMain (string[] args) {FileChannel FC=NULL; Try{File F=NewFile ("D:\\centos-6.5-x86_64-bin-dvd1.iso"); if(F.exists () &&F.isfile ()) {FileInputStream FIS=NewFileInputStream (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); } } }}
The results obtained using FileChannel match the first case, accurately describing the exact size of the file.
Here also remind you technical colleagues, involved in large file reading, the type of int data must be left a heart, in order to avoid hidden bugs, it is difficult to locate.
The correct way to get the file size in the "Go" Java