Java get file size
Today, when writing code, you need to implement the function of obtaining file size. There are currently two implementation methods. One is to use File's length() method; the other is to use FileInputStream's available() method. When InputStream does not perform read operation, the size of available() should be equal to the file size. But when dealing with large files, problems occur in the latter. Let's take a look:
In the example, I used the installation image file of CentOS 6.5, mainly considering that this file is large enough (greater than 2GB).
1. Use File's length() method
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"); } }Let's look at the output:
4467982336
The result is 4.16GB, which is consistent with the results displayed on Windows.
Next, let's take a look at the file size obtained through FileInputStream:
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); } } } } }Here are the running results:
2147483647
Does this result look familiar? It is Integer.MAX_VALUE, which is the maximum value that a signed integer can represent.
So, if you convert it into a familiar unit, how big is the file size obtained in this way?
It's about 2GB, which is obviously not the correct result.
The reason is that the type returned by the length() method of File is long, and the maximum value of the positive value that the long type can represent is: 9223372036854775807. The maximum file size that can be converted into the maximum support is: 8954730132868714 EB bytes. This order of magnitude will be used for many years in the history of human IT development. The return value of FileInputStream's available() method is int. The maximum representation range was introduced before. The maximum file size that can be supported is: 1.99GB, and we can easily reach this order of magnitude.
Added on March 31, 2014:
It is not feasible to read large file sizes for streaming methods, but you can no longer use the traditional package under java.io.*. Here you need to use the new tool under java.nio.* - FileChannel. Let's take a look at the following sample code:
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); } } } } }The results obtained after using FileChannel are consistent with the first case, accurately describing the exact size of the file.
This also reminds all technical colleagues that when reading large files, you must be careful about int-type data to avoid hidden bugs and it is difficult to locate them.
Thank you for reading, I hope it can help you. Thank you for your support for this site!