Get File Size In MB : File Size « File Input Output « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » File Input Output » File SizeScreenshots 
Get File Size In MB
      
import java.io.File;


/**
 * BeehiveZ is a business process model and instance management system.
 * Copyright (C) 2011  
 * Institute of Information System and Engineering, School of Software, Tsinghua University,
 * Beijing, China
 *
 * Contact: [email protected] 
 *
 * This program is a free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation with the version of 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


/**
 @author Tao Jin
 
 */

public class Util{
  public static float getFileSizeInMB(String fileName) {
    float ret = getFileSizeInBytes(fileName);
    ret = ret / (float) (1024 1024);
    return ret;
  }
  public static long getFileSizeInBytes(String fileName) {
    long ret = 0;
    File f = new File(fileName);
    if (f.isFile()) {
      return f.length();
    else if (f.isDirectory()) {
      File[] contents = f.listFiles();
      for (int i = 0; i < contents.length; i++) {
        if (contents[i].isFile()) {
          ret += contents[i].length();
        else if (contents[i].isDirectory())
          ret += getFileSizeInBytes(contents[i].getPath());
      }
    }
    return ret;
  }
}

   
    
    
    
    
    
  
Related examples in the same category
1.Get File Size In Bytes
2.Returns a human-readable version of the file size, where the input represents a specific number of bytes.
3.display Bytes Size
4.Bytes counting
5.Size formatting utility.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.