Java Utililty Methods File Size Get

List of utility methods to do File Size Get

Description

The list of methods to do File Size Get are organized into topic(s).

Method

longgetFileSize(String loc)
Returns the length of a file in bytes given its location.
File f = new File(loc);
if (!f.exists()) {
    System.err.println("File " + loc + " does not exist");
    System.exit(1);
long leng = f.length();
if (leng == 0) {
    System.err.println("File " + loc + " has zero length");
...
longgetFileSize(String path)
get File Size
File file = new File(path);
if (file.isFile() && file.exists()) {
    return file.length();
return 0;
longgetFileSize(String path)
Returns the size of the specified file in bytes.
try {
    File file = new File(path);
    long fileSize = file.length();
    return fileSize;
} catch (Exception e) {
    System.out.println("Could not obtain file size:\n" + e.toString());
    return -1;
StringgetFilesize(String path)
Returns the file size in MB, KB or B rounded to two decimal places.
File file = new File(path);
long fileSize = file.length();
float kfileSize = fileSize / 1024;
float mfileSize = kfileSize / 1024;
String size;
if (fileSize > 1048576) {
    size = round(mfileSize, 2) + "MB";
} else if (fileSize > 1024) {
...
longgetFileSize2(String filename)
get File Size
File file = new File(filename);
return file.length();
longgetFileSizeAggregate(String[] files)
Gets the file size aggregate.
int size = 0;
for (String f : files) {
    size += new File(f).length();
return size;
StringgetFileSizeFormat(double inputvalue)
get File Size Format
String outputvalue = "0";
if (inputvalue >= 1024 * 1024) {
    outputvalue = formatFloat("###,##0.0", inputvalue / (1024 * 1024)) + "M";
} else if (inputvalue >= 1024) {
    outputvalue = formatFloat("###,##0.0", inputvalue / 1024) + "K";
} else {
    outputvalue = formatFloat("###,##0", inputvalue) + "Bytes";
return outputvalue;
StringgetFileSizeFormat(String total_size)
getFileSizeFormat()
double file_size = Double.parseDouble(total_size);
String file_size_nm = "";
DecimalFormat f2 = new DecimalFormat("0.00");
if (1023 < file_size) {
    file_size = file_size / 1024;
    file_size_nm = f2.format(file_size) + "KB";
} else {
    file_size_nm = file_size + "byte";
...
StringgetFileSizeLabel(long sizeInBytes)
get File Size Label
double divisor;
String units = "";
if (sizeInBytes > Math.pow(10, 9)) {
    divisor = Math.pow(10, 9);
    units = GB;
} else if (sizeInBytes > Math.pow(10, 6)) {
    divisor = Math.pow(10, 6);
    units = MB;
...
longgetFileSizeRecursive(File file, boolean subFolders)
get File Size Recursive
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();