Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.File;
import java.text.DecimalFormat;

public class Main {

    public static String fileLength(String filePath) {
        return fileLength(new File(filePath).length());
    }

    public static String fileLength(long length) {
        String lenStr = null;
        DecimalFormat formater = new DecimalFormat("#0.##");
        if (length < 1024) {
            lenStr = formater.format(length) + " Byte";
        } else if (length < 1024 * 1024) {
            lenStr = formater.format(length / 1024.0f) + " KB";
        } else if (length < 1024 * 1024 * 1024) {
            lenStr = formater.format(length / (1024 * 1024)) + " MB";
        } else {
            lenStr = formater.format(length / (1024 * 1024 * 1024)) + " GB";
        }
        return lenStr;
    }
}