一、String工具類
- package?com.mkyong.common;??
- ???
- import?java.util.ArrayList;??
- import?java.util.List;??
- ???
- /**?
- ?*??
- ?*?String工具類.?<br>?
- ?*??
- ?*?@author?宋立君?
- ?*?@date?2014年06月24日?
- ?*/??
- public?class?StringUtil?{??
- ???
- ????private?static?final?int?INDEX_NOT_FOUND?=?-1;??
- ????private?static?final?String?EMPTY?=?"";??
- ????/**?
- ?????*?<p>?
- ?????*?The?maximum?size?to?which?the?padding?constant(s)?can?expand.?
- ?????*?</p>?
- ?????*/??
- ????private?static?final?int?PAD_LIMIT?=?8192;??
- ???
- ????/**?
- ?????*?功能:將半角的符號轉換成全角符號.(即英文字符轉中文字符)?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@return?String?
- ?????*?@date?2014年06月24日?
- ?????*/??
- ????public?static?String?changeToFull(String?str)?{??
- ????????String?source?=?"1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+\\|[];:'\",<.>/?";??
- ????????String[]?decode?=?{?"1",?"2",?"3",?"4",?"5",?"6",?"7",?"8",?"9",?"0",??
- ????????????????"!",?"@",?"#",?"$",?"%",?"︿",?"&",?"*",?"(",?")",?"a",?"b",??
- ????????????????"c",?"d",?"e",?"f",?"g",?"h",?"i",?"j",?"k",?"l",?"m",?"n",??
- ????????????????"o",?"p",?"q",?"r",?"s",?"t",?"u",?"v",?"w",?"x",?"y",?"z",??
- ????????????????"A",?"B",?"C",?"D",?"E",?"F",?"G",?"H",?"I",?"J",?"K",?"L",??
- ????????????????"M",?"N",?"O",?"P",?"Q",?"R",?"S",?"T",?"U",?"V",?"W",?"X",??
- ????????????????"Y",?"Z",?"-",?"_",?"=",?"+",?"\",?"|",?"【",?"】",?";",?":",??
- ????????????????"'",?"\"",?",",?"〈",?"。",?"〉",?"/",?"?"?};??
- ????????String?result?=?"";??
- ????????for?(int?i?=?0;?i?<?str.length();?i++)?{??
- ????????????int?pos?=?source.indexOf(str.charAt(i));??
- ????????????if?(pos?!=?-1)?{??
- ????????????????result?+=?decode[pos];??
- ????????????}?else?{??
- ????????????????result?+=?str.charAt(i);??
- ????????????}??
- ????????}??
- ????????return?result;??
- ????}??
- ???
- ????/**?
- ?????*?功能:cs串中是否一個都不包含字符數組searchChars中的字符。?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@param?cs?
- ?????*????????????字符串?
- ?????*?@param?searchChars?
- ?????*????????????字符數組?
- ?????*?@return?boolean?都不包含返回true,否則返回false。?
- ?????*?@date?2014年06月24日?
- ?????*/??
- ????public?static?boolean?containsNone(CharSequence?cs,?char...?searchChars)?{??
- ????????if?(cs?==?null?||?searchChars?==?null)?{??
- ????????????return?true;??
- ????????}??
- ????????int?csLen?=?cs.length();??
- ????????int?csLast?=?csLen?-?1;??
- ????????int?searchLen?=?searchChars.length;??
- ????????int?searchLast?=?searchLen?-?1;??
- ????????for?(int?i?=?0;?i?<?csLen;?i++)?{??
- ????????????char?ch?=?cs.charAt(i);??
- ????????????for?(int?j?=?0;?j?<?searchLen;?j++)?{??
- ????????????????if?(searchChars[j]?==?ch)?{??
- ????????????????????if?(Character.isHighSurrogate(ch))?{??
- ????????????????????????if?(j?==?searchLast)?{??
- ????????????????????????????//?missing?low?surrogate,?fine,?like??
- ????????????????????????????//?String.indexOf(String)??
- ????????????????????????????return?false;??
- ????????????????????????}??
- ????????????????????????if?(i?<?csLast??
- ????????????????????????????????&&?searchChars[j?+?1]?==?cs.charAt(i?+?1))?{??
- ????????????????????????????return?false;??
- ????????????????????????}??
- ????????????????????}?else?{??
- ????????????????????????//?ch?is?in?the?Basic?Multilingual?Plane??
- ????????????????????????return?false;??
- ????????????????????}??
- ????????????????}??
- ????????????}??
- ????????}??
- ????????return?true;??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?編碼為Unicode,格式?'\u0020'.?
- ?????*?</p>?
- ?????*??
- ?????*?@author?宋立君?
- ?????*??
- ?????*?????????<pre>?
- ?????*???CharUtils.unicodeEscaped('?')?=?"\u0020"?
- ?????*???CharUtils.unicodeEscaped('A')?=?"\u0041"?
- ?????*?</pre>?
- ?????*??
- ?????*?@param?ch?
- ?????*????????????源字符串?
- ?????*?@return?轉碼后的字符串?
- ?????*?@date?2014年06月24日?
- ?????*/??
- ????public?static?String?unicodeEscaped(char?ch)?{??
- ????????if?(ch?<?0x10)?{??
- ????????????return?"\\u000"?+?Integer.toHexString(ch);??
- ????????}?else?if?(ch?<?0x100)?{??
- ????????????return?"\\u00"?+?Integer.toHexString(ch);??
- ????????}?else?if?(ch?<?0x1000)?{??
- ????????????return?"\\u0"?+?Integer.toHexString(ch);??
- ????????}??
- ????????return?"\\u"?+?Integer.toHexString(ch);??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?進行tostring操作,如果傳入的是null,返回空字符串。?
- ?????*?</p>?
- ?????*?
- ?????*?<pre>?
- ?????*?ObjectUtils.toString(null)?????????=?""?
- ?????*?ObjectUtils.toString("")???????????=?""?
- ?????*?ObjectUtils.toString("bat")????????=?"bat"?
- ?????*?ObjectUtils.toString(Boolean.TRUE)?=?"true"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?obj?
- ?????*????????????源?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?toString(Object?obj)?{??
- ????????return?obj?==?null???""?:?obj.toString();??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?進行tostring操作,如果傳入的是null,返回指定的默認值。?
- ?????*?</p>?
- ?????*?
- ?????*?<pre>?
- ?????*?ObjectUtils.toString(null,?null)???????????=?null?
- ?????*?ObjectUtils.toString(null,?"null")?????????=?"null"?
- ?????*?ObjectUtils.toString("",?"null")???????????=?""?
- ?????*?ObjectUtils.toString("bat",?"null")????????=?"bat"?
- ?????*?ObjectUtils.toString(Boolean.TRUE,?"null")?=?"true"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?obj?
- ?????*????????????源?
- ?????*?@param?nullStr?
- ?????*????????????如果obj為null時返回這個指定值?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?toString(Object?obj,?String?nullStr)?{??
- ????????return?obj?==?null???nullStr?:?obj.toString();??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?只從源字符串中移除指定開頭子字符串.?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.removeStart(null,?*)??????=?null?
- ?????*?StringUtil.removeStart("",?*)????????=?""?
- ?????*?StringUtil.removeStart(*,?null)??????=?*?
- ?????*?StringUtil.removeStart("www.domain.com",?"www.")???=?"domain.com"?
- ?????*?StringUtil.removeStart("domain.com",?"www.")???????=?"domain.com"?
- ?????*?StringUtil.removeStart("www.domain.com",?"domain")?=?"www.domain.com"?
- ?????*?StringUtil.removeStart("abc",?"")????=?"abc"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?remove?
- ?????*????????????將要被移除的子字符串?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?removeStart(String?str,?String?remove)?{??
- ????????if?(isEmpty(str)?||?isEmpty(remove))?{??
- ????????????return?str;??
- ????????}??
- ????????if?(str.startsWith(remove))?{??
- ????????????return?str.substring(remove.length());??
- ????????}??
- ????????return?str;??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?只從源字符串中移除指定結尾的子字符串.?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.removeEnd(null,?*)??????=?null?
- ?????*?StringUtil.removeEnd("",?*)????????=?""?
- ?????*?StringUtil.removeEnd(*,?null)??????=?*?
- ?????*?StringUtil.removeEnd("www.domain.com",?".com.")??=?"www.domain.com"?
- ?????*?StringUtil.removeEnd("www.domain.com",?".com")???=?"www.domain"?
- ?????*?StringUtil.removeEnd("www.domain.com",?"domain")?=?"www.domain.com"?
- ?????*?StringUtil.removeEnd("abc",?"")????=?"abc"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?remove?
- ?????*????????????將要被移除的子字符串?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?removeEnd(String?str,?String?remove)?{??
- ????????if?(isEmpty(str)?||?isEmpty(remove))?{??
- ????????????return?str;??
- ????????}??
- ????????if?(str.endsWith(remove))?{??
- ????????????return?str.substring(0,?str.length()?-?remove.length());??
- ????????}??
- ????????return?str;??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?將一個字符串重復N次?
- ?????*?</p>?
- ?????*?
- ?????*?<pre>?
- ?????*?StringUtil.repeat(null,?2)?=?null?
- ?????*?StringUtil.repeat("",?0)???=?""?
- ?????*?StringUtil.repeat("",?2)???=?""?
- ?????*?StringUtil.repeat("a",?3)??=?"aaa"?
- ?????*?StringUtil.repeat("ab",?2)?=?"abab"?
- ?????*?StringUtil.repeat("a",?-2)?=?""?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?repeat?
- ?????*????????????重復的次數?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?repeat(String?str,?int?repeat)?{??
- ????????//?Performance?tuned?for?2.0?(JDK1.4)??
- ???
- ????????if?(str?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????if?(repeat?<=?0)?{??
- ????????????return?EMPTY;??
- ????????}??
- ????????int?inputLength?=?str.length();??
- ????????if?(repeat?==?1?||?inputLength?==?0)?{??
- ????????????return?str;??
- ????????}??
- ????????if?(inputLength?==?1?&&?repeat?<=?PAD_LIMIT)?{??
- ????????????return?repeat(str.charAt(0),?repeat);??
- ????????}??
- ???
- ????????int?outputLength?=?inputLength?*?repeat;??
- ????????switch?(inputLength)?{??
- ????????case?1:??
- ????????????return?repeat(str.charAt(0),?repeat);??
- ????????case?2:??
- ????????????char?ch0?=?str.charAt(0);??
- ????????????char?ch1?=?str.charAt(1);??
- ????????????char[]?output2?=?new?char[outputLength];??
- ????????????for?(int?i?=?repeat?*?2?-?2;?i?>=?0;?i--,?i--)?{??
- ????????????????output2[i]?=?ch0;??
- ????????????????output2[i?+?1]?=?ch1;??
- ????????????}??
- ????????????return?new?String(output2);??
- ????????default:??
- ????????????StringBuilder?buf?=?new?StringBuilder(outputLength);??
- ????????????for?(int?i?=?0;?i?<?repeat;?i++)?{??
- ????????????????buf.append(str);??
- ????????????}??
- ????????????return?buf.toString();??
- ????????}??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?將一個字符串重復N次,并且中間加上指定的分隔符?
- ?????*?</p>?
- ?????*?
- ?????*?<pre>?
- ?????*?StringUtil.repeat(null,?null,?2)?=?null?
- ?????*?StringUtil.repeat(null,?"x",?2)??=?null?
- ?????*?StringUtil.repeat("",?null,?0)???=?""?
- ?????*?StringUtil.repeat("",?"",?2)?????=?""?
- ?????*?StringUtil.repeat("",?"x",?3)????=?"xxx"?
- ?????*?StringUtil.repeat("?",?",?",?3)??=?"?,??,??"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?separator?
- ?????*????????????分隔符?
- ?????*?@param?repeat?
- ?????*????????????重復次數?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?repeat(String?str,?String?separator,?int?repeat)?{??
- ????????if?(str?==?null?||?separator?==?null)?{??
- ????????????return?repeat(str,?repeat);??
- ????????}?else?{??
- ????????????//?given?that?repeat(String,?int)?is?quite?optimized,?better?to?rely??
- ????????????//?on?it?than?try?and?splice?this?into?it??
- ????????????String?result?=?repeat(str?+?separator,?repeat);??
- ????????????return?removeEnd(result,?separator);??
- ????????}??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?將某個字符重復N次.?
- ?????*?</p>?
- ?????*?
- ?????*?@param?ch?
- ?????*????????????某個字符?
- ?????*?@param?repeat?
- ?????*????????????重復次數?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?repeat(char?ch,?int?repeat)?{??
- ????????char[]?buf?=?new?char[repeat];??
- ????????for?(int?i?=?repeat?-?1;?i?>=?0;?i--)?{??
- ????????????buf[i]?=?ch;??
- ????????}??
- ????????return?new?String(buf);??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?字符串長度達不到指定長度時,在字符串右邊補指定的字符.?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.rightPad(null,?*,?*)?????=?null?
- ?????*?StringUtil.rightPad("",?3,?'z')?????=?"zzz"?
- ?????*?StringUtil.rightPad("bat",?3,?'z')??=?"bat"?
- ?????*?StringUtil.rightPad("bat",?5,?'z')??=?"batzz"?
- ?????*?StringUtil.rightPad("bat",?1,?'z')??=?"bat"?
- ?????*?StringUtil.rightPad("bat",?-1,?'z')?=?"bat"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?size?
- ?????*????????????指定的長度?
- ?????*?@param?padChar?
- ?????*????????????進行補充的字符?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?rightPad(String?str,?int?size,?char?padChar)?{??
- ????????if?(str?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????int?pads?=?size?-?str.length();??
- ????????if?(pads?<=?0)?{??
- ????????????return?str;?//?returns?original?String?when?possible??
- ????????}??
- ????????if?(pads?>?PAD_LIMIT)?{??
- ????????????return?rightPad(str,?size,?String.valueOf(padChar));??
- ????????}??
- ????????return?str.concat(repeat(padChar,?pads));??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?擴大字符串長度,從左邊補充指定字符?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.rightPad(null,?*,?*)??????=?null?
- ?????*?StringUtil.rightPad("",?3,?"z")??????=?"zzz"?
- ?????*?StringUtil.rightPad("bat",?3,?"yz")??=?"bat"?
- ?????*?StringUtil.rightPad("bat",?5,?"yz")??=?"batyz"?
- ?????*?StringUtil.rightPad("bat",?8,?"yz")??=?"batyzyzy"?
- ?????*?StringUtil.rightPad("bat",?1,?"yz")??=?"bat"?
- ?????*?StringUtil.rightPad("bat",?-1,?"yz")?=?"bat"?
- ?????*?StringUtil.rightPad("bat",?5,?null)??=?"bat??"?
- ?????*?StringUtil.rightPad("bat",?5,?"")????=?"bat??"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?size?
- ?????*????????????擴大后的長度?
- ?????*?@param?padStr?
- ?????*????????????在右邊補充的字符串?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?rightPad(String?str,?int?size,?String?padStr)?{??
- ????????if?(str?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????if?(isEmpty(padStr))?{??
- ????????????padStr?=?"?";??
- ????????}??
- ????????int?padLen?=?padStr.length();??
- ????????int?strLen?=?str.length();??
- ????????int?pads?=?size?-?strLen;??
- ????????if?(pads?<=?0)?{??
- ????????????return?str;?//?returns?original?String?when?possible??
- ????????}??
- ????????if?(padLen?==?1?&&?pads?<=?PAD_LIMIT)?{??
- ????????????return?rightPad(str,?size,?padStr.charAt(0));??
- ????????}??
- ???
- ????????if?(pads?==?padLen)?{??
- ????????????return?str.concat(padStr);??
- ????????}?else?if?(pads?<?padLen)?{??
- ????????????return?str.concat(padStr.substring(0,?pads));??
- ????????}?else?{??
- ????????????char[]?padding?=?new?char[pads];??
- ????????????char[]?padChars?=?padStr.toCharArray();??
- ????????????for?(int?i?=?0;?i?<?pads;?i++)?{??
- ????????????????padding[i]?=?padChars[i?%?padLen];??
- ????????????}??
- ????????????return?str.concat(new?String(padding));??
- ????????}??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?擴大字符串長度,從左邊補充空格?
- ?????*?</p>?
- ?????*?
- ?????*?<pre>?
- ?????*?StringUtil.leftPad(null,?*)???=?null?
- ?????*?StringUtil.leftPad("",?3)?????=?"???"?
- ?????*?StringUtil.leftPad("bat",?3)??=?"bat"?
- ?????*?StringUtil.leftPad("bat",?5)??=?"??bat"?
- ?????*?StringUtil.leftPad("bat",?1)??=?"bat"?
- ?????*?StringUtil.leftPad("bat",?-1)?=?"bat"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?size?
- ?????*????????????擴大后的長度?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?leftPad(String?str,?int?size)?{??
- ????????return?leftPad(str,?size,?'?');??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?擴大字符串長度,從左邊補充指定的字符?
- ?????*?</p>?
- ?????*?
- ?????*?<pre>?
- ?????*?StringUtil.leftPad(null,?*,?*)?????=?null?
- ?????*?StringUtil.leftPad("",?3,?'z')?????=?"zzz"?
- ?????*?StringUtil.leftPad("bat",?3,?'z')??=?"bat"?
- ?????*?StringUtil.leftPad("bat",?5,?'z')??=?"zzbat"?
- ?????*?StringUtil.leftPad("bat",?1,?'z')??=?"bat"?
- ?????*?StringUtil.leftPad("bat",?-1,?'z')?=?"bat"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?size?
- ?????*????????????擴大后的長度?
- ?????*?@param?padStr?
- ?????*????????????補充的字符?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?leftPad(String?str,?int?size,?char?padChar)?{??
- ????????if?(str?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????int?pads?=?size?-?str.length();??
- ????????if?(pads?<=?0)?{??
- ????????????return?str;?//?returns?original?String?when?possible??
- ????????}??
- ????????if?(pads?>?PAD_LIMIT)?{??
- ????????????return?leftPad(str,?size,?String.valueOf(padChar));??
- ????????}??
- ????????return?repeat(padChar,?pads).concat(str);??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?擴大字符串長度,從左邊補充指定的字符?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.leftPad(null,?*,?*)??????=?null?
- ?????*?StringUtil.leftPad("",?3,?"z")??????=?"zzz"?
- ?????*?StringUtil.leftPad("bat",?3,?"yz")??=?"bat"?
- ?????*?StringUtil.leftPad("bat",?5,?"yz")??=?"yzbat"?
- ?????*?StringUtil.leftPad("bat",?8,?"yz")??=?"yzyzybat"?
- ?????*?StringUtil.leftPad("bat",?1,?"yz")??=?"bat"?
- ?????*?StringUtil.leftPad("bat",?-1,?"yz")?=?"bat"?
- ?????*?StringUtil.leftPad("bat",?5,?null)??=?"??bat"?
- ?????*?StringUtil.leftPad("bat",?5,?"")????=?"??bat"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?size?
- ?????*????????????擴大后的長度?
- ?????*?@param?padStr?
- ?????*????????????補充的字符串?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?leftPad(String?str,?int?size,?String?padStr)?{??
- ????????if?(str?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????if?(isEmpty(padStr))?{??
- ????????????padStr?=?"?";??
- ????????}??
- ????????int?padLen?=?padStr.length();??
- ????????int?strLen?=?str.length();??
- ????????int?pads?=?size?-?strLen;??
- ????????if?(pads?<=?0)?{??
- ????????????return?str;?//?returns?original?String?when?possible??
- ????????}??
- ????????if?(padLen?==?1?&&?pads?<=?PAD_LIMIT)?{??
- ????????????return?leftPad(str,?size,?padStr.charAt(0));??
- ????????}??
- ???
- ????????if?(pads?==?padLen)?{??
- ????????????return?padStr.concat(str);??
- ????????}?else?if?(pads?<?padLen)?{??
- ????????????return?padStr.substring(0,?pads).concat(str);??
- ????????}?else?{??
- ????????????char[]?padding?=?new?char[pads];??
- ????????????char[]?padChars?=?padStr.toCharArray();??
- ????????????for?(int?i?=?0;?i?<?pads;?i++)?{??
- ????????????????padding[i]?=?padChars[i?%?padLen];??
- ????????????}??
- ????????????return?new?String(padding).concat(str);??
- ????????}??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?擴大字符串長度并將現在的字符串居中,被擴大部分用空格填充。?
- ?????*?<p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.center(null,?*)???=?null?
- ?????*?StringUtil.center("",?4)?????=?"????"?
- ?????*?StringUtil.center("ab",?-1)??=?"ab"?
- ?????*?StringUtil.center("ab",?4)???=?"?ab?"?
- ?????*?StringUtil.center("abcd",?2)?=?"abcd"?
- ?????*?StringUtil.center("a",?4)????=?"?a??"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?size?
- ?????*????????????擴大后的長度?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?center(String?str,?int?size)?{??
- ????????return?center(str,?size,?'?');??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?將字符串長度修改為指定長度,并進行居中顯示。?
- ?????*?</p>?
- ?????*?
- ?????*?<pre>?
- ?????*?StringUtil.center(null,?*,?*)?????=?null?
- ?????*?StringUtil.center("",?4,?'?')?????=?"????"?
- ?????*?StringUtil.center("ab",?-1,?'?')??=?"ab"?
- ?????*?StringUtil.center("ab",?4,?'?')???=?"?ab"?
- ?????*?StringUtil.center("abcd",?2,?'?')?=?"abcd"?
- ?????*?StringUtil.center("a",?4,?'?')????=?"?a??"?
- ?????*?StringUtil.center("a",?4,?'y')????=?"yayy"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?size?
- ?????*????????????指定的長度?
- ?????*?@param?padStr?
- ?????*????????????長度不夠時補充的字符串?
- ?????*?@return?String?
- ?????*?@throws?IllegalArgumentException?
- ?????*?????????????如果被補充字符串為?null或者?empty?
- ?????*/??
- ????public?static?String?center(String?str,?int?size,?char?padChar)?{??
- ????????if?(str?==?null?||?size?<=?0)?{??
- ????????????return?str;??
- ????????}??
- ????????int?strLen?=?str.length();??
- ????????int?pads?=?size?-?strLen;??
- ????????if?(pads?<=?0)?{??
- ????????????return?str;??
- ????????}??
- ????????str?=?leftPad(str,?strLen?+?pads?/?2,?padChar);??
- ????????str?=?rightPad(str,?size,?padChar);??
- ????????return?str;??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?將字符串長度修改為指定長度,并進行居中顯示。?
- ?????*?</p>?
- ?????*?
- ?????*?<pre>?
- ?????*?StringUtil.center(null,?*,?*)?????=?null?
- ?????*?StringUtil.center("",?4,?"?")?????=?"????"?
- ?????*?StringUtil.center("ab",?-1,?"?")??=?"ab"?
- ?????*?StringUtil.center("ab",?4,?"?")???=?"?ab"?
- ?????*?StringUtil.center("abcd",?2,?"?")?=?"abcd"?
- ?????*?StringUtil.center("a",?4,?"?")????=?"?a??"?
- ?????*?StringUtil.center("a",?4,?"yz")???=?"yayz"?
- ?????*?StringUtil.center("abc",?7,?null)?=?"??abc??"?
- ?????*?StringUtil.center("abc",?7,?"")???=?"??abc??"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?size?
- ?????*????????????指定的長度?
- ?????*?@param?padStr?
- ?????*????????????長度不夠時補充的字符串?
- ?????*?@return?String?
- ?????*?@throws?IllegalArgumentException?
- ?????*?????????????如果被補充字符串為?null或者?empty?
- ?????*/??
- ????public?static?String?center(String?str,?int?size,?String?padStr)?{??
- ????????if?(str?==?null?||?size?<=?0)?{??
- ????????????return?str;??
- ????????}??
- ????????if?(isEmpty(padStr))?{??
- ????????????padStr?=?"?";??
- ????????}??
- ????????int?strLen?=?str.length();??
- ????????int?pads?=?size?-?strLen;??
- ????????if?(pads?<=?0)?{??
- ????????????return?str;??
- ????????}??
- ????????str?=?leftPad(str,?strLen?+?pads?/?2,?padStr);??
- ????????str?=?rightPad(str,?size,?padStr);??
- ????????return?str;??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?檢查字符串是否全部為小寫.?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.isAllLowerCase(null)???=?false?
- ?????*?StringUtil.isAllLowerCase("")?????=?false?
- ?????*?StringUtil.isAllLowerCase("??")???=?false?
- ?????*?StringUtil.isAllLowerCase("abc")??=?true?
- ?????*?StringUtil.isAllLowerCase("abC")?=?false?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?cs?
- ?????*????????????源字符串?
- ?????*?@return?String?
- ?????*/??
- ????public?static?boolean?isAllLowerCase(String?cs)?{??
- ????????if?(cs?==?null?||?isEmpty(cs))?{??
- ????????????return?false;??
- ????????}??
- ????????int?sz?=?cs.length();??
- ????????for?(int?i?=?0;?i?<?sz;?i++)?{??
- ????????????if?(Character.isLowerCase(cs.charAt(i))?==?false)?{??
- ????????????????return?false;??
- ????????????}??
- ????????}??
- ????????return?true;??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?檢查是否都是大寫.?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.isAllUpperCase(null)???=?false?
- ?????*?StringUtil.isAllUpperCase("")?????=?false?
- ?????*?StringUtil.isAllUpperCase("??")???=?false?
- ?????*?StringUtil.isAllUpperCase("ABC")??=?true?
- ?????*?StringUtil.isAllUpperCase("aBC")?=?false?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?cs?
- ?????*????????????源字符串?
- ?????*?@return?String?
- ?????*/??
- ????public?static?boolean?isAllUpperCase(String?cs)?{??
- ????????if?(cs?==?null?||?StringUtil.isEmpty(cs))?{??
- ????????????return?false;??
- ????????}??
- ????????int?sz?=?cs.length();??
- ????????for?(int?i?=?0;?i?<?sz;?i++)?{??
- ????????????if?(Character.isUpperCase(cs.charAt(i))?==?false)?{??
- ????????????????return?false;??
- ????????????}??
- ????????}??
- ????????return?true;??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?反轉字符串.?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.reverse(null)??=?null?
- ?????*?StringUtil.reverse("")????=?""?
- ?????*?StringUtil.reverse("bat")?=?"tab"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?reverse(String?str)?{??
- ????????if?(str?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????return?new?StringBuilder(str).reverse().toString();??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?字符串達不到一定長度時在右邊補空白.?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.rightPad(null,?*)???=?null?
- ?????*?StringUtil.rightPad("",?3)?????=?"???"?
- ?????*?StringUtil.rightPad("bat",?3)??=?"bat"?
- ?????*?StringUtil.rightPad("bat",?5)??=?"bat??"?
- ?????*?StringUtil.rightPad("bat",?1)??=?"bat"?
- ?????*?StringUtil.rightPad("bat",?-1)?=?"bat"?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?size?
- ?????*????????????指定的長度?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?rightPad(String?str,?int?size)?{??
- ????????return?rightPad(str,?size,?'?');??
- ????}??
- ???
- ????/**?
- ?????*?從右邊截取字符串.</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.right(null,?*)????=?null?
- ?????*?StringUtil.right(*,?-ve)?????=?""?
- ?????*?StringUtil.right("",?*)??????=?""?
- ?????*?StringUtil.right("abc",?0)???=?""?
- ?????*?StringUtil.right("abc",?2)???=?"bc"?
- ?????*?StringUtil.right("abc",?4)???=?"abc"?
- ?????*?</pre>?
- ?????*??
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?len?
- ?????*????????????長度?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?right(String?str,?int?len)?{??
- ????????if?(str?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????if?(len?<?0)?{??
- ????????????return?EMPTY;??
- ????????}??
- ????????if?(str.length()?<=?len)?{??
- ????????????return?str;??
- ????????}??
- ????????return?str.substring(str.length()?-?len);??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?截取一個字符串的前幾個.?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.left(null,?*)????=?null?
- ?????*?StringUtil.left(*,?-ve)?????=?""?
- ?????*?StringUtil.left("",?*)??????=?""?
- ?????*?StringUtil.left("abc",?0)???=?""?
- ?????*?StringUtil.left("abc",?2)???=?"ab"?
- ?????*?StringUtil.left("abc",?4)???=?"abc"?
- ?????*?</pre>?
- ?????*??
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?len?
- ?????*????????????截取的長度?
- ?????*?@return?the?String?
- ?????*/??
- ????public?static?String?left(String?str,?int?len)?{??
- ????????if?(str?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????if?(len?<?0)?{??
- ????????????return?EMPTY;??
- ????????}??
- ????????if?(str.length()?<=?len)?{??
- ????????????return?str;??
- ????????}??
- ????????return?str.substring(0,?len);??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?得到tag字符串中間的子字符串,只返回第一個匹配項。?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.substringBetween(null,?*)????????????=?null?
- ?????*?StringUtil.substringBetween("",?"")?????????????=?""?
- ?????*?StringUtil.substringBetween("",?"tag")??????????=?null?
- ?????*?StringUtil.substringBetween("tagabctag",?null)??=?null?
- ?????*?StringUtil.substringBetween("tagabctag",?"")????=?""?
- ?????*?StringUtil.substringBetween("tagabctag",?"tag")?=?"abc"?
- ?????*?</pre>?
- ?????*??
- ?????*?@param?str?
- ?????*????????????源字符串。?
- ?????*?@param?tag?
- ?????*????????????標識字符串。?
- ?????*?@return?String?子字符串,?如果沒有符合要求的,返回{@code?null}。?
- ?????*/??
- ????public?static?String?substringBetween(String?str,?String?tag)?{??
- ????????return?substringBetween(str,?tag,?tag);??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?得到兩個字符串中間的子字符串,只返回第一個匹配項。?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.substringBetween("wx[b]yz",?"[",?"]")?=?"b"?
- ?????*?StringUtil.substringBetween(null,?*,?*)??????????=?null?
- ?????*?StringUtil.substringBetween(*,?null,?*)??????????=?null?
- ?????*?StringUtil.substringBetween(*,?*,?null)??????????=?null?
- ?????*?StringUtil.substringBetween("",?"",?"")??????????=?""?
- ?????*?StringUtil.substringBetween("",?"",?"]")?????????=?null?
- ?????*?StringUtil.substringBetween("",?"[",?"]")????????=?null?
- ?????*?StringUtil.substringBetween("yabcz",?"",?"")?????=?""?
- ?????*?StringUtil.substringBetween("yabcz",?"y",?"z")???=?"abc"?
- ?????*?StringUtil.substringBetween("yabczyabcz",?"y",?"z")???=?"abc"?
- ?????*?</pre>?
- ?????*??
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?open?
- ?????*????????????起字符串。?
- ?????*?@param?close?
- ?????*????????????末字符串。?
- ?????*?@return?String?子字符串,?如果沒有符合要求的,返回{@code?null}。?
- ?????*/??
- ????public?static?String?substringBetween(String?str,?String?open,?String?close)?{??
- ????????if?(str?==?null?||?open?==?null?||?close?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????int?start?=?str.indexOf(open);??
- ????????if?(start?!=?INDEX_NOT_FOUND)?{??
- ????????????int?end?=?str.indexOf(close,?start?+?open.length());??
- ????????????if?(end?!=?INDEX_NOT_FOUND)?{??
- ????????????????return?str.substring(start?+?open.length(),?end);??
- ????????????}??
- ????????}??
- ????????return?null;??
- ????}??
- ???
- ????/**?
- ?????*?<p>?
- ?????*?得到兩個字符串中間的子字符串,所有匹配項組合為數組并返回。?
- ?????*?</p>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.substringsBetween("[a][b][c]",?"[",?"]")?=?["a","b","c"]?
- ?????*?StringUtil.substringsBetween(null,?*,?*)????????????=?null?
- ?????*?StringUtil.substringsBetween(*,?null,?*)????????????=?null?
- ?????*?StringUtil.substringsBetween(*,?*,?null)????????????=?null?
- ?????*?StringUtil.substringsBetween("",?"[",?"]")??????????=?[]?
- ?????*?</pre>?
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@param?open?
- ?????*????????????起字符串。?
- ?????*?@param?close?
- ?????*????????????末字符串。?
- ?????*?@return?String?子字符串數組,?如果沒有符合要求的,返回{@code?null}。?
- ?????*/??
- ????public?static?String[]?substringsBetween(String?str,?String?open,??
- ????????????String?close)?{??
- ????????if?(str?==?null?||?isEmpty(open)?||?isEmpty(close))?{??
- ????????????return?null;??
- ????????}??
- ????????int?strLen?=?str.length();??
- ????????if?(strLen?==?0)?{??
- ????????????return?new?String[0];??
- ????????}??
- ????????int?closeLen?=?close.length();??
- ????????int?openLen?=?open.length();??
- ????????List<String>?list?=?new?ArrayList<String>();??
- ????????int?pos?=?0;??
- ????????while?(pos?<?strLen?-?closeLen)?{??
- ????????????int?start?=?str.indexOf(open,?pos);??
- ????????????if?(start?<?0)?{??
- ????????????????break;??
- ????????????}??
- ????????????start?+=?openLen;??
- ????????????int?end?=?str.indexOf(close,?start);??
- ????????????if?(end?<?0)?{??
- ????????????????break;??
- ????????????}??
- ????????????list.add(str.substring(start,?end));??
- ????????????pos?=?end?+?closeLen;??
- ????????}??
- ????????if?(list.isEmpty())?{??
- ????????????return?null;??
- ????????}??
- ????????return?list.toArray(new?String[list.size()]);??
- ????}??
- ???
- ????/**?
- ?????*?功能:切換字符串中的所有字母大小寫。<br/>?
- ?????*??
- ?????*?<pre>?
- ?????*?StringUtil.swapCase(null)?????????????????=?null?
- ?????*?StringUtil.swapCase("")???????????????????=?""?
- ?????*?StringUtil.swapCase("The?dog?has?a?BONE")?=?"tHE?DOG?HAS?A?bone"?
- ?????*?</pre>?
- ?????*??
- ?????*?
- ?????*?@param?str?
- ?????*????????????源字符串?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?swapCase(String?str)?{??
- ????????if?(StringUtil.isEmpty(str))?{??
- ????????????return?str;??
- ????????}??
- ????????char[]?buffer?=?str.toCharArray();??
- ???
- ????????boolean?whitespace?=?true;??
- ???
- ????????for?(int?i?=?0;?i?<?buffer.length;?i++)?{??
- ????????????char?ch?=?buffer[i];??
- ????????????if?(Character.isUpperCase(ch))?{??
- ????????????????buffer[i]?=?Character.toLowerCase(ch);??
- ????????????????whitespace?=?false;??
- ????????????}?else?if?(Character.isTitleCase(ch))?{??
- ????????????????buffer[i]?=?Character.toLowerCase(ch);??
- ????????????????whitespace?=?false;??
- ????????????}?else?if?(Character.isLowerCase(ch))?{??
- ????????????????if?(whitespace)?{??
- ????????????????????buffer[i]?=?Character.toTitleCase(ch);??
- ????????????????????whitespace?=?false;??
- ????????????????}?else?{??
- ????????????????????buffer[i]?=?Character.toUpperCase(ch);??
- ????????????????}??
- ????????????}?else?{??
- ????????????????whitespace?=?Character.isWhitespace(ch);??
- ????????????}??
- ????????}??
- ????????return?new?String(buffer);??
- ????}??
- ???
- ????/**?
- ?????*?功能:截取出最后一個標志位之后的字符串.<br/>?
- ?????*?如果sourceStr為empty或者expr為null,直接返回源字符串。<br/>?
- ?????*?如果expr長度為0,直接返回sourceStr。<br/>?
- ?????*?如果expr在sourceStr中不存在,直接返回sourceStr。<br/>?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?sourceStr?
- ?????*????????????被截取的字符串?
- ?????*?@param?expr?
- ?????*????????????分隔符?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?substringAfterLast(String?sourceStr,?String?expr)?{??
- ????????if?(isEmpty(sourceStr)?||?expr?==?null)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????if?(expr.length()?==?0)?{??
- ????????????return?sourceStr;??
- ????????}??
- ???
- ????????int?pos?=?sourceStr.lastIndexOf(expr);??
- ????????if?(pos?==?-1)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????return?sourceStr.substring(pos?+?expr.length());??
- ????}??
- ???
- ????/**?
- ?????*?功能:截取出最后一個標志位之前的字符串.<br/>?
- ?????*?如果sourceStr為empty或者expr為null,直接返回源字符串。<br/>?
- ?????*?如果expr長度為0,直接返回sourceStr。<br/>?
- ?????*?如果expr在sourceStr中不存在,直接返回sourceStr。<br/>?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?sourceStr?
- ?????*????????????被截取的字符串?
- ?????*?@param?expr?
- ?????*????????????分隔符?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?substringBeforeLast(String?sourceStr,?String?expr)?{??
- ????????if?(isEmpty(sourceStr)?||?expr?==?null)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????if?(expr.length()?==?0)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????int?pos?=?sourceStr.lastIndexOf(expr);??
- ????????if?(pos?==?-1)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????return?sourceStr.substring(0,?pos);??
- ????}??
- ???
- ????/**?
- ?????*?功能:截取出第一個標志位之后的字符串.<br/>?
- ?????*?如果sourceStr為empty或者expr為null,直接返回源字符串。<br/>?
- ?????*?如果expr長度為0,直接返回sourceStr。<br/>?
- ?????*?如果expr在sourceStr中不存在,直接返回sourceStr。<br/>?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?sourceStr?
- ?????*????????????被截取的字符串?
- ?????*?@param?expr?
- ?????*????????????分隔符?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?substringAfter(String?sourceStr,?String?expr)?{??
- ????????if?(isEmpty(sourceStr)?||?expr?==?null)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????if?(expr.length()?==?0)?{??
- ????????????return?sourceStr;??
- ????????}??
- ???
- ????????int?pos?=?sourceStr.indexOf(expr);??
- ????????if?(pos?==?-1)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????return?sourceStr.substring(pos?+?expr.length());??
- ????}??
- ???
- ????/**?
- ?????*?功能:截取出第一個標志位之前的字符串.<br/>?
- ?????*?如果sourceStr為empty或者expr為null,直接返回源字符串。<br/>?
- ?????*?如果expr長度為0,直接返回sourceStr。<br/>?
- ?????*?如果expr在sourceStr中不存在,直接返回sourceStr。<br/>?
- ?????*?如果expr在sourceStr中存在不止一個,以第一個位置為準。?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?sourceStr?
- ?????*????????????被截取的字符串?
- ?????*?@param?expr?
- ?????*????????????分隔符?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?substringBefore(String?sourceStr,?String?expr)?{??
- ????????if?(isEmpty(sourceStr)?||?expr?==?null)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????if?(expr.length()?==?0)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????int?pos?=?sourceStr.indexOf(expr);??
- ????????if?(pos?==?-1)?{??
- ????????????return?sourceStr;??
- ????????}??
- ????????return?sourceStr.substring(0,?pos);??
- ????}??
- ???
- ????/**?
- ?????*?功能:檢查這個字符串是不是空字符串。<br/>?
- ?????*?如果這個字符串為null或者trim后為空字符串則返回true,否則返回false。?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?chkStr?
- ?????*????????????被檢查的字符串?
- ?????*?@return?boolean?
- ?????*/??
- ????public?static?boolean?isEmpty(String?chkStr)?{??
- ????????if?(chkStr?==?null)?{??
- ????????????return?true;??
- ????????}?else?{??
- ????????????return?"".equals(chkStr.trim())???true?:?false;??
- ????????}??
- ????}??
- ???
- ????/**?
- ?????*?如果字符串沒有超過最長顯示長度返回原字符串,否則從開頭截取指定長度并加...返回。?
- ?????*??
- ?????*?@param?str?
- ?????*????????????原字符串?
- ?????*?@param?length?
- ?????*????????????字符串最長顯示的長度?
- ?????*?@return?轉換后的字符串?
- ?????*/??
- ????public?static?String?trimString(String?str,?int?length)?{??
- ????????if?(str?==?null)?{??
- ????????????return?"";??
- ????????}?else?if?(str.length()?>?length)?{??
- ????????????return?str.substring(0,?length?-?3)?+?"...";??
- ????????}?else?{??
- ????????????return?str;??
- ????????}??
- ????}??
- ???
- }??
二、MD5
- package?com.mkyong.common;??
- ??
- import?java.io.File;??
- import?java.io.FileInputStream;??
- import?java.io.IOException;??
- import?java.nio.MappedByteBuffer;??
- import?java.nio.channels.FileChannel;??
- import?java.security.MessageDigest;??
- import?java.security.NoSuchAlgorithmException;??
- ??
- /**?
- ?*??
- ?*?String工具類.?<br>?
- ?*??
- ?*?@author?宋立君?
- ?*?@date?2014年06月24日?
- ?*/??
- public?class?MD5Util?{??
- ??
- ????protected?static?char?hexDigits[]?=?{?'0',?'1',?'2',?'3',?'4',?'5',?'6',??
- ????????????'7',?'8',?'9',?'a',?'b',?'c',?'d',?'e',?'f'?};??
- ??
- ????protected?static?MessageDigest?messagedigest?=?null;??
- ??
- ????static?{??
- ????????try?{??
- ????????????messagedigest?=?MessageDigest.getInstance("MD5");??
- ????????}?catch?(NoSuchAlgorithmException?nsaex)?{??
- ????????????System.err.println(MD5Util.class.getName()??
- ????????????????????+?"初始化失敗,MessageDigest不支持MD5Util。");??
- ????????????nsaex.printStackTrace();??
- ????????}??
- ????}??
- ??
- ????/**?
- ?????*?功能:加鹽版的MD5.返回格式為MD5(密碼+{鹽值})?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?password?
- ?????*????????????密碼?
- ?????*?@param?salt?
- ?????*????????????鹽值?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?getMD5StringWithSalt(String?password,?String?salt)?{??
- ????????if?(password?==?null)?{??
- ????????????throw?new?IllegalArgumentException("password不能為null");??
- ????????}??
- ????????if?(StringUtil.isEmpty(salt))?{??
- ????????????throw?new?IllegalArgumentException("salt不能為空");??
- ????????}??
- ????????if?((salt.toString().lastIndexOf("{")?!=?-1)??
- ????????????????||?(salt.toString().lastIndexOf("}")?!=?-1))?{??
- ????????????throw?new?IllegalArgumentException("salt中不能包含?{?或者?}");??
- ????????}??
- ????????return?getMD5String(password?+?"{"?+?salt.toString()?+?"}");??
- ????}??
- ??
- ????/**?
- ?????*?功能:得到文件的md5值。?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?file?
- ?????*????????????文件。?
- ?????*?@return?String?
- ?????*?@throws?IOException?
- ?????*?????????????讀取文件IO異常時。?
- ?????*/??
- ????public?static?String?getFileMD5String(File?file)?throws?IOException?{??
- ????????FileInputStream?in?=?new?FileInputStream(file);??
- ????????FileChannel?ch?=?in.getChannel();??
- ????????MappedByteBuffer?byteBuffer?=?ch.map(FileChannel.MapMode.READ_ONLY,?0,??
- ????????????????file.length());??
- ????????messagedigest.update(byteBuffer);??
- ????????return?bufferToHex(messagedigest.digest());??
- ????}??
- ??
- ????/**?
- ?????*?功能:得到一個字符串的MD5值。?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?str?
- ?????*????????????字符串?
- ?????*?@return?String?
- ?????*/??
- ????public?static?String?getMD5String(String?str)?{??
- ????????return?getMD5String(str.getBytes());??
- ????}??
- ??
- ????private?static?String?getMD5String(byte[]?bytes)?{??
- ????????messagedigest.update(bytes);??
- ????????return?bufferToHex(messagedigest.digest());??
- ????}??
- ??
- ????private?static?String?bufferToHex(byte?bytes[])?{??
- ????????return?bufferToHex(bytes,?0,?bytes.length);??
- ????}??
- ??
- ????private?static?String?bufferToHex(byte?bytes[],?int?m,?int?n)?{??
- ????????StringBuffer?stringbuffer?=?new?StringBuffer(2?*?n);??
- ????????int?k?=?m?+?n;??
- ????????for?(int?l?=?m;?l?<?k;?l++)?{??
- ????????????appendHexPair(bytes[l],?stringbuffer);??
- ????????}??
- ????????return?stringbuffer.toString();??
- ????}??
- ??
- ????private?static?void?appendHexPair(byte?bt,?StringBuffer?stringbuffer)?{??
- ????????char?c0?=?hexDigits[(bt?&?0xf0)?>>?4];??
- ????????char?c1?=?hexDigits[bt?&?0xf];??
- ????????stringbuffer.append(c0);??
- ????????stringbuffer.append(c1);??
- ????}??
- }??
三、File工具類
- package?com.mkyong.common;??
- ??
- import?java.io.ByteArrayInputStream;??
- import?java.io.File;??
- import?java.io.FileInputStream;??
- import?java.io.FileOutputStream;??
- import?java.io.IOException;??
- import?java.io.InputStream;??
- import?java.io.OutputStream;??
- ??
- /**?
- ?*?文件相關操作輔助類。?
- ?*??
- ?*?@author?宋立君?
- ?*?@date?2014年06月24日?
- ?*/??
- public?class?FileUtil?{??
- ????private?static?final?String?FOLDER_SEPARATOR?=?"/";??
- ????private?static?final?char?EXTENSION_SEPARATOR?=?'.';??
- ??
- ????/**?
- ?????*?功能:復制文件或者文件夾。?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?inputFile?
- ?????*????????????源文件?
- ?????*?@param?outputFile?
- ?????*????????????目的文件?
- ?????*?@param?isOverWrite?
- ?????*????????????是否覆蓋(只針對文件)?
- ?????*?@throws?IOException?
- ?????*/??
- ????public?static?void?copy(File?inputFile,?File?outputFile,?boolean?isOverWrite)??
- ????????????throws?IOException?{??
- ????????if?(!inputFile.exists())?{??
- ????????????throw?new?RuntimeException(inputFile.getPath()?+?"源目錄不存在!");??
- ????????}??
- ????????copyPri(inputFile,?outputFile,?isOverWrite);??
- ????}??
- ??
- ????/**?
- ?????*?功能:為copy?做遞歸使用。?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?inputFile?
- ?????*?@param?outputFile?
- ?????*?@param?isOverWrite?
- ?????*?@throws?IOException?
- ?????*/??
- ????private?static?void?copyPri(File?inputFile,?File?outputFile,??
- ????????????boolean?isOverWrite)?throws?IOException?{??
- ????????//?是個文件。??
- ????????if?(inputFile.isFile())?{??
- ????????????copySimpleFile(inputFile,?outputFile,?isOverWrite);??
- ????????}?else?{??
- ????????????//?文件夾??
- ????????????if?(!outputFile.exists())?{??
- ????????????????outputFile.mkdir();??
- ????????????}??
- ????????????//?循環子文件夾??
- ????????????for?(File?child?:?inputFile.listFiles())?{??
- ????????????????copy(child,??
- ????????????????????????new?File(outputFile.getPath()?+?"/"?+?child.getName()),??
- ????????????????????????isOverWrite);??
- ????????????}??
- ????????}??
- ????}??
- ??
- ????/**?
- ?????*?功能:copy單個文件?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?inputFile?
- ?????*????????????源文件?
- ?????*?@param?outputFile?
- ?????*????????????目標文件?
- ?????*?@param?isOverWrite?
- ?????*????????????是否允許覆蓋?
- ?????*?@throws?IOException?
- ?????*/??
- ????private?static?void?copySimpleFile(File?inputFile,?File?outputFile,??
- ????????????boolean?isOverWrite)?throws?IOException?{??
- ????????//?目標文件已經存在??
- ????????if?(outputFile.exists())?{??
- ????????????if?(isOverWrite)?{??
- ????????????????if?(!outputFile.delete())?{??
- ????????????????????throw?new?RuntimeException(outputFile.getPath()?+?"無法覆蓋!");??
- ????????????????}??
- ????????????}?else?{??
- ????????????????//?不允許覆蓋??
- ????????????????return;??
- ????????????}??
- ????????}??
- ????????InputStream?in?=?new?FileInputStream(inputFile);??
- ????????OutputStream?out?=?new?FileOutputStream(outputFile);??
- ????????byte[]?buffer?=?new?byte[1024];??
- ????????int?read?=?0;??
- ????????while?((read?=?in.read(buffer))?!=?-1)?{??
- ????????????out.write(buffer,?0,?read);??
- ????????}??
- ????????in.close();??
- ????????out.close();??
- ????}??
- ??
- ????/**?
- ?????*?功能:刪除文件?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?file?
- ?????*????????????文件?
- ?????*/??
- ????public?static?void?delete(File?file)?{??
- ????????deleteFile(file);??
- ????}??
- ??
- ????/**?
- ?????*?功能:刪除文件,內部遞歸使用?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?file?
- ?????*????????????文件?
- ?????*?@return?boolean?true?刪除成功,false?刪除失敗。?
- ?????*/??
- ????private?static?void?deleteFile(File?file)?{??
- ????????if?(file?==?null?||?!file.exists())?{??
- ????????????return;??
- ????????}??
- ????????//?單文件??
- ????????if?(!file.isDirectory())?{??
- ????????????boolean?delFlag?=?file.delete();??
- ????????????if?(!delFlag)?{??
- ????????????????throw?new?RuntimeException(file.getPath()?+?"刪除失敗!");??
- ????????????}?else?{??
- ????????????????return;??
- ????????????}??
- ????????}??
- ????????//?刪除子目錄??
- ????????for?(File?child?:?file.listFiles())?{??
- ????????????deleteFile(child);??
- ????????}??
- ????????//?刪除自己??
- ????????file.delete();??
- ????}??
- ??
- ????/**?
- ?????*?從文件路徑中抽取文件的擴展名,?例如.?"mypath/myfile.txt"?->?"txt".?*?@author?宋立君?
- ?????*??
- ?????*?@date?2014年06月24日?
- ?????*?@param?文件路徑?
- ?????*?@return?如果path為null,直接返回null。?
- ?????*/??
- ????public?static?String?getFilenameExtension(String?path)?{??
- ????????if?(path?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????int?extIndex?=?path.lastIndexOf(EXTENSION_SEPARATOR);??
- ????????if?(extIndex?==?-1)?{??
- ????????????return?null;??
- ????????}??
- ????????int?folderIndex?=?path.lastIndexOf(FOLDER_SEPARATOR);??
- ????????if?(folderIndex?>?extIndex)?{??
- ????????????return?null;??
- ????????}??
- ????????return?path.substring(extIndex?+?1);??
- ????}??
- ??
- ????/**?
- ?????*?從文件路徑中抽取文件名,?例如:?"mypath/myfile.txt"?->?"myfile.txt"。?*?@author?宋立君?
- ?????*??
- ?????*?@date?2014年06月24日?
- ?????*?@param?path?
- ?????*????????????文件路徑。?
- ?????*?@return?抽取出來的文件名,?如果path為null,直接返回null。?
- ?????*/??
- ????public?static?String?getFilename(String?path)?{??
- ????????if?(path?==?null)?{??
- ????????????return?null;??
- ????????}??
- ????????int?separatorIndex?=?path.lastIndexOf(FOLDER_SEPARATOR);??
- ????????return?(separatorIndex?!=?-1???path.substring(separatorIndex?+?1)??
- ????????????????:?path);??
- ????}??
- ??
- ????/**?
- ?????*?功能:保存文件。?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?content?
- ?????*????????????字節?
- ?????*?@param?file?
- ?????*????????????保存到的文件?
- ?????*?@throws?IOException?
- ?????*/??
- ????public?static?void?save(byte[]?content,?File?file)?throws?IOException?{??
- ????????if?(file?==?null)?{??
- ????????????throw?new?RuntimeException("保存文件不能為空");??
- ????????}??
- ????????if?(content?==?null)?{??
- ????????????throw?new?RuntimeException("文件流不能為空");??
- ????????}??
- ????????InputStream?is?=?new?ByteArrayInputStream(content);??
- ????????save(is,?file);??
- ????}??
- ??
- ????/**?
- ?????*?功能:保存文件?
- ?????*??
- ?????*?@author?宋立君?
- ?????*?@date?2014年06月24日?
- ?????*?@param?streamIn?
- ?????*????????????文件流?
- ?????*?@param?file?
- ?????*????????????保存到的文件?
- ?????*?@throws?IOException?
- ?????*/??
- ????public?static?void?save(InputStream?streamIn,?File?file)?throws?IOException?{??
- ????????if?(file?==?null)?{??
- ????????????throw?new?RuntimeException("保存文件不能為空");??
- ????????}??
- ????????if?(streamIn?==?null)?{??
- ????????????throw?new?RuntimeException("文件流不能為空");??
- ????????}??
- ????????//?輸出流??
- ????????OutputStream?streamOut?=?null;??
- ????????//?文件夾不存在就創建。??
- ????????if?(!file.getParentFile().exists())?{??
- ????????????file.getParentFile().mkdirs();??
- ????????}??
- ????????streamOut?=?new?FileOutputStream(file);??
- ????????int?bytesRead?=?0;??
- ????????byte[]?buffer?=?new?byte[8192];??
- ????????while?((bytesRead?=?streamIn.read(buffer,?0,?8192))?!=?-1)?{??
- ????????????streamOut.write(buffer,?0,?bytesRead);??
- ????????}??
- ????????streamOut.close();??
- ????????streamIn.close();??
- ????}??
- }??