首页 > 解决方案 > humanReadableByteCountBin 在 2GB 文件大小后重新返回负数

问题描述

我正在迭代一个 for 循环,该循环添加文件的字节大小并使用 humanReadableByteCountBin 函数以可读格式获取总和。

  1. 在循环中添加文件的字节大小:

    File file = new File(sdFile);
    int file_size = Integer.parseInt(String.valueOf(file.length()));
    totalSize = totalSize+file_size;
    
  2. 将字节转换为人类可读

    humanReadableByteCountBin(totalSize)
    
  3. 人类可读功能

    public static String humanReadableByteCountBin(long bytes) {
    long absB = bytes == Long.MIN_VALUE ? Long.MAX_VALUE : Math.abs(bytes);
    if (absB < 1024) {
        //return bytes + " B";
        return bytes + "B"; //no space 1B
    }
    long value = absB;
    CharacterIterator ci = new StringCharacterIterator("KMGTPE");
    for (int i = 40; i >= 0 && absB > 0xfffccccccccccccL >> i; i -= 10) {
        value >>= 10;
        ci.next();
    }
    value *= Long.signum(bytes);
    return String.format("%.1f %ciB", value / 1024.0, ci.current());
    }//This method of getting readable file size is an used by the method above
    

4.调试输出

...
2119779124
2128881125
2137772858
2146700152
-2140127194
-2131631425
-2122627269
...
  1. 总大小 =-776.4MiB

如上所示,字节总和直到它们达到 2GB 然后它们开始返回大小减小的负数(如果读取为正数)。我期望的总大小约为 7GB,我该如何实现呢?

标签: android

解决方案


推荐阅读