Java File Size Get getFileSizeStringFor(File file)

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

Description

Returns a string representation of a file size, such as "842 bytes", "1.43 KB" or "3.4 MB".

License

BSD License

Parameter

Parameter Description
file The file for which you want its size converted into an appropriate string.

Return

The string. Note that this will be invalid if file is a directory.

Declaration

public static final String getFileSizeStringFor(File file) 

Method Source Code

//package com.java2s;
/*//from   w w w.j  a  v a2  s . c  o m
 * 04/13/2005
 *
 * Utilities.java - Utility methods for dealing with RTextFileChooser.
 * Copyright (C) 2005 Robert Futrell
 * http://fifesoft.com/rtext
 * Licensed under a modified BSD license.
 * See the included license file for details.
 */

import java.io.File;

import java.text.NumberFormat;

public class Main {
    /**
     * Should only be accessed on the EDT.
     */
    private static NumberFormat fileSizeFormat;

    /**
     * Returns a string representation of a file size, such as "842 bytes",
     * "1.43 KB" or "3.4 MB".
     *
     * @param file The file for which you want its size converted into an
     *        appropriate string.
     * @return The string.  Note that this will be invalid if <code>file</code>
     *         is a directory.
     */
    public static final String getFileSizeStringFor(File file) {
        return getFileSizeStringFor(file.length(), false);
    }

    /**
     * Returns a string representation of a file size, such as "842 bytes",
     * "1.43 KB" or "3.4 MB".  This method should only be called on the EDT.
     *
     * @param size the size of a file, in bytes.
     * @return The string.
     * @see #getFileSizeStringFor(File)
     */
    public static final String getFileSizeStringFor(long size, boolean reportInKB) {

        String str = null;

        if (reportInKB) {
            String suffix = " bytes";
            if (size >= 1024) {
                size /= 1024;
                suffix = " KB";
            }
            str = fileSizeFormat.format(size) + suffix;
        }

        else {

            int count = 0;
            double tempSize = size;
            double prevSize = tempSize;

            // Keep dividing by 1024 until you get the largest unit that goes
            // into this file's size.
            while (count < 4 && ((tempSize = prevSize / 1024f) >= 1)) {
                prevSize = tempSize;
                count++;
            }

            String suffix = null;
            switch (count) {
            case 0:
                suffix = "bytes";
                break;
            case 1:
                suffix = "KB";
                break;
            case 2:
                suffix = "MB";
                break;
            case 3:
                suffix = "GB";
                break;
            case 4:
                suffix = "TB";
                break;
            }

            str = fileSizeFormat.format(prevSize) + " " + suffix;

        }

        return str;

    }
}

Related

  1. getFileSizeLabel(long sizeInBytes)
  2. getFileSizeRecursive(File file, boolean subFolders)
  3. getFileSizes(File f)
  4. getFileSizeString(File file, boolean si)
  5. getFilesizeString(long size)
  6. getFolderSize(File folder)
  7. getSize(long fileSize)
  8. getSizeErrorMessage(String pattern, Long maxFileSize, Long fileSize, String fileName)
  9. getStringSizeLengthFile(long size)