Java File Size Readable Format formatSize(long size)

Here you can find the source of formatSize(long size)

Description

format Size

License

Apache License

Declaration

public static String formatSize(long size) 

Method Source Code

//package com.java2s;
/*/*from ww  w.  j a va 2 s.c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

public class Main {
    private static final long KB = 1024;
    private static final long MB = 1024 * 1024;
    private static final long GB = 1024 * 1024 * 1024;

    public static String formatSize(long size) {
        String result = "";
        if (size >= 0 && size < KB) {
            result = size + "B";
        } else if (size >= KB && size < MB) {
            result = size / KB + "KB";
        } else if (size >= MB && size < GB) {
            result = size / MB + "MB";
        } else if (size >= GB) {
            result = size / GB + "GB";
        }
        return result;
    }
}

Related

  1. formatSize(long bytes, String b, String kb, String mb, String gb)
  2. formatSize(long size)
  3. formatSize(long size)
  4. formatSize(long size)
  5. formatSize(long size)
  6. formatSize(long size)
  7. formatSize(long size)
  8. formatSize(long size)
  9. formatSize(Long size)