-
2004年12月30日
如何使用org.apache.commons.lang(学习笔记)
CharRange
You use the CharRange class to create and maintain a range of characters. The class provides functionality to determine if a character lies in a certain range as well as to determine if one character range lies within another character range.CharSet
The CharSet class simplifies creating and using a set of characters. The CharSet can be created from the characters in a string and is especially useful when you want to check if a certain string holds only valid characters. You could create a CharSet and then check each character in the string against the character set.CharSetUtils
CharSetUtils provides static methods to handle character-related functionality that is often required during developmentNote For CharSet, case does matter. As a result, the character b and the character B are not the same.
System.out.println("B,o,k,e and r count = " +
CharSetUtils.count("BorisBecker", new String[] { "Bo", "ker" })); //8
System.out.println("Delete B,o,k,e and r = " +
CharSetUtils.delete("BorisBecker", new String[] { "Bo", "ker" })); //isc
//Keeps only the characters specified
System.out.println("Keep B and o = " +
CharSetUtils.keep("BorisBecker", "Bo")); //BoB
//Removes specified character repetitions
System.out.println("Squeeze B and o = " +
CharSetUtils.squeeze("BBoooorisbbbecker", "Bo")); //BorisbbbeckerObjectUtils
The ObjectUtils class is a simple one that provides a couple of useful features. The toString method that has been introduced in Lang version 2 can be useful to avoid one of the most deadly problems facing Java developers, the NullPointerException. You can use the toString method to return an empty string or a specified string if the String instance provided is null. You could now replace all ternary operator usages in the form strInstance= (strInstance == null ? "": strInstance); with strInstance = ObjectUtils.toString(strInstance);.SerializationUtils
Working with Java Input/Output (I/O) has never been my favorite. Remembering numerous classes, their hierarchies, and when to use what can be quite a pain. You can use the SerializationUtils class to take up the task of serializing and deserializing objects, so you do not have to worry about object input and output streams.The SerializationUtils class also provides methods that can use a byte array to serialize and deserialize. A clone method is also provided that serializes an object to a byte array and then deserializes it back to achieve cloning of the object.
RandomStringUtils
RandomStringUtils is one class that you can have a lot of fun using. The class generates random strings based on various parameters specified. One great option is that you can use it to hold a daily lottery at your desk. The point, of course, is that the source code can be rigged to make you win! A commonly required business application for the RandomStringUtils class is to generate passwords.RandomStringUtils是一个设计用来随机生成文本位的类,这在密码的生成上很常见。其全部范围是unicode,并且支持英文字母、Ascii码、AsciiNumeric和数字的特别重载。
StringUtils
StringUtils is the class in the Lang component that provides the most number of static methods to use. StringUtils has methods that can do almost everything you could want to do with a string. The class provides more than 100 methods; you will learn about some of the most useful ones.The StringUtils class even has some methods such as equals, trim, and substring that do what java.lang.String methods do. The StringUtils methods, however, provide built-in handling for null strings. A string being null is one of the most common causes of NullPointerException being thrown, so using the StringUtils methods is a better option in cases where you might get a null string. The Javadocs for the StringUtils class list all the methods that are null safe. Another useful addition to the Javadocs in Lang 2 is that now every method in StringUtils has some usage examples.
NULL 这个空值常常会导致程序的 NullPointerException, 在撰写接收使用者 request 程序的时候会特別麻烦, 需要多输入 null 的检查, 不如使用 EMPTY 空字串来得方便, 所以我们可以将一些 StringUtils.defaultString(request.getParameter("xxx")) 来确保不是 NULL.
capitalise(String):一个String大写化函数(这里保留了英式拼法),它不像大多数字符串库那样使用toUpperCase,而是恰当地使用了toTitleCase。
join(Object[], String):它把对象数组里每个对象的toString合并成带有指定定界符的单个String。所以join( {"A","B",C"}, ";")的结果是"A;B;C"。Iterator也可以被连接。
split(String, String):它能分割带有定界符的文本。其功能没有StringTokenizer(它不能一次处理多组定界符)强大,但是能够很快、很容易地应用于很多场合。它是上一个合并方法的反向操作:split("A;B;C",";") => {"A", "B", "C"}。这个分割方法现在已经可以在 JDK 1.4的java.lang.String里找到了。
reverseDelimitedString(String, String):这是个很有趣的方法。它能以定界符为基础颠倒一段文本。所以:reverseDelimitedString("org.apache.commons")的结果就变成了"commons.apache.org."
replace(String, String, String):这个String替换方法常常是很让人期待的。JDK1.4能够使用正规表达式解决这个问题,但是StringUtils类所提供的基于String更简单的版本更常用。
SystemUtils
增加了一些 java 系统上面的处理, 例如检查 java 的版本 isJavaVersionAtLeast , 执行的作业系统等等..
SystemUtils is a handy little class that reads the system properties and returns appropriate messages based on the property values. You can use this class to find more information about the host machine and operating system. The most common usage, however, is to check the version of the JSDK being used. So, if you are using the logging introduced in JSDK 1.4, you could first check if the version being used is 1.4 or higher, and only if it is do you use the JSDK 1.4 features.The class provides only three public static methods, one of which is deprecated. Dozens of useful public static variables handle specific cases.
ClassUtils
The ClassUtils class is a new class introduced with version 2. This class provides static methods that can fetch you more information about a class or an object without having to directly use the Reflection Application Programming Interface (API).However, think twice before using some of the methods; if your design is proper, you ideally should not have to put such checks in your code.
StringEscapeUtils
Having to handle Hypertext Markup Language (HTML), JavaScript, Structured Query Language (SQL), and Java simultaneously is very much a part of a Java developer's life. However, generating output in a different format or handling input in a different format can be quite a task.For example, say you want to display <p>MyName<p> on a Web page. If you just send this as part of the HTML, what would appear on the screen is a new paragraph with the word MyName in it. The <p> tags used would not be displayed. What you need to send in the HTML would be this line of code:
<p>MyName<p>
This would then result in you getting the expected display <p>MyName<p>. TheStringEscapeUtils class makes implementing these cases quite simple because it provides ways of converting characters to achieve the expected output.
-
2004年12月30日
喻思成对中国开发者的建议
喻思成是bea中国公司技术总监,他建议中国开发者不要太沉迷于技术的细节,对技术的概念,对系统架构要掌握清楚。有一个很宽广的知识面,对当前的技术潮流保持敏感性,建立一个架构师级的视野,这些知识对一个开发者未来的发展很重要。
我觉得这些话都很正确,真正做起来却不容易。J2EE的世界太庞大了,要深入进去就不容易,还要跟踪的话就更困难。不过应该按这个路线努力。btw,我觉得dev2dev days实际上的内容不多,半天就可以。明年不知道还有没有兴趣去。
-
2004年12月29日
寻找失落的mysql密码
方法一:
mysql -Skip-grant
use mysql
update user set password=password("") where user="root";
flush privileges;
方法二:
在方法一不奏效的情况下,使用方法二。原来的mysql用户信息会丢失。
因为MySQL密码存储于数据库mysql中的user表中
所以只需要将别的MySQL中的user表拷贝过来覆盖掉就行了
在c:\mysql\data\mysql\(linux 则一般在/var/lib/mysql/mysql/)目录下有三个user表相关文件user.frm、user.MYD、user.MYI
user.frm //user表样式文件
user.MYD //user表数据文件
user.MYI //user表索引文件
为保险起见,三个都拷贝过来,不过其实如果之前在要恢复的那个MySQL上没有更改过表结构的话,只要拷贝user.MYD就行了
然后#. /etc/rc.d/init.d/mysql stop
#. /etc/rc.d/init.d/mysql start
#mysql -u root -p XXXXXX
好了,可以用别的mysql密码登陆了 -
2004年12月28日
处理mp3 id3 tag的 模块
eyed3 http://eyed3.nicfit.net/
id3-py http://id3-py.sourceforge.net/
pyid3lib http://pyid3lib.sourceforge.net/
pymedia -
2004年12月17日
maxthon:个人社会保险快速搜索
http://www.szsi.gov.cn是深圳市社会保险基金管理中心的网站,要查个人帐户的社会保险基金(养老专户余额、医疗专户余额、住房专户余额)的话,可以去http://wssb1.szsi.gov.cn/NetApplyWeb/personacctoutInput.jsp查。但这种方式还不够方便。通过分析personacctoutInput.jsp的源码,得到了maxthon里面可以用的快捷搜索的方法。将http://wssb1.szsi.gov.cn/NetApplyWeb/personacctoutResult.jsp?bacode=%s加到你的maxthon搜索里面去,取个缩写,如bx,以后就可以用bx 个人电脑号的方式随时查询你的社会保险基金了。







