Java File Size Readable Format getFileSizeInMB(String fileName)

Here you can find the source of getFileSizeInMB(String fileName)

Description

get File Size In MB

License

Open Source License

Declaration

public static float getFileSizeInMB(String fileName) 

Method Source Code

//package com.java2s;
/**//from   www  .  ja va 2s .  co  m
 * 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: jintao05@gmail.com 
 *
 * 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.
 */

import java.io.File;

public class Main {
    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

  1. formatSizeInBytes(int size)
  2. formatSpaceUsage(long size)
  3. formatToolBarDisplay(String text, int size)
  4. getFileSizeInByte(File file)
  5. getFileSizeInBytes(File f)
  6. getHumanSize(File dir)
  7. getReadableFileSize(int size)
  8. getReadableFileSize(long fileSizeInBytes)
  9. getReadableFileSize(long size)