Java File Size Get getFileSize(File dir)

Here you can find the source of getFileSize(File dir)

Description

Get size of the file

License

Open Source License

Parameter

Parameter Description
file the file for which size to be found

Return

size of the file

Declaration

public static long getFileSize(File dir) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2011. All rights reserved. This program and the
 * accompanying materials are made available under the terms of the Eclipse
 * Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //from  w ww. j a v  a  2 s. c o m
 * Contributors: 
 *       Fizer Khan, Yasmine - initial API and implementation
 *******************************************************************************/

import java.io.File;

public class Main {
    /**
     * Get size of the file
     * 
     * @param file
     *            the file for which size to be found
     * @return size of the file
     */
    public static long getFileSize(File dir) {
        long size = 0;
        if (dir.isFile()) {
            size = dir.length();
        } else {
            File[] subFiles = dir.listFiles();

            for (File file : subFiles) {
                if (file.isFile()) {
                    size += file.length();
                } else {
                    size += getFileSize(file);
                }
            }
        }

        return size;
    }
}

Related

  1. countFileDisplaySize(long size)
  2. countFileSize(String pathname)
  3. fileSizeString(long bytes)
  4. getFileKBSize(long fsize)
  5. getFileSize(@Nonnull File file)
  6. getFileSize(File element)
  7. getFileSize(File f)
  8. getFileSize(File file)
  9. getFileSize(File file)