<small id='DXOCd'></small><noframes id='DXOCd'>

    <i id='DXOCd'><tr id='DXOCd'><dt id='DXOCd'><q id='DXOCd'><span id='DXOCd'><b id='DXOCd'><form id='DXOCd'><ins id='DXOCd'></ins><ul id='DXOCd'></ul><sub id='DXOCd'></sub></form><legend id='DXOCd'></legend><bdo id='DXOCd'><pre id='DXOCd'><center id='DXOCd'></center></pre></bdo></b><th id='DXOCd'></th></span></q></dt></tr></i><div id='DXOCd'><tfoot id='DXOCd'></tfoot><dl id='DXOCd'><fieldset id='DXOCd'></fieldset></dl></div>
        • <bdo id='DXOCd'></bdo><ul id='DXOCd'></ul>
      1. <tfoot id='DXOCd'></tfoot>
        <legend id='DXOCd'><style id='DXOCd'><dir id='DXOCd'><q id='DXOCd'></q></dir></style></legend>

        将文件大小格式化为 MB、GB 等

        时间:2023-05-28

              <bdo id='OmMAx'></bdo><ul id='OmMAx'></ul>
              <tfoot id='OmMAx'></tfoot>

              1. <legend id='OmMAx'><style id='OmMAx'><dir id='OmMAx'><q id='OmMAx'></q></dir></style></legend>
                  <tbody id='OmMAx'></tbody>

                <small id='OmMAx'></small><noframes id='OmMAx'>

              2. <i id='OmMAx'><tr id='OmMAx'><dt id='OmMAx'><q id='OmMAx'><span id='OmMAx'><b id='OmMAx'><form id='OmMAx'><ins id='OmMAx'></ins><ul id='OmMAx'></ul><sub id='OmMAx'></sub></form><legend id='OmMAx'></legend><bdo id='OmMAx'><pre id='OmMAx'><center id='OmMAx'></center></pre></bdo></b><th id='OmMAx'></th></span></q></dt></tr></i><div id='OmMAx'><tfoot id='OmMAx'></tfoot><dl id='OmMAx'><fieldset id='OmMAx'></fieldset></dl></div>
                  本文介绍了将文件大小格式化为 MB、GB 等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要使用合理的单位将文件大小显示为字符串.

                  例如,

                  1L ==>1B";1024L ==>1 KB";2537253L ==>2.3 MB"

                  等等

                  我发现 this previous answer,我觉得不满意.

                  我想出了自己的解决方案,但也有类似的缺点:

                  private static final long K = 1024;私有静态最终长 M = K * K;私有静态最终长 G = M * K;私有静态最终长 T = G * K;public static String convertToStringRepresentation(final long value){最终的 long[] 分隔符 = new long[] { T, G, M, K, 1 };final String[] units = new String[] { TB"、GB"、MB"、KB"、B"};如果(值 <1)throw new IllegalArgumentException("无效的文件大小:"+ value);字符串结果 = null;for(int i = 0; i 

                  主要问题是我对 Decimalformat 和/或 String.format 的了解有限.我希望 1024L、1025L 等映射到 1 KB 而不是 1.0 KB.

                  所以,有两种可能:

                  1. 我更喜欢公共图书馆中的开箱即用解决方案,例如 Apache Commons 或 Google Guava.
                  2. 如果没有,我怎样才能摆脱 '.0' 部分(不求助于字符串替换和正则表达式,我可以自己做)?

                  解决方案

                  public static String readableFileSize(long size) {如果(大小 <= 0)返回0";最终字符串 [] 单位 = 新字符串 [] { "B", "kB", "MB", "GB", "TB" };int digitGroups = (int) (Math.log10(size)/Math.log10(1024));return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];}

                  这将工作到 1000 TB....而且程序很短!

                  I need to display a file size as a string using sensible units.

                  For example,

                  1L ==> "1 B";
                  1024L ==> "1 KB";
                  2537253L ==> "2.3 MB"
                  

                  etc.

                  I found this previous answer, which I didn't find satisfactory.

                  I have come up with my own solution which has similar shortcomings:

                  private static final long K = 1024;
                  private static final long M = K * K;
                  private static final long G = M * K;
                  private static final long T = G * K;
                  
                  public static String convertToStringRepresentation(final long value){
                      final long[] dividers = new long[] { T, G, M, K, 1 };
                      final String[] units = new String[] { "TB", "GB", "MB", "KB", "B" };
                      if(value < 1)
                          throw new IllegalArgumentException("Invalid file size: " + value);
                      String result = null;
                      for(int i = 0; i < dividers.length; i++){
                          final long divider = dividers[i];
                          if(value >= divider){
                              result = format(value, divider, units[i]);
                              break;
                          }
                      }
                      return result;
                  }
                  
                  private static String format(final long value,
                      final long divider,
                      final String unit){
                      final double result =
                          divider > 1 ? (double) value / (double) divider : (double) value;
                      return String.format("%.1f %s", Double.valueOf(result), unit);
                  }
                  

                  The main problem is my limited knowledge of Decimalformat and / or String.format. I would like 1024L, 1025L, etc. to map to 1 KB rather than 1.0 KB.

                  So, two possibilities:

                  1. I would prefer a good out-of-the-box solution in a public library like Apache Commons or Google Guava.
                  2. If there isn't, how can I get rid of the '.0' part (without resorting to string replacement and regex, I can do that myself)?

                  解决方案

                  public static String readableFileSize(long size) {
                      if(size <= 0) return "0";
                      final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" };
                      int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
                      return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
                  }
                  

                  This will work up to 1000 TB.... and the program is short!

                  这篇关于将文件大小格式化为 MB、GB 等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <small id='hCFC4'></small><noframes id='hCFC4'>

                      <bdo id='hCFC4'></bdo><ul id='hCFC4'></ul>
                        <tfoot id='hCFC4'></tfoot>

                          <tbody id='hCFC4'></tbody>

                        1. <i id='hCFC4'><tr id='hCFC4'><dt id='hCFC4'><q id='hCFC4'><span id='hCFC4'><b id='hCFC4'><form id='hCFC4'><ins id='hCFC4'></ins><ul id='hCFC4'></ul><sub id='hCFC4'></sub></form><legend id='hCFC4'></legend><bdo id='hCFC4'><pre id='hCFC4'><center id='hCFC4'></center></pre></bdo></b><th id='hCFC4'></th></span></q></dt></tr></i><div id='hCFC4'><tfoot id='hCFC4'></tfoot><dl id='hCFC4'><fieldset id='hCFC4'></fieldset></dl></div>
                          <legend id='hCFC4'><style id='hCFC4'><dir id='hCFC4'><q id='hCFC4'></q></dir></style></legend>