java 板


LINE

Apache Commons 有蛮多小工具的 :) 自己写虽然不算难, 不过别人都写好了也考虑了很多例外的状况 像一个读取文字档成为字串的小工具 public class CommonsIoTest extends TestCase { public static String readFromFile(File file, String enc) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = new BufferedInputStream(new FileInputStream(file)); int c = 0; while((c=in.read())!=-1){ out.write(c); } return new String(out.toByteArray(), enc); } public void testReadFile() throws Exception { File file = new File(CommonsIoTest.class.getResource( "CommonsIoTest.java").getFile()); String text1 = CommonsIoTest.readFromFile(file, "utf-8"); String text2 = FileUtils.readFileToString(file, "utf-8"); assertEquals(text1, text2); } } 即使自己写(先不考虑最佳化), 也不算困难. 但是因为 Commons 提供了一堆小工具, 我们可以简单以一行来达成 而且这些 library 都是大家能取得的, 下面介绍一些我自己常用到的类别: ============================================================= public class CommonsLangTest extends TestCase { String[] array = { "a", "b", "o", "d", "c", "e" }; /** * 找索引 */ public void testArrayIndex() throws Exception { int idx = -1; for (int i = 0; i < array.length; i++) { if ("b".equals(array[i])) { idx = i; break; } } assertEquals(idx, ArrayUtils.indexOf(array, "b")); } /** * 增加元素 */ public void testArrayAdd() throws Exception { String newElement = "orz"; String[] newArray = new String[array.length + 1]; System.arraycopy(array, 0, newArray, 0, array.length); newArray[newArray.length - 1] = newElement; // 因为 Array Object 没有覆写 equals, hashCode 先转成 String 再比较 // assertEquals(newArray, ArrayUtils.add(array, "orz")); assertEquals(ArrayUtils.toString(newArray), ArrayUtils .toString(ArrayUtils.add(array, "orz"))); } /** * 删除索引位置的元素 */ public void testArrayRemoveByIndex() throws Exception { int index = 3; String[] newArray = new String[array.length - 1]; System.arraycopy(array, 0, newArray, 0, index); System.arraycopy(array, index + 1, newArray, index, newArray.length - index); assertEquals(ArrayUtils.toString(newArray), ArrayUtils .toString(ArrayUtils.remove(array, index))); } /** * 找出元素并删除 */ public void testArrayRemoveByElement() throws Exception { int index = -1; for (int i = 0; i < array.length; i++) { if ("o".equals(array[i])) { index = i; break; } } String[] newArray = new String[array.length - 1]; System.arraycopy(array, 0, newArray, 0, index); System.arraycopy(array, index + 1, newArray, index, newArray.length - index); assertEquals(ArrayUtils.toString(newArray), ArrayUtils .toString(ArrayUtils.removeElement(array, "o"))); } /** * 判断空白字串 */ public void testStringIsBlank() throws Exception { String str1 = null; String str2 = " "; String str3 = " \r \r\n "; assertTrue(StringUtils.isBlank(str1)); assertTrue(str1 == null || "".equals(str1.trim())); assertTrue(StringUtils.isBlank(str2)); assertTrue(str2 == null || "".equals(str2.trim())); assertTrue(StringUtils.isBlank(str3)); assertTrue(str3 == null || "".equals(str3.trim())); } /** * 判断字串是否是数值 */ public void testStringIsNumeric() throws Exception { assertTrue(StringUtils.isNumeric("1234")); // 有小数的不行 :) assertFalse(StringUtils.isNumeric("456.3")); } /** * 判断字串是否是数值 */ public void testNumber() throws Exception { assertTrue(NumberUtils.isDigits("12345")); assertFalse(NumberUtils.isDigits("12345L")); assertTrue(NumberUtils.isNumber("12345L")); assertTrue(NumberUtils.isNumber("3e003")); } /** * 字串反转 */ public void testStringReverse() throws Exception { assertEquals(StringUtils.reverse("123456789"), "987654321"); assertEquals(new StringBuffer("123456789").reverse().toString(), "987654321"); } /** * 制作随机字串 */ public void testRandomString() throws Exception { int len = 10; StringBuffer buf = new StringBuffer(); List chars = Arrays.asList(ArrayUtils.toObject("0123456789abcdef" .toCharArray())); while (len-- > 0) { Collections.shuffle(chars); buf.append(chars.get(0)); } // 产生长度 10 的字串 String str1 = RandomStringUtils.random(10, "0123456789abcdef"); assertEquals(10, str1.length()); assertEquals(10, buf.toString().length()); System.out.println(str1); System.out.println(buf.toString()); } /** * 处理特殊字元 */ public void testEscape() throws Exception { String escapedHtml = StringEscapeUtils.escapeHtml("<中文/>"); String escapedSQL = StringEscapeUtils.escapeSql("' OR 1=1 UNION * "); String escapedXml = StringEscapeUtils.escapeXml("<x> a < b </x>"); assertEquals(escapedHtml, "&lt;&#20013;&#25991;/&gt;"); assertEquals(escapedSQL, "'' OR 1=1 UNION * "); assertEquals(escapedXml, "&lt;x&gt; a &lt; b &lt;/x&gt;"); } /** * 格式化日期 */ public void testDateFormat() throws Exception { Date now = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd a K:h", Locale.TAIWAN); df.format(now); System.out.println(df.format(now)); assertEquals(df.format(now), DateFormatUtils.format(now, "yyyy-MM-dd a K:h", Locale.TAIWAN)); } /** * 由字串转 Locale */ public void testLocale() throws Exception { assertEquals(LocaleUtils.toLocale("zh_TW"), Locale.TAIWAN); } /** * 布林判断 */ public void testBoolean() throws Exception { String str = null; assertFalse(BooleanUtils.toBoolean(str)); assertFalse(BooleanUtils.toBoolean("false")); assertFalse(BooleanUtils.toBoolean("x gti")); assertFalse(BooleanUtils.toBoolean("nO")); assertTrue(BooleanUtils.toBoolean("yEs")); assertTrue(BooleanUtils.toBoolean("tRUe")); assertTrue(BooleanUtils.toBoolean("TRUE")); assertTrue(BooleanUtils.toBoolean("true")); assertTrue(BooleanUtils.xor(new boolean[] { true, false })); assertFalse(BooleanUtils.xor(new boolean[] { false, false })); assertFalse(BooleanUtils.xor(new boolean[] { true, true })); } } --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.133.80.216 ※ 编辑: qrtt1 来自: 220.133.80.216 (01/04 23:22)
1F:推 ellinas:感谢分享 我不知道他有这麽多小功能 01/08 21:59







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP