Java File Size Get getFileSize(File file)

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

Description

Method for computing the size of a file or folder

License

Open Source License

Parameter

Parameter Description
file a parameter

Return

OL if it is not possible to compute, in other case size in bytes

Declaration

public static long getFileSize(File file) 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

import java.io.File;

public class Main {
    /**//from   w ww .  j a v  a  2 s. c  om
     * Method for computing the size of a file or folder
     *
     * @param file
     * @return OL if it is not possible to compute, in other case size in bytes
     */
    public static long getFileSize(File file) {
        if (file == null)
            return 0L;
        if (file.isFile())
            return file.length();
        if (!file.isDirectory())
            return 0L;

        File[] subFiles = file.listFiles();
        if (subFiles == null)
            return 0L;

        long size = 0L;
        for (File subFile : subFiles) {
            if (subFile == null)
                continue;
            if (subFile.isFile())
                size += subFile.length();
            else
                size += getFileSize(subFile);
        }

        return size;
    }
}

Related

  1. getFileSize(File file)
  2. getFileSize(File file)
  3. getFileSize(File file)
  4. getFileSize(File file)
  5. getFileSize(File file)
  6. getFileSize(File file)
  7. getFileSize(File file)
  8. getFileSize(File file)
  9. getFileSize(File[] files)