代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
文件大小单位转换
作者:
/ 发布于
2011/1/14
/
544
<div><span style="color: #0000ff">import</span><span style="color: #000000"> java.text.DecimalFormat; </span><span style="color: #0000ff">import</span><span style="color: #000000"> java.util.Hashtable; </span><span style="color: #008000">/**</span><span style="color: #008000"> * 文件大小单位转换 * </span><span style="color: #808080">@author</span><span style="color: #008000"> Administrator * </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> UnitsConversion </span><span style="color: #0000ff">extends</span><span style="color: #000000"> DecimalFormat { </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">final</span><span style="color: #000000"> </span><span style="color: #0000ff">long</span><span style="color: #000000"> serialVersionUID </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">3168068393840262910L</span><span style="color: #000000">; </span><span style="color: #008000">/**</span><span style="color: #008000"> * 存放有效单位的数组 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> Hashtable</span><span style="color: #000000"><</span><span style="color: #000000">String, String</span><span style="color: #000000">></span><span style="color: #000000"> validUnits </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Hashtable</span><span style="color: #000000"><</span><span style="color: #000000">String, String</span><span style="color: #000000">></span><span style="color: #000000">(); </span><span style="color: #008000">/**</span><span style="color: #008000"> * 限制文件大小上限为1G </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> GB_MAX_SIZE </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">; </span><span style="color: #008000">/**</span><span style="color: #008000"> * 最大的MB值 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> MB_MAX_SIZE </span><span style="color: #000000">=</span><span style="color: #000000"> GB_MAX_SIZE </span><span style="color: #000000">*</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000">; </span><span style="color: #008000">/**</span><span style="color: #008000"> * 最大的KB值 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> KB_MAX_SIZE </span><span style="color: #000000">=</span><span style="color: #000000"> MB_MAX_SIZE </span><span style="color: #000000">*</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000">; </span><span style="color: #008000">/**</span><span style="color: #008000"> * 最大的Bytes值 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">int</span><span style="color: #000000"> BYTES_MAX_SIZE </span><span style="color: #000000">=</span><span style="color: #000000"> KB_MAX_SIZE </span><span style="color: #000000">*</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000">; </span><span style="color: #008000">/**</span><span style="color: #008000"> * 数字部分的值 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> Double numPart; </span><span style="color: #008000">/**</span><span style="color: #008000"> * 原始的单位字符串 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> String originalUnit; </span><span style="color: #008000">/**</span><span style="color: #008000"> * 标准的单位字符串 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> String unit; </span><span style="color: #008000">/**</span><span style="color: #008000"> * 转换后的结果 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> String result; </span><span style="color: #008000">//</span><span style="color: #008000"> 添加所有有效单位</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> { validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">字节</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">Bytes</span><span style="color: #000000">"</span><span style="color: #000000">); validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">bytes</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">Bytes</span><span style="color: #000000">"</span><span style="color: #000000">); validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">byte</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">Bytes</span><span style="color: #000000">"</span><span style="color: #000000">); validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">kb</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">KB</span><span style="color: #000000">"</span><span style="color: #000000">); validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">k</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">KB</span><span style="color: #000000">"</span><span style="color: #000000">); validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">兆</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">MB</span><span style="color: #000000">"</span><span style="color: #000000">); validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">mb</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">MB</span><span style="color: #000000">"</span><span style="color: #000000">); validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">m</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">MB</span><span style="color: #000000">"</span><span style="color: #000000">); validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">gb</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">GB</span><span style="color: #000000">"</span><span style="color: #000000">); validUnits.put(</span><span style="color: #000000">"</span><span style="color: #000000">g</span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">"</span><span style="color: #000000">GB</span><span style="color: #000000">"</span><span style="color: #000000">); } </span><span style="color: #008000">/**</span><span style="color: #008000"> * 构造方法:指定了数字格式,初始所有属性为NULL </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> UnitsConversion() { </span><span style="color: #0000ff">super</span><span style="color: #000000">(</span><span style="color: #000000">"</span><span style="color: #000000">########.##</span><span style="color: #000000">"</span><span style="color: #000000">); numPart </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">; result </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">; unit </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">; originalUnit </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">; } </span><span style="color: #008000">/**</span><span style="color: #008000"> * 根据单位、数字大小按照常用的转换原则进行转换 * * </span><span style="color: #808080">@param</span><span style="color: #008000"> input * </span><span style="color: #808080">@return</span><span style="color: #008000"> 成功转换后的结果是非空字符串;若失败了,结果为空字符串 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> String defaultConversion(String input) { analyzeString(input); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (result </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> result; } </span><span style="color: #008000">//</span><span style="color: #008000"> 单位 Bytes</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (unit.equals(</span><span style="color: #000000">"</span><span style="color: #000000">Bytes</span><span style="color: #000000">"</span><span style="color: #000000">)) { </span><span style="color: #0000ff">int</span><span style="color: #000000"> numPart2Int </span><span style="color: #000000">=</span><span style="color: #000000"> numPart.intValue(); </span><span style="color: #008000">//</span><span style="color: #008000"> 输入大小与1G相差0.5M之内,返回1GB</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">if</span><span style="color: #000000"> ((BYTES_MAX_SIZE </span><span style="color: #000000">-</span><span style="color: #000000"> numPart2Int) </span><span style="color: #000000"><</span><span style="color: #000000"> (</span><span style="color: #000000">1024</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000">) </span><span style="color: #000000">/</span><span style="color: #000000"> </span><span style="color: #000000">2</span><span style="color: #000000">) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">1 GB</span><span style="color: #000000">"</span><span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000"> (0,1KB)</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (numPart2Int </span><span style="color: #000000"><</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000">) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> numPart2Int </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000"> Bytes</span><span style="color: #000000">"</span><span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000"> [1KB,1023KB]</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (numPart2Int </span><span style="color: #000000">>=</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000"> </span><span style="color: #000000">&&</span><span style="color: #000000"> numPart2Int </span><span style="color: #000000"><=</span><span style="color: #000000"> (</span><span style="color: #000000">1024</span><span style="color: #000000"> </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">) </span><span style="color: #000000">*</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000">) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> format(numPart </span><span style="color: #000000">/</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000">) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000"> KB</span><span style="color: #000000">"</span><span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000"> (1023KB,1GB)</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (numPart2Int </span><span style="color: #000000">></span><span style="color: #000000"> (</span><span style="color: #000000">1024</span><span style="color: #000000"> </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">) </span><span style="color: #000000">*</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000"> </span><span style="color: #000000">&&</span><span style="color: #000000"> numPart2Int </span><span style="color: #000000"><</span><span style="color: #000000"> BYTES_MAX_SIZE) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> format(numPart </span><span style="color: #000000">/</span><span style="color: #000000"> (</span><span style="color: #000000">1024</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000">)) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000"> MB</span><span style="color: #000000">"</span><span style="color: #000000">; } </span><span style="color: #0000ff">else</span><span style="color: #000000"> result </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">""</span><span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000"> result; } </span><span style="color: #0000ff">if</span><span style="color: #000000"> (unit.equals(</span><span style="color: #000000">"</span><span style="color: #000000">KB</span><span style="color: #000000">"</span><span style="color: #000000">)) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">还没实现....</span><span style="color: #000000">"</span><span style="color: #000000">; } </span><span style="color: #0000ff">if</span><span style="color: #000000"> (unit.equals(</span><span style="color: #000000">"</span><span style="color: #000000">MB</span><span style="color: #000000">"</span><span style="color: #000000">)) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">还没实现....</span><span style="color: #000000">"</span><span style="color: #000000">; } </span><span style="color: #0000ff">if</span><span style="color: #000000"> (unit.equals(</span><span style="color: #000000">"</span><span style="color: #000000">GB</span><span style="color: #000000">"</span><span style="color: #000000">)) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">还没实现....</span><span style="color: #000000">"</span><span style="color: #000000">; } result </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">""</span><span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000"> result; } </span><span style="color: #008000">/**</span><span style="color: #008000"> * 把字符串的数字部分与单位分离,并对数字、单位的有效性进行检验, 若有非法状况,把结果赋值为 "" ,将其返回给用户 * * </span><span style="color: #808080">@param</span><span style="color: #008000"> input </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> analyzeString(String input) { </span><span style="color: #008000">//</span><span style="color: #008000"> 初步检验输入的字符串</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (input </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> input.trim().length() </span><span style="color: #000000"><</span><span style="color: #000000"> </span><span style="color: #000000">2</span><span style="color: #000000">) { System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">输入的字符串有误</span><span style="color: #000000">"</span><span style="color: #000000">); result </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">""</span><span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000">; } input </span><span style="color: #000000">=</span><span style="color: #000000"> input.replaceAll(</span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">, </span><span style="color: #000000">""</span><span style="color: #000000">); </span><span style="color: #0000ff">int</span><span style="color: #000000"> firstIndexOfUnit;</span><span style="color: #008000">//</span><span style="color: #008000"> 单位的起始位置</span><span style="color: #008000"> </span><span style="color: #000000"> String strOfNum;</span><span style="color: #008000">//</span><span style="color: #008000"> 数字部分的字符串 </span><span style="color: #008000">//</span><span style="color: #008000"> 从尾部开始遍历字符串</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> input.length() </span><span style="color: #000000">-</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">; i </span><span style="color: #000000">>=</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">; i</span><span style="color: #000000">--</span><span style="color: #000000">) { </span><span style="color: #0000ff">if</span><span style="color: #000000"> (Character.isDigit(input.charAt(i))) { firstIndexOfUnit </span><span style="color: #000000">=</span><span style="color: #000000"> i </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">; originalUnit </span><span style="color: #000000">=</span><span style="color: #000000"> input.substring(firstIndexOfUnit, input.length()).toLowerCase(); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #000000">isValidUnit(originalUnit)) { System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">无效单位。</span><span style="color: #000000">"</span><span style="color: #000000">); result </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">""</span><span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000">; } unit </span><span style="color: #000000">=</span><span style="color: #000000"> validUnits.get(originalUnit); strOfNum </span><span style="color: #000000">=</span><span style="color: #000000"> input.substring(</span><span style="color: #000000">0</span><span style="color: #000000">, firstIndexOfUnit); numPart </span><span style="color: #000000">=</span><span style="color: #000000"> Double.parseDouble(strOfNum); </span><span style="color: #0000ff">if</span><span style="color: #000000"> (</span><span style="color: #000000">!</span><span style="color: #000000">isValidNum(numPart, unit)) { System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">文件大小非法</span><span style="color: #000000">"</span><span style="color: #000000">); result </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">""</span><span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000">; } </span><span style="color: #0000ff">if</span><span style="color: #000000"> (numPart </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000">) { result </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">0 Bytes</span><span style="color: #000000">"</span><span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000">; } </span><span style="color: #0000ff">break</span><span style="color: #000000">; } } </span><span style="color: #0000ff">if</span><span style="color: #000000"> (unit </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> numPart </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000">) { System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">输入的字符串有误</span><span style="color: #000000">"</span><span style="color: #000000">); result </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">""</span><span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000">; } } </span><span style="color: #008000">/**</span><span style="color: #008000"> * 文件大小越界检查 * * </span><span style="color: #808080">@param</span><span style="color: #008000"> num * </span><span style="color: #808080">@param</span><span style="color: #008000"> unit * </span><span style="color: #808080">@return</span><span style="color: #008000"> 在1G范围内(包括1G),返回true;否则返回false </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">boolean</span><span style="color: #000000"> isValidNum(Double num, String unit) { </span><span style="color: #0000ff">if</span><span style="color: #000000"> (num </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> num </span><span style="color: #000000"><</span><span style="color: #000000"> </span><span style="color: #000000">0</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> num </span><span style="color: #000000">></span><span style="color: #000000"> BYTES_MAX_SIZE) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">false</span><span style="color: #000000">; } </span><span style="color: #0000ff">if</span><span style="color: #000000"> (unit.equals(</span><span style="color: #000000">"</span><span style="color: #000000">KB</span><span style="color: #000000">"</span><span style="color: #000000">) </span><span style="color: #000000">&&</span><span style="color: #000000"> num </span><span style="color: #000000">></span><span style="color: #000000"> KB_MAX_SIZE) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">false</span><span style="color: #000000">; } </span><span style="color: #0000ff">if</span><span style="color: #000000"> (unit.equals(</span><span style="color: #000000">"</span><span style="color: #000000">MB</span><span style="color: #000000">"</span><span style="color: #000000">) </span><span style="color: #000000">&&</span><span style="color: #000000"> num </span><span style="color: #000000">></span><span style="color: #000000"> MB_MAX_SIZE) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">false</span><span style="color: #000000">; } </span><span style="color: #0000ff">if</span><span style="color: #000000"> (unit.equals(</span><span style="color: #000000">"</span><span style="color: #000000">GB</span><span style="color: #000000">"</span><span style="color: #000000">) </span><span style="color: #000000">&&</span><span style="color: #000000"> num </span><span style="color: #000000">></span><span style="color: #000000"> GB_MAX_SIZE) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">false</span><span style="color: #000000">; } </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">; } </span><span style="color: #008000">/**</span><span style="color: #008000"> * 检查原始单位originalUnit是否有效 * * </span><span style="color: #808080">@param</span><span style="color: #008000"> originalUnit * </span><span style="color: #808080">@return</span><span style="color: #008000"> 若originalUnit为空,那么会给他赋默认值 bytes ,并返回true; * 若originalUnit是有效单位集合中之一,返回true。 </span><span style="color: #008000">*/</span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">boolean</span><span style="color: #000000"> isValidUnit(String originalUnit) { </span><span style="color: #0000ff">if</span><span style="color: #000000"> (originalUnit </span><span style="color: #000000">==</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> originalUnit.trim().length() </span><span style="color: #000000"><</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">) { originalUnit </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">bytes</span><span style="color: #000000">"</span><span style="color: #000000">; </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">; } </span><span style="color: #0000ff">for</span><span style="color: #000000"> (String validUnit : validUnits.keySet()) { </span><span style="color: #0000ff">if</span><span style="color: #000000"> (validUnit.equalsIgnoreCase(originalUnit)) { </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">true</span><span style="color: #000000">; } } </span><span style="color: #0000ff">return</span><span style="color: #000000"> </span><span style="color: #0000ff">false</span><span style="color: #000000">; } </span><span style="color: #008000">//</span><span style="color: #008000">测试</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> main(String[] args) { System.out.println(</span><span style="color: #000000">"</span><span style="color: #000000">-------------</span><span style="color: #000000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">for</span><span style="color: #000000"> (</span><span style="color: #0000ff">int</span><span style="color: #000000"> i </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #000000">1020</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000">; i </span><span style="color: #000000"><=</span><span style="color: #000000"> </span><span style="color: #000000">1024</span><span style="color: #000000"> </span><span style="color: #000000">*</span><span style="color: #000000"> </span><span style="color: #000000">1111</span><span style="color: #000000">; i </span><span style="color: #000000">+=</span><span style="color: #000000"> </span><span style="color: #000000">9</span><span style="color: #000000">) { String input </span><span style="color: #000000">=</span><span style="color: #000000"> i </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000">; System.out.println(input </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">"</span><span style="color: #000000"> ---> </span><span style="color: #000000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> UnitsConversion().defaultConversion(input)); } } } </span></div>
试试其它关键字
文件大小
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3