Java to get the size of the file and the example code _java

Source: Internet
Author: User
Tags centos

Java Get File size

Today, when writing code to achieve the function of getting file size, there are two implementations, one is using the length () method of file, and the other is using 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 can cause problems. Let's take a look at:

In the example, I used the installation image file for CentOS 6.5, mainly considering that the file is large enough (greater than 2GB).


1. Use the length () method of the file

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, consistent with the results displayed on Windows.

Let's take a look at the file size obtained by 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); 
      } 
    } 
  } 

The following are the results of the operation:

2147483647

Does this result look familiar? It is a integer.max_value, which is the maximum value that a signed integer can represent.

So how large is the file size to be converted into familiar units?

About 2GB, which is obviously not the right result.

The reason for this is that the length () method of the file returns the maximum number of positive values that can be represented by the Long,long type: 9223372036854775807, converted to the maximum supported file size: 8954730132868714 EB Byte, This magnitude will be useful for many, many years in human it development, and FileInputStream's avaliable () method returns an int, which also describes the largest range of representations, The maximum file size that can be supported is 1.99GB, and this level of magnitude is now easy to reach.

March 31, 2014 added:

Reading large file sizes for streaming methods is not java.io.*, but it is no longer possible to use the traditional packages under the java.nio.*, where the new tool--filechannel is used. Let's look at the sample code below:

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) { 
    Logge R.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 and accurately describe the exact size of the file.

Here also remind you technical colleagues, involved in large file reading, the type of int data must leave a heart, so as to avoid hidden bugs, positioning is very difficult.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.