




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
看例子练apache commons之lang篇看commons包时的相关练习,可以用这个包提高编码效率。1. .c4c.test.apache; 2.3. importjava.io.File; 4. importjava.io.FileInputStream; 5. importjava.io.FileNotFoundException; 6. importjava.io.FileOutputStream; 7. importjava.io.IOException; 8. importjava.util.Calendar; 9. importjava.util.Date; 10. importjava.util.Iterator; 11.12. mons.lang.ArrayUtils; 13. mons.lang.CharSet; 14. mons.lang.CharSetUtils; 15. mons.lang.ClassUtils; 16. mons.lang.ObjectUtils; 17. mons.lang.RandomStringUtils; 18. mons.lang.SerializationUtils; 19. mons.lang.StringEscapeUtils; 20. mons.lang.StringUtils; 21. mons.lang.SystemUtils; 22. mons.lang.builder.EqualsBuilder; 23. mons.lang.builder.HashCodeBuilder; 24. mons.lang.builder.ToStringBuilder; 25. mons.lang.builder.ToStringStyle; 26. mons.lang.math.NumberUtils; 27. mons.lang.time.DateFormatUtils; 28. mons.lang.time.DateUtils; 29. mons.lang.time.StopWatch; 30.31. publicclassLangDemo 32. publicvoidcharSetDemo() 33. System.out.println(*CharSetDemo*); 34. CharSetcharSet=CharSet.getInstance(aeiou); 35. StringdemoStr=Thequickbrownfoxjumpsoverthelazydog.; 36. intcount=0; 37. for(inti=0,len=demoStr.length();ilen;i+) 38. if(charSet.contains(demoStr.charAt(i) 39. count+; 40. 41. 42. System.out.println(count:+count); 43. 44.45. publicvoidcharSetUtilsDemo() 46. System.out.println(*CharSetUtilsDemo*); 47. System.out.println(计算字符串中包含某字符数.); 48. System.out.println(CharSetUtils.count( 49. Thequickbrownfoxjumpsoverthelazydog.,aeiou); 50.51. System.out.println(删除字符串中某字符.); 52. System.out.println(CharSetUtils.delete( 53. Thequickbrownfoxjumpsoverthelazydog.,aeiou); 54.55. System.out.println(保留字符串中某字符.); 56. System.out.println(CharSetUtils.keep( 57. Thequickbrownfoxjumpsoverthelazydog.,aeiou); 58.59. System.out.println(合并重复的字符.); 60. System.out.println(CharSetUtils.squeeze(abbbbbbcdd,bd); 61. 62.63. publicvoidobjectUtilsDemo() 64. System.out.println(*ObjectUtilsDemo*); 65. System.out.println(Object为null时,默认打印某字符.); 66. Objectobj=null; 67. System.out.println(ObjectUtils.defaultIfNull(obj,空); 68.69. System.out.println(验证两个引用是否指向的Object是否相等,取决于Object的equals()方法.); 70. Objecta=newObject(); 71. Objectb=a; 72. Objectc=newObject(); 73. System.out.println(ObjectUtils.equals(a,b); 74. System.out.println(ObjectUtils.equals(a,c); 75.76. System.out.println(用父类Object的toString()方法返回对象信息.); 77. Datedate=newDate(); 78. System.out.println(ObjectUtils.identityToString(date); 79. System.out.println(date); 80.81. System.out.println(返回类本身的toString()方法结果,对象为null时,返回0长度字符串.); 82. System.out.println(ObjectUtils.toString(date); 83. System.out.println(ObjectUtils.toString(null); 84. System.out.println(date); 85. 86.87. publicvoidserializationUtilsDemo() 88. System.out.println(*SerializationUtils*); 89. Datedate=newDate(); 90. bytebytes=SerializationUtils.serialize(date); 91. System.out.println(ArrayUtils.toString(bytes); 92. System.out.println(date); 93.94. DatereDate=(Date)SerializationUtils.deserialize(bytes); 95. System.out.println(reDate); 96. System.out.println(ObjectUtils.equals(date,reDate); 97. System.out.println(date=reDate); 98.99. FileOutputStreamfos=null; 100. FileInputStreamfis=null; 101. try 102. fos=newFileOutputStream(newFile(d:/test.txt); 103. fis=newFileInputStream(newFile(d:/test.txt); 104. SerializationUtils.serialize(date,fos); 105. DatereDate2=(Date)SerializationUtils.deserialize(fis); 106.107. System.out.println(date.equals(reDate2); 108.109. catch(FileNotFoundExceptione) 110. e.printStackTrace(); 111. finally 112. try 113. fos.close(); 114. fis.close(); 115. catch(IOExceptione) 116. e.printStackTrace(); 117. 118. 119.120. 121.122. publicvoidrandomStringUtilsDemo() 123. System.out.println(*RandomStringUtilsDemo*); 124. System.out.println(生成指定长度的随机字符串,好像没什么用.); 125. System.out.println(RandomStringUtils.random(500); 126.127. System.out.println(在指定字符串中生成长度为n的随机字符串.); 128. System.out.println(RandomStringUtils.random(5,abcdefghijk); 129.130. System.out.println(指定从字符或数字中生成随机字符串.); 131. System.out.println(RandomStringUtils.random(5,true,false); 132. System.out.println(RandomStringUtils.random(5,false,true); 133.134. 135.136. publicvoidstringUtilsDemo() 137. System.out.println(*StringUtilsDemo*); 138. System.out.println(将字符串重复n次,将文字按某宽度居中,将字符串数组用某字符串连接.); 139. Stringheader=newString3; 140. header0=StringUtils.repeat(*,50); 141. header1=StringUtils.center(StringUtilsDemo,50,O); 142. header2=header0; 143. Stringhead=StringUtils.join(header,n); 144. System.out.println(head); 145.146. System.out.println(缩短到某长度,用.结尾.); 147. System.out.println(StringUtils.abbreviate( 148. Thequickbrownfoxjumpsoverthelazydog.,10); 149. System.out.println(StringUtils.abbreviate( 150. Thequickbrownfoxjumpsoverthelazydog.,15,10); 151.152. System.out.println(返回两字符串不同处索引号.); 153. System.out.println(StringUtils.indexOfDifference(aaabc,aaacc); 154.155. System.out.println(返回两字符串不同处开始至结束.); 156. System.out.println(StringUtils.difference(aaabcde,aaaccde); 157.158. System.out.println(截去字符串为以指定字符串结尾的部分.); 159. System.out.println(StringUtils.chomp(aaabcde,de); 160.161. System.out.println(检查一字符串是否为另一字符串的子集.); 162. System.out.println(StringUtils.containsOnly(aad,aadd); 163.164. System.out.println(检查一字符串是否不是另一字符串的子集.); 165. System.out.println(StringUtils.containsNone(defg,aadd); 166.167. System.out.println(检查一字符串是否包含另一字符串.); 168. System.out.println(StringUtils.contains(defg,ef); 169. System.out.println(StringUtils.containsOnly(ef,defg); 170.171. System.out.println(返回可以处理null的toString().); 172. System.out.println(StringUtils.defaultString(aaaa); 173. System.out.println(?+StringUtils.defaultString(null)+!); 174.175. System.out.println(去除字符中的空格.); 176. System.out.println(StringUtils.deleteWhitespace(aabbcc); 177.178. System.out.println(判断是否是某类字符.); 179. System.out.println(StringUtils.isAlpha(ab); 180. System.out.println(StringUtils.isAlphanumeric(12); 181. System.out.println(StringUtils.isBlank(); 182. System.out.println(StringUtils.isNumeric(123); 183. 184.185. publicvoidsystemUtilsDemo() 186. System.out.println(genHeader(SystemUtilsDemo); 187. System.out.println(获得系统文件分隔符.); 188. System.out.println(SystemUtils.FILE_SEPARATOR); 189.190. System.out.println(获得源文件编码.); 191. System.out.println(SystemUtils.FILE_ENCODING); 192.193. System.out.println(获得ext目录.); 194. System.out.println(SystemUtils.JAVA_EXT_DIRS); 195.196. System.out.println(获得java版本.); 197. System.out.println(SystemUtils.JAVA_VM_VERSION); 198.199. System.out.println(获得java厂商.); 200. System.out.println(SystemUtils.JAVA_VENDOR); 201. 202.203. publicvoidclassUtilsDemo() 204. System.out.println(genHeader(ClassUtilsDemo); 205. System.out.println(获取类实现的所有接口.); 206. System.out.println(ClassUtils.getAllInterfaces(Date.class); 207.208. System.out.println(获取类所有父类.); 209. System.out.println(ClassUtils.getAllSuperclasses(Date.class); 210.211. System.out.println(获取简单类名.); 212. System.out.println(ClassUtils.getShortClassName(Date.class); 213.214. System.out.println(获取包名.); 215. System.out.println(ClassUtils.getPackageName(Date.class); 216.217. System.out.println(判断是否可以转型.); 218. System.out.println(ClassUtils.isAssignable(Date.class,Object.class); 219. System.out.println(ClassUtils.isAssignable(Object.class,Date.class); 220. 221.222. publicvoidstringEscapeUtilsDemo() 223. System.out.println(genHeader(StringEcsapeUtils); 224. System.out.println(转换特殊字符.); 225. System.out.println(html:+StringEscapeUtils.escapeHtml(); 226. System.out.println(html:+StringEscapeUtils.unescapeHtml(); 227. 228.229. privatefinalclassBuildDemo 230. Stringname; 231.232. intage; 233.234. publicBuildDemo(Stringname,intage) 235. =name; 236. this.age=age; 237. 238.239. publicStringtoString() 240. ToStringBuildertsb=newToStringBuilder(this, 241. ToStringStyle.MULTI_LINE_STYLE); 242. tsb.append(Name,name); 243. tsb.append(Age,age); 244. returntsb.toString(); 245. 246.247. publicinthashCode() 248. HashCodeBuilderhcb=newHashCodeBuilder(); 249. hcb.append(name); 250. hcb.append(age); 251. returnhcb.hashCode(); 252. 253.254. publicbooleanequals(Objectobj) 255. if(!(objinstanceofBuildDemo) 256. returnfalse; 257. 258. BuildDemobd=(BuildDemo)obj; 259. EqualsBuildereb=newEqualsBuilder(); 260. eb.append(name,); 261. eb.append(age,bd.age); 262. returneb.isEquals(); 263. 264. 265.266. publicvoidbuilderDemo() 267. System.out.println(genHeader(BuilderDemo); 268. BuildDemoobj1=newBuildDemo(a,1); 269. BuildDemoobj2=newBuildDemo(b,2); 270. BuildDemoobj3=newBuildDemo(a,1); 271.272. System.out.println(toString(); 273. System.out.println(obj1); 274. System.out.println(obj2); 275. System.out.println(obj3); 276.277. System.out.println(hashCode(); 278. System.out.println(obj1.hashCode(); 279. System.out.println(obj2.hashCode(); 280. System.out.println(obj3.hashCode(); 281.282. System.out.println(equals(); 283. System.out.println(obj1.equals(obj2); 284. System.out.println(obj1.equals(obj3); 285. 286.287. publicvoidnumberUtils() 288. System.out.println(genHeader(NumberUtils); 289. System.out.println(字符串转为数字(不知道有什么用).); 290. System.out.println(NumberUtils.stringToInt(ba,33); 291.292. System.out.println(从数组中选出最大值.); 293. System.out.println(NumberUtils.max(newint1,2,3,4); 294.295. System.out.println(判断字符串是否全是整数.); 296. System.out.println(NumberUtils.isDigits(123.1); 297.298. System.out.println(判断字符串是否是有效数字.); 299. System.out.println(NumberUtils.isNumber(0123.1); 300. 301.302. publicvoiddateFormatUtilsDemo() 303. System.out.println(genHeader(DateFormatUtilsDemo); 304. System.out.println(格式化日期输出.); 305. System.out.println(DateFormatUtils.format(System.currentTimeMillis(), 306. yyyy-MM-ddHH:mm:ss); 307.308. System.out.println(秒表.); 309. StopWatchsw=newStopWatc
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业并购交易合同修订及调整协议
- 离婚双方无子女财产分配及子女监护权约定
- 安全员证续期网络考及答案
- 2025年大学瑜伽课程题库及答案
- 基于自适应学习系统的计算机课程教学研究
- 2025年新能源产业质量追溯体系与产业链协同创新报告
- 展示台 饲养达人评选会说课稿-2025-2026学年小学劳动粤教版劳动与技术四年级-粤教版(劳动与技术)
- 城区建筑竞赛方案设计案例
- 建环专业考试题及答案
- 2025年数学几何竞赛真题及答案
- 医学信息检索与利用智慧树知到答案章节测试2023年杭州医学院
- 黑底搭配大气企业宣传商业计划书商务通用PPT模板
- GB/T 17608-2006煤炭产品品种和等级划分
- GB/T 16866-2006铜及铜合金无缝管材外形尺寸及允许偏差
- 量子信息与量子计算课件
- 沪教五年级数学上册第一单元测试卷
- 地下停车库设计统一规定
- 综合实践课《绳结》教学设计
- 建筑装饰设计收费管理规定
- 电子课件-《市场营销》-A45-2298完整版教学课件全书电子讲义(最新)
- (整理)ASME-B161.34规定的标准磅级阀门(常用材料)额定工作压力和试验压力
评论
0/150
提交评论