Java File Size Get getFileSizeRecursive(File file, boolean subFolders)

Here you can find the source of getFileSizeRecursive(File file, boolean subFolders)

Description

get File Size Recursive

License

Open Source License

Parameter

Parameter Description
file a parameter
subFolders a parameter

Return

long

Declaration

public static long getFileSizeRecursive(File file, boolean subFolders) 

Method Source Code

//package com.java2s;
/*//from   ww w.ja  va  2 s.  c o  m
 * Copyright (c) 2012 Diamond Light Source Ltd.
 *
 * 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
 */

import java.io.File;

public class Main {
    /**
     * @param file
     * @param subFolders
     * @return long
     */
    public static long getFileSizeRecursive(File file, boolean subFolders) {
        long size = 0;
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File element : files) {
                    if (!subFolders && element.isDirectory()) {
                        continue;
                    }
                    long tmpSize = getFileSizeRecursive(element, subFolders);
                    if (tmpSize != -1) {
                        size += tmpSize;
                    }
                }
                return size;
            }
            return -1;
        }
        return file.length();
    }
}

Related

  1. getFileSize2(String filename)
  2. getFileSizeAggregate(String[] files)
  3. getFileSizeFormat(double inputvalue)
  4. getFileSizeFormat(String total_size)
  5. getFileSizeLabel(long sizeInBytes)
  6. getFileSizes(File f)
  7. getFileSizeString(File file, boolean si)
  8. getFilesizeString(long size)
  9. getFileSizeStringFor(File file)