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

StringgetFileSize(final String filePath)
get File Size
String formattedFileSize = "0 B";
File fileReference = new File(filePath);
if (fileReference.exists()) {
    long fileSizeInBytes = fileReference.length();
    if (fileSizeInBytes < 1024) {
        return fileSizeInBytes + " B";
    int unitIndex = (int) (Math.log(fileSizeInBytes) / Math.log(1024));
...
longgetFileSize(final String path)
get File Size
return getFileSize(getFile(path));
intgetFileSize(InputStream inputStream)
get File Size
int size = 0;
try {
    size = inputStream.available();
} catch (IOException e) {
    e.printStackTrace();
return size;
StringgetFileSize(int fileSize)
get File Size
double result = fileSize;
if (result < 1024) {
    return result + "B";
DecimalFormat df = new DecimalFormat("#0.00");
result = result / 1024;
if (result < 1024) {
    return df.format(result) + "KB";
...
StringgetFileSize(long fileSize)
get File Size
String fileSizeStr = "";
DecimalFormat df = new DecimalFormat("#.0");
if (fileSize < 1024) {
    fileSizeStr = df.format((double) fileSize) + "B";
} else if (fileSize < 1048576) {
    fileSizeStr = df.format((double) fileSize / 1024) + "K";
} else if (fileSize < 1073741824) {
    fileSizeStr = df.format((double) fileSize / 1048576) + "M";
...
longgetFilesize(String address)
get Filesize
File file = new File(address);
if (file.exists()) {
    return file.length();
} else {
    return 0;
longgetFileSize(String fileName)
Get the size of a file.
File file = new File(fileName);
return file.length();
StringgetFileSize(String filename)
get File Size
File file = new File(filename);
return getReadableFileSize(file.length());
intgetFileSize(String filename)
get File Size
try {
    File fl = new File(filename);
    int length = (int) fl.length();
    return length;
} catch (Exception e) {
    return 0;
longgetFileSize(String fileName, Logger log)
get File Size
File file = new File(fileName);
if (!file.exists()) {
    if (log != null)
        log.log(Level.WARNING, "File \"" + fileName + "\" not found, so size is zero");
    return 0;
return file.length();