﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-JavaWorm</title><link>http://www.blogjava.net/tanghc/</link><description>Welcome!</description><language>zh-cn</language><lastBuildDate>Sat, 18 Apr 2026 19:59:20 GMT</lastBuildDate><pubDate>Sat, 18 Apr 2026 19:59:20 GMT</pubDate><ttl>60</ttl><item><title>java文件操作大全</title><link>http://www.blogjava.net/tanghc/archive/2006/03/18/35943.html</link><dc:creator>JavaWorm</dc:creator><author>JavaWorm</author><pubDate>Sat, 18 Mar 2006 07:19:00 GMT</pubDate><guid>http://www.blogjava.net/tanghc/archive/2006/03/18/35943.html</guid><wfw:comment>http://www.blogjava.net/tanghc/comments/35943.html</wfw:comment><comments>http://www.blogjava.net/tanghc/archive/2006/03/18/35943.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tanghc/comments/commentRss/35943.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tanghc/services/trackbacks/35943.html</trackback:ping><description><![CDATA[
		<span id="ArticleContent1_ArticleContent1_lblContent">
				<font color="#a52a2a" size="4">java文件操作大全<br /><br />文件的建立/检查与删除<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;文件的建立、检查与删除&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath("");<br />//out.println(path);<br />File f=new File(path,"File.txt");<br />//out.println(f);<br />//out.println(f.exists());<br /><br />if(f.exists()){//检查File.txt是否存在<br />f.delete();//删除File.txt文件<br />out.println(path + "\\File.txt 存在，已删除。");<br />}else{<br />f.createNewFile();//在当前目录下建立一个名为File.txt的文件<br />out.println(path + "\\File.txt 不存在，已建立。");//输出目前所在的目录路径<br />}<br />%&gt;<br /><br />目录的建立/检查与删除<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;目录的建立/检查与删除&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath("");<br />path=path + "\\Sub";//将要建立的目录路径<br />File d=new File(path);//建立代表Sub目录的File对象，并得到它的一个引用<br />if(d.exists()){//检查Sub目录是否存在<br />d.delete();<br />out.println("Sub目录存在，已删除");<br />}else{<br />d.mkdir();//建立Sub目录<br />out.println("Sub目录不存在，已建立");<br />}<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br /><br />如何在JSP中处理虚拟目录<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;JSP中如何处理虚拟目录&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />取得虚拟目录对应的磁盘路径&lt;br&gt;<br />Web站点主目录的位置为&lt;font color=#ff0000&gt;&lt;%=request.getRealPath("/")%&gt;&lt;/font&gt;&lt;br&gt;<br />JSP网页所在的目录位置&lt;font color=#ff0000&gt;&lt;%=request.getRealPath("./")%&gt;&lt;/font&gt;&lt;br&gt;<br />JSP网页所在目录上一层目录的位置&lt;font color=#ff0000&gt;&lt;%=request.getRealPath("../")%&gt;&lt;/font&gt;&lt;br&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br /><br />文件属性的取得<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.util.Date,java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;文件属性的取得&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath("/");<br />File f=new File(path,"ReadData.txt");<br />if(f.exists()){<br />%&gt;<br />&lt;%=f.getName()%&gt;的属性如下：&lt;br&gt;&lt;br&gt;<br />文件长度为：&lt;%=f.length()%&gt;<br />&lt;%=f.isFile()?"是文件":"不是文件"%&gt;&lt;br&gt;<br />&lt;%=f.isDirectory()?"是目录":"不是目录"%&gt;&lt;br&gt;<br />&lt;%=f.canRead()?"可读取":"不可读取"%&gt;&lt;br&gt;<br />&lt;%=f.canWrite()?"可写入":"不可写入"%&gt;&lt;br&gt;<br />&lt;%=f.isHidden()?"是隐藏文件":"不是隐藏文件"%&gt;&lt;br&gt;<br />文件的最后修改日期为：&lt;%=new Date(f.lastModified())%&gt;&lt;br&gt;<br />&lt;%<br />}else{<br />f.createNewFile();//在当前目录下建立一个名为ReaData.txt的文件<br />%&gt; <br />&lt;%=f.getName()%&gt;的属性如下：&lt;br&gt;&lt;br&gt;<br />文件长度为：&lt;%=f.length()%&gt;<br />&lt;%=f.isFile()?"是文件":"不是文件"%&gt;&lt;br&gt;<br />&lt;%=f.isDirectory()?"是目录":"不是目录"%&gt;&lt;br&gt;<br />&lt;%=f.canRead()?"可读取":"不可读取"%&gt;&lt;br&gt;<br />&lt;%=f.canWrite()?"可写入":"不可写入"%&gt;&lt;br&gt;<br />&lt;%=f.isHidden()?"是隐藏文件":"不是隐藏文件"%&gt;&lt;br&gt;<br />文件的最后修改日期为：&lt;%=new Date(f.lastModified())%&gt;&lt;br&gt;<br />&lt;%<br />}<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br /><br />取出目录中文件的方法<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;取出目录中文件的方法--列出目录中的文件&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath("/");<br />File d=new File(path);//建立当前目录中文件的File对象<br />File list[]=d.listFiles();//取得代表目录中所有文件的File对象数组<br />out.println("&lt;font color=#ff0000&gt;" + path + "目录下的文件：&lt;/font&gt;&lt;br&gt;");<br />for(int i=0;i&lt;list.length;i++){<br />if(list&lt;I&gt;.isFile()){<br />out.println(list&lt;I&gt;.getName() + "&lt;br&gt;");<br />}<br />}<br />out.println("&lt;br&gt;&lt;font color=#ff0000&gt;" + path + "目录下的目录：&lt;/font&gt;&lt;br&gt;");<br />for(int i=0;i&lt;list.length;i++){<br />if(list&lt;I&gt;.isDirectory()){<br />out.println(list&lt;I&gt;.getName() + "&lt;br&gt;");<br />}<br />}<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br /><br />判断是否为空白文件<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;判断是否为空白文件&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath("/");<br />out.println(path);<br />FileReader fr=new FileReader(path + "\\AtEnd.txt");//建立FileReader对象，并实例化为fr<br />//对FileReader类生成的对象使用read()方法，可以从字符流中读取下一个字符。<br />if(fr.read()==-1)//判断是否已读到文件的结尾<br />{<br />out.print("AtEnd.txt文件中没有数据&lt;br&gt;");<br />}else{<br />out.println("AtEnd.txt文件中有数据");<br />}<br />fr.close();<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br /><br />读取所有的文件数据<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*,java.lang.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;读取所有的文件数据&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath(".");<br />FileReader fr=new FileReader(path + "\\ReadData.txt");<br />//关键在于读取过程中，要判断所读取的字符是否已经到了文件的末尾，并且这个字符是不是文件中的断行符，即判断该字符值是否为13。<br />int c=fr.read();//从文件中读取一个字符<br />//判断是否已读到文件结尾<br />while(c!=-1){<br />out.print((char)c);//输出读到的数据<br />c=fr.read();//从文件中继续读取数据<br />if(c==13){//判断是否为断行字符<br />out.print("&lt;br&gt;");//输出分行标签<br />fr.skip(1);//略过一个字符<br />//c=fr.read();//读取一个字符<br />}<br />}<br />fr.close();<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br /><br />一行一行读取数据<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;文件读取&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath("");//取得当前目录的路径<br />FileReader fr=new FileReader(path + "\\file\\inc\\t.txt");//建立FileReader对象，并实例化为fr<br />BufferedReader br=new BufferedReader(fr);//建立BufferedReader对象，并实例化为br<br />String Line=br.readLine();//从文件读取一行字符串<br />//判断读取到的字符串是否不为空<br />while(Line!=null){<br />out.println(Line + "&lt;br&gt;");//输出从文件中读取的数据<br />Line=br.readLine();//从文件中继续读取一行数据<br />}<br />br.close();//关闭BufferedReader对象<br />fr.close();//关闭文件<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br /><br />略过文件中的字符不读取<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;略过字节不读取&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath(".");<br />FileReader fr=new FileReader(path + "\\ReadData.txt");<br />fr.skip(2);//跳过2个字节<br />int c=fr.read();//读取一个字节<br />while(c!=-1){<br />out.print((char)c);<br />c=fr.read();<br />}<br />fr.close();<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br /><br />将数据写入文件<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;将数据写入文件&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath(".");<br />FileWriter fw=new FileWriter(path + "\\WriteData.txt");//建立FileWriter对象，并实例化fw<br />//将字符串写入文件<br />fw.write("大家好！");<br />fw.write("本书是《JSP编程技巧》");<br />fw.write("请多多指教！");<br />fw.write("email:stride@sina.com");<br />fw.close();<br /><br />FileReader fr=new FileReader(path + "\\WriteData.txt");<br />BufferedReader br=new BufferedReader(fr);//建立BufferedReader对象，并实例化为br<br />String Line=br.readLine();<br />//读取一行数据<br />out.println(Line + "&lt;br&gt;");<br />br.close();//关闭BufferedReader对象<br />fr.close();<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br /><br />将写入文件的数据分行<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;将写入文件的数据分行&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath(".");<br />FileWriter fw=new FileWriter(path + "\\WriteData.txt");<br />BufferedWriter bw=new BufferedWriter(fw);<br />bw.write("大家好！");<br />bw.write("本书是《JSP编程技巧》。");<br />bw.newLine();//断行<br />bw.write("请多多指教！");<br />bw.newLine();//断行<br />bw.write("email: stride@sina.com");<br />bw.flush();//将数据更新至文件<br />fw.close();//关闭文件流<br />out.println("写入文件内容为：&lt;br&gt;");<br />FileReader fr=new FileReader(path + "\\WriteData.txt");<br />BufferedReader br=new BufferedReader(fr);<br />String Line=br.readLine();//读取一行数据<br />while(Line!=null){<br />out.println(Line + "&lt;br&gt;");<br />Line=br.readLine();<br />}<br />fr.close();<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br />如何将数据追加写入到文件<br />&lt;%@ page contentType="text/html;charset=gb2312"%&gt;<br />&lt;%@ page import="java.io.*"%&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;将写入文件的数据分行&lt;/title&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />&lt;%<br />String path=request.getRealPath(".");<br />RandomAccessFile rf=new RandomAccessFile(path + "\\WriteData.txt","rw");//定义一个类RandomAccessFile的对象，并实例化<br />rf.seek(rf.length());//将指针移动到文件末尾<br />rf.writeBytes("\nAppend a line to the file!");<br />rf.close();//关闭文件流<br />out.println("写入文件内容为：&lt;br&gt;");<br />FileReader fr=new FileReader(path + "\\WriteData.txt");<br />BufferedReader br=new BufferedReader(fr);//读取文件的BufferedRead对象<br />String Line=br.readLine();<br />while(Line!=null){<br />out.println(Line + "&lt;br&gt;");<br />Line=br.readLine();<br />}<br />fr.close();//关闭文件<br />%&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;<br />转载：<a id="ArticleContent1_ArticleContent1_AuthorBlogLink" href="http://blog.csdn.net/chensheng913/" target="_blank">http://blog.csdn.net/chensheng913/</a></font>
		</span>
<img src ="http://www.blogjava.net/tanghc/aggbug/35943.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tanghc/" target="_blank">JavaWorm</a> 2006-03-18 15:19 <a href="http://www.blogjava.net/tanghc/archive/2006/03/18/35943.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK5.0的11个主要新特征</title><link>http://www.blogjava.net/tanghc/archive/2006/03/18/35939.html</link><dc:creator>JavaWorm</dc:creator><author>JavaWorm</author><pubDate>Sat, 18 Mar 2006 06:50:00 GMT</pubDate><guid>http://www.blogjava.net/tanghc/archive/2006/03/18/35939.html</guid><wfw:comment>http://www.blogjava.net/tanghc/comments/35939.html</wfw:comment><comments>http://www.blogjava.net/tanghc/archive/2006/03/18/35939.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tanghc/comments/commentRss/35939.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tanghc/services/trackbacks/35939.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 1           泛型(Generic)1.1          说明增强了java的类型安全，可以在编译期间对容器内的对象进行类型检查，在运行期不必进行类型的转换。而在j2se5之前必须在运行期动态进行容器内对象的检查及转换减少含糊的容器，可以定义什么类型的数据放入容器ArrayList&lt;Integer&gt; listOfIntegers; // &lt;TYPE_NAME&gt;...&nbsp;&nbsp;<a href='http://www.blogjava.net/tanghc/archive/2006/03/18/35939.html'>阅读全文</a><img src ="http://www.blogjava.net/tanghc/aggbug/35939.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tanghc/" target="_blank">JavaWorm</a> 2006-03-18 14:50 <a href="http://www.blogjava.net/tanghc/archive/2006/03/18/35939.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java正则表达式详解 </title><link>http://www.blogjava.net/tanghc/archive/2006/03/18/35935.html</link><dc:creator>JavaWorm</dc:creator><author>JavaWorm</author><pubDate>Sat, 18 Mar 2006 06:32:00 GMT</pubDate><guid>http://www.blogjava.net/tanghc/archive/2006/03/18/35935.html</guid><wfw:comment>http://www.blogjava.net/tanghc/comments/35935.html</wfw:comment><comments>http://www.blogjava.net/tanghc/archive/2006/03/18/35935.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/tanghc/comments/commentRss/35935.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tanghc/services/trackbacks/35935.html</trackback:ping><description><![CDATA[
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">如果你曾经用过Perl或任何其他内建正则表达式支持的语言，你一定知道用正则表达式处理文本和匹配模式是多么简单。如果你不熟悉这个术语，那么“正则表达式”（Regular Expression）就是一个字符构成的串，它定义了一个用来搜索匹配字符串的模式。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">许多语言，包括Perl、PHP、Python、JavaScript和JScript，都支持用正则表达式处理文本，一些文本编辑器用正则表达式实现高级“搜索-替换”功能。那么Java又怎样呢？本文写作时，一个包含了用正则表达式进行文本处理的Java规范需求（Specification Request）已经得到认可，你可以期待在JDK的下一版本中看到它。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">然而，如果现在就需要使用正则表达式，又该怎么办呢？你可以从Apache.org下载源代码开放的Jakarta-ORO库。本文接下来的内容先简要地介绍正则表达式的入门知识，然后以Jakarta-ORO API为例介绍如何使用正则表达式。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>
												<font size="4">一、正则表达式基础知识</font>
										</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">我们先从简单的开始。假设你要搜索一个包含字符“cat”的字符串，搜索用的正则表达式就是“cat”。如果搜索对大小写不敏感，单词“catalog”、“Catherine”、“sophisticated”都可以匹配。也就是说： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_a.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>1.1 句点符号</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">假设你在玩英文拼字游戏，想要找出三个字母的单词，而且这些单词必须以“t”字母开头，以“n”字母结束。另外，假设有一本英文字典，你可以用正则表达式搜索它的全部内容。要构造出这个正则表达式，你可以使用一个通配符——句点符号“.”。这样，完整的表达式就是“t.n”，它匹配“tan”、 “ten”、“tin”和“ton”，还匹配“t#n”、“tpn”甚至“t n”，还有其他许多无意义的组合。这是因为句点符号匹配所有字符，包括空格、Tab字符甚至换行符： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_b.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>1.2 方括号符号</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">为了解决句点符号匹配范围过于广泛这一问题，你可以在方括号（“[]”）里面指定看来有意义的字符。此时，只有方括号里面指定的字符才参与匹配。也就是说，正则表达式“t[aeio]n”只匹配“tan”、“Ten”、“tin”和“ton”。但“Toon”不匹配，因为在方括号之内你只能匹配单个字符： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_c.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>1.3 “或”符号</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">如果除了上面匹配的所有单词之外，你还想要匹配“toon”，那么，你可以使用“|”操作符。“|”操作符的基本意义就是“或”运算。要匹配 “toon”，使用“t(a|e|i|o|oo)n”正则表达式。这里不能使用方扩号，因为方括号只允许匹配单个字符；这里必须使用圆括号“()”。圆括号还可以用来分组，具体请参见后面介绍。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_d.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>1.4 表示匹配次数的符号</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">表一显示了表示匹配次数的符号，这些符号用来确定紧靠该符号左边的符号出现的次数： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4n.jpg" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">假设我们要在文本文件中搜索美国的社会安全号码。这个号码的格式是999-99-9999。用来匹配它的正则表达式如图一所示。在正则表达式中，连字符（“-”）有着特殊的意义，它表示一个范围，比如从0到9。因此，匹配社会安全号码中的连字符号时，它的前面要加上一个转义字符“\”。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4a.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图一：匹配所有123-12-1234形式的社会安全号码</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">假设进行搜索的时候，你希望连字符号可以出现，也可以不出现——即，999-99-9999和999999999都属于正确的格式。这时，你可以在连字符号后面加上“？”数量限定符号，如图二所示： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4b.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图二：匹配所有123-12-1234和123121234形式的社会安全号码</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">下面我们再来看另外一个例子。美国汽车牌照的一种格式是四个数字加上二个字母。它的正则表达式前面是数字部分“[0-9]{4}”，再加上字母部分“[A-Z]{2}”。图三显示了完整的正则表达式。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4c.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图三：匹配典型的美国汽车牌照号码，如8836KV</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">1.5 “否”符号 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">“^”符号称为“否”符号。如果用在方括号内，“^”表示不想要匹配的字符。例如，图四的正则表达式匹配所有单词，但以“X”字母开头的单词除外。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4d.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图四：匹配所有单词，但“X”开头的除外</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">1.6 圆括号和空白符号 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">假设要从格式为“June 26, 1951”的生日日期中提取出月份部分，用来匹配该日期的正则表达式可以如图五所示： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4e.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图五：匹配所有Moth DD,YYYY格式的日期</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">新出现的“\s”符号是空白符号，匹配所有的空白字符，包括Tab字符。如果字符串正确匹配，接下来如何提取出月份部分呢？只需在月份周围加上一个圆括号创建一个组，然后用ORO API（本文后面详细讨论）提取出它的值。修改后的正则表达式如图六所示： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4f.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图六：匹配所有Month DD,YYYY格式的日期，定义月份值为第一个组</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>1.7 其它符号</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">为简便起见，你可以使用一些为常见正则表达式创建的快捷符号。如表二所示： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">表二：常用符号 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4o.jpg" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">例如，在前面社会安全号码的例子中，所有出现“[0-9]”的地方我们都可以使用“\d”。修改后的正则表达式如图七所示： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4g.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图七：匹配所有123-12-1234格式的社会安全号码</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>
												<font size="4">二、Jakarta-ORO库</font>
										</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">有许多源代码开放的正则表达式库可供Java程序员使用，而且它们中的许多支持Perl 5兼容的正则表达式语法。我在这里选用的是Jakarta-ORO正则表达式库，它是最全面的正则表达式API之一，而且它与Perl 5正则表达式完全兼容。另外，它也是优化得最好的API之一。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">Jakarta-ORO库以前叫做OROMatcher，Daniel Savarese大方地把它赠送给了Jakarta Project。你可以按照本文最后参考资源的说明下载它。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">我首先将简要介绍使用Jakarta-ORO库时你必须创建和访问的对象，然后介绍如何使用Jakarta-ORO API。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>▲ PatternCompiler对象</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">首先，创建一个Perl5Compiler类的实例，并把它赋值给PatternCompiler接口对象。Perl5Compiler是PatternCompiler接口的一个实现，允许你把正则表达式编译成用来匹配的Pattern对象。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_e.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>▲ Pattern对象</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">要把正则表达式编译成Pattern对象，调用compiler对象的compile()方法，并在调用参数中指定正则表达式。例如，你可以按照下面这种方式编译正则表达式“t[aeio]n”： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_f.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">默认情况下，编译器创建一个大小写敏感的模式（pattern）。因此，上面代码编译得到的模式只匹配“tin”、“tan”、 “ten”和“ton”，但不匹配“Tin”和“taN”。要创建一个大小写不敏感的模式，你应该在调用编译器的时候指定一个额外的参数： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_g.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">创建好Pattern对象之后，你就可以通过PatternMatcher类用该Pattern对象进行模式匹配。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>▲ PatternMatcher对象</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">PatternMatcher对象根据Pattern对象和字符串进行匹配检查。你要实例化一个Perl5Matcher类并把结果赋值给 PatternMatcher接口。Perl5Matcher类是PatternMatcher接口的一个实现，它根据Perl 5正则表达式语法进行模式匹配： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_h.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">使用PatternMatcher对象，你可以用多个方法进行匹配操作，这些方法的第一个参数都是需要根据正则表达式进行匹配的字符串： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">· boolean matches(String input, Pattern pattern)：当输入字符串和正则表达式要精确匹配时使用。换句话说，正则表达式必须完整地描述输入字符串。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">· boolean matchesPrefix(String input, Pattern pattern)：当正则表达式匹配输入字符串起始部分时使用。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">· boolean contains(String input, Pattern pattern)：当正则表达式要匹配输入字符串的一部分时使用（即，它必须是一个子串）。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">另外，在上面三个方法调用中，你还可以用PatternMatcherInput对象作为参数替代String对象；这时，你可以从字符串中最后一次匹配的位置开始继续进行匹配。当字符串可能有多个子串匹配给定的正则表达式时，用PatternMatcherInput对象作为参数就很有用了。用 PatternMatcherInput对象作为参数替代String时，上述三个方法的语法如下： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">· boolean matches(PatternMatcherInput input, Pattern pattern) </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">· boolean matchesPrefix(PatternMatcherInput input, Pattern pattern) </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">· boolean contains(PatternMatcherInput input, Pattern pattern) </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>
												<font size="4">三、应用实例</font>
										</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">下面我们来看看Jakarta-ORO库的一些应用实例。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>3.1 日志文件处理</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">任务：分析一个Web服务器日志文件，确定每一个用户花在网站上的时间。在典型的BEA WebLogic日志文件中，日志记录的格式如下： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_i.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">分析这个日志记录，可以发现，要从这个日志文件提取的内容有两项：IP地址和页面访问时间。你可以用分组符号（圆括号）从日志记录提取出IP地址和时间标记。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">首先我们来看看IP地址。IP地址有4个字节构成，每一个字节的值在0到255之间，各个字节通过一个句点分隔。因此，IP地址中的每一个字节有至少一个、最多三个数字。图八显示了为IP地址编写的正则表达式： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4h.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图八：匹配IP地址</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">IP地址中的句点字符必须进行转义处理（前面加上“\”），因为IP地址中的句点具有它本来的含义，而不是采用正则表达式语法中的特殊含义。句点在正则表达式中的特殊含义本文前面已经介绍。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">日志记录的时间部分由一对方括号包围。你可以按照如下思路提取出方括号里面的所有内容：首先搜索起始方括号字符（“[”），提取出所有不超过结束方括号字符（“]”）的内容，向前寻找直至找到结束方括号字符。图九显示了这部分的正则表达式。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4i.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图九：匹配至少一个字符，直至找到“]”</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">现在，把上述两个正则表达式加上分组符号（圆括号）后合并成单个表达式，这样就可以从日志记录提取出IP地址和时间。注意，为了匹配“- -”（但不提取它），正则表达式中间加入了“\s-\s-\s”。完整的正则表达式如图十所示。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4j.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图十：匹配IP地址和时间标记</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">现在正则表达式已经编写完毕，接下来可以编写使用正则表达式库的Java代码了。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">为使用Jakarta-ORO库，首先创建正则表达式字符串和待分析的日志记录字符串： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_j.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">这里使用的正则表达式与图十的正则表达式差不多完全相同，但有一点例外：在Java中，你必须对每一个向前的斜杠（“\”）进行转义处理。图十不是 Java的表示形式，所以我们要在每个“\”前面加上一个“\”以免出现编译错误。遗憾的是，转义处理过程很容易出现错误，所以应该小心谨慎。你可以首先输入未经转义处理的正则表达式，然后从左到右依次把每一个“\”替换成“\\”。如果要复检，你可以试着把它输出到屏幕上。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">初始化字符串之后，实例化PatternCompiler对象，用PatternCompiler编译正则表达式创建一个Pattern对象： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_k.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">现在，创建PatternMatcher对象，调用PatternMatcher接口的contain()方法检查匹配情况： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_l.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">接下来，利用PatternMatcher接口返回的MatchResult对象，输出匹配的组。由于logEntry字符串包含匹配的内容，你可以看到类如下面的输出： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_m.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>3.2 HTML处理实例一</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">下面一个任务是分析HTML页面内FONT标记的所有属性。HTML页面内典型的FONT标记如下所示： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<font face="Arial, Serif" color="#ff0000" size="+2">
										</font>
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_n.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">程序将按照如下形式，输出每一个FONT标记的属性： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_o.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">在这种情况下，我建议你使用两个正则表达式。第一个如图十一所示，它从字体标记提取出“"face="Arial, Serif" size="+2" color="red"”。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4k.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图十一：匹配FONT标记的所有属性</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">第二个正则表达式如图十二所示，它把各个属性分割成名字-值对。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4l.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图十二：匹配单个属性，并把它分割成名字-值对</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">分割结果为： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_p.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">现在我们来看看完成这个任务的Java代码。首先创建两个正则表达式字符串，用Perl5Compiler把它们编译成Pattern对象。编译正则表达式的时候，指定Perl5Compiler.CASE_INSENSITIVE_MASK选项，使得匹配操作不区分大小写。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">接下来，创建一个执行匹配操作的Perl5Matcher对象。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_q.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">假设有一个String类型的变量html，它代表了HTML文件中的一行内容。如果html字符串包含FONT标记，匹配器将返回true。此时，你可以用匹配器对象返回的MatchResult对象获得第一个组，它包含了FONT的所有属性： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_r.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">接下来创建一个PatternMatcherInput对象。这个对象允许你从最后一次匹配的位置开始继续进行匹配操作，因此，它很适合于提取FONT标记内属性的名字-值对。创建PatternMatcherInput对象，以参数形式传入待匹配的字符串。然后，用匹配器实例提取出每一个FONT的属性。这通过指定PatternMatcherInput对象（而不是字符串对象）为参数，反复地调用PatternMatcher对象的contains ()方法完成。PatternMatcherInput对象之中的每一次迭代将把它内部的指针向前移动，下一次检测将从前一次匹配位置的后面开始。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">本例的输出结果如下： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_s.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<strong>3.3 HTML处理实例二</strong>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">下面我们来看看另一个处理HTML的例子。这一次，我们假定Web服务器从widgets.acme.com移到了newserver.acme.com。现在你要修改一些页面中的链接： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_t.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">执行这个搜索的正则表达式如图十三所示： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">
												<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4m.gif" border="0" />
										</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<p align="center">图十三：匹配修改前的链接</p>
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">如果能够匹配这个正则表达式，你可以用下面的内容替换图十三的链接： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_u.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">注意#字符的后面加上了$1。Perl正则表达式语法用$1、$2等表示已经匹配且提取出来的组。图十三的表达式把所有作为一个组匹配和提取出来的内容附加到链接的后面。 </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">现在，返回Java。就象前面我们所做的那样，你必须创建测试字符串，创建把正则表达式编译到Pattern对象所必需的对象，以及创建一个PatternMatcher对象：<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_v.jpg" border="0" /></td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">接下来，用com.oroinc.text.regex包Util类的substitute()静态方法进行替换，输出结果字符串： </td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_w.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">Util.substitute()方法的语法如下： </td>
						</tr>
				</tbody>
		</table>
		<table height="17" width="620" align="center">
				<tbody>
						<tr>
								<td class="a14" height="13">
										<img alt="" src="http://www.ccw.com.cn/htm/app/aprog/01_7_31_4_x.jpg" border="0" />
								</td>
						</tr>
				</tbody>
		</table>
		<table width="620" align="center">
				<tbody>
						<tr>
								<td class="a14">这个调用的前两个参数是以前创建的PatternMatcher和Pattern对象。第三个参数是一个Substiution对象，它决定了替换操作如何进行。本例使用的是Perl5Substitution对象，它能够进行Perl5风格的替换。第四个参数是想要进行替换操作的字符串，最后一个参数允许指定是否替换模式的所有匹配子串（Util.SUBSTITUTE_ALL），或只替换指定的次数。<br />转自：<a href="http://blog.csdn.net/feng_sundy/archive/2006/03/15/625227.aspx">http://blog.csdn.net/feng_sundy/archive/2006/03/15/625227.aspx</a></td>
						</tr>
				</tbody>
		</table>
<img src ="http://www.blogjava.net/tanghc/aggbug/35935.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tanghc/" target="_blank">JavaWorm</a> 2006-03-18 14:32 <a href="http://www.blogjava.net/tanghc/archive/2006/03/18/35935.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java 开发中遇到的乱码问题</title><link>http://www.blogjava.net/tanghc/archive/2006/03/18/35934.html</link><dc:creator>JavaWorm</dc:creator><author>JavaWorm</author><pubDate>Sat, 18 Mar 2006 06:24:00 GMT</pubDate><guid>http://www.blogjava.net/tanghc/archive/2006/03/18/35934.html</guid><wfw:comment>http://www.blogjava.net/tanghc/comments/35934.html</wfw:comment><comments>http://www.blogjava.net/tanghc/archive/2006/03/18/35934.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tanghc/comments/commentRss/35934.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tanghc/services/trackbacks/35934.html</trackback:ping><description><![CDATA[
		<p>
				<font face="verdana, arial, helvetica">
						<span class="javascript" id="text2295884">
								<font size="2">若想明白jsp开发过程中为什么会产生中文乱码，我们先来看看unicode编码。<br />Unicode （统一码）顾名思义是一个将世界上各种文字统一在一起的东东。由美国各大电脑厂商组成的Unicode策进会来推动。目的，推广一个世界通用的编码体制，惊世界上所有常用的文字都涵盖进去，从而减少个电脑商开发国外市场遇到的问题。<br />为了将成千上万的文字统统收集到一个共同的编码机制下，在兼顾经济的原则下，不管是东方还是西方文字，每个字在Unicode中一律以两个Bytes来表示，这样至少有2的16次方65536种不同的组合，足以应付目前绝大多数场合的需要。<br />基本上，计算机只是处理数字。它们指定一个数字，来储存字母或其他字符。在创造Unicode之前，有数百种指定这些数字的编码系统。没有一个编码可以包含足够的字符：例如，单单欧州共同体就需要好几种不同的编码来包括所有的语言。即使是单一种语言，例如英语，也没有哪一个编码可以适用于所有的字母，标点符号，和常用的技术符号。<br />这些编码系统也会互相冲突。也就是说，两种编码可能使用相同的数字代表两个不同的字符，或使用不同的数字代表相同的字符。任何一台特定的计算机(特别是服务器)都需要支持许多不同的编码，但是，不论什么时候数据通过不同的编码或平台之间，那些数据总会有损坏的危险。<br />Unicode给每个字符提供了一个唯一的数字，不论是什么平台，不论是什么程序，不论什么语言。Unicode标准已经被这些工业界的领导们所采用，例如：Apple, HP, IBM, JustSystem, Microsoft, Oracle, SAP, Sun, Sybase, Unisys和其它许多公司。最新的标准都需要Unicode，例如XML, Java, ECMAScript (JavaScript), LDAP, CORBA 3.0, WML等等，并且，Unicode是实现ISO/IEC 10646的正规方式。许多操作系统，所有最新的浏览器和许多其他产品都支持它。Unicode标准的出现和支持它工具的存在，是近来全球软件技术最重要的发展趋势。<br />将Unicode与客户服务器或多层应用程序和网站结合，比使用传统字符集节省费用。Unicode使单一软件产品或单一网站能够贯穿多个平台，语言和国家，而不需要重建。它可将数据传输到许多不同的系统，而无损坏。<br />在与Unicode相关的各技术文件中，经常会看到ISO 10646和UCS这两个名词。<br />ISO是位于瑞士的国际标准局的缩写。<br />UCS为ISO颁布的第10646号标准 Universal Character Set,就是世界通用字符集。<br />UCS通用字符集采用4个Bytes来编码,将世界上所有的官用和商用编码大小通吃，一网打尽。Unicode自1991年便和ISO的UCS小组密切配合，让Unicode和ISO 10646保持一致。因此Unicode 自2.0版开始，便和 ISO 10646-1使用相同的编码。<br />康熙字典中的汉字有4万7千，如果再加上里面没有的简体字，和不同写法的日文字，那么 Unicode6万多的分配空间，光用来分配汉字就显得捉禁见昼，更别说什么泰文，阿拉伯等其他文字了。针对这个问题Unicode和UCS采用了[中日韩文整合]（CJK Unification）的解决方案，把中日韩笔画详尽的汉字用同一个单码表示。<br />经过[中日韩文整合]的Unicode称为统汉字Unihan。<br />完整的Unicode4.0版可由</font>
								<a href="http://www.unicode.org/Public/UNIDATA/Unihan.txt" target="_blank">
										<font size="2">http://www.unicode.org/Public/UNIDATA/Unihan.txt</font>
								</a>
								<font size="2">下载。<br /><br />UTF (Unicode/UCS Transformation Format),Unicode推荐使用UTF-8和UTF-16两种格式其中8和16指的是Bits数而不是Bytes数。<br />UTF-16基本就是Unicode双字节的实现，加上一个应付未来需要的扩充编码机制（很少用）<br />UTF-8 是一种不等幅的编码方式，英数字（Ascii字码）保持原状，完全不受影响（因此不需要做转换），而其他汉字资料须透过程序来转换，会[变胖]，因为每个字需要额外一个或两个Bytes来编码。<br />UCS字符集中，有UCS-2和UCS-4等编码方式其中的2和4指的是bytes数，对应UTF-8和UTF-16.<br />UCS-2基本和Unicode双byte 编码差不多<br />UCS-4四 byte编码表示一个字，在每个 UCS-2前面加上两个空白的Byte，便可得到对应的 UCS-4。<br /></font>
								<font color="#ff0000">
										<br />
										<font size="2">Unicode的空间分配：<br />以下Unicode区位码均以16进制表示<br />Unicode的前256个字符和ISO-8859-1(西欧字母)完全相同，其中前半段就是Ascii(u+0000到u+00FF)。每个ISO-8859-1码前面补上一个空byte(0x00)后才是相应的Unicode码。<br />和我们切身相关的Unihan主要分布在u+3400到u+F9FFF之间，GB2312和BIG5主要分布在u+4E00到U+9FFF之间。<br /></font>
								</font>
								<br />
								<font size="2">UTF-8的编码原理和特性：<br />知道了西欧字符和汉字在Unicode中的位置后，来看看UTF-8<br /><br />U+0000~U+007E 1 _ _ _ _ _ _ _ (7bits)<br />U+0080~U+07FF 1 1 0_ _ _ _ _ 1 0_ _ _ _ _ _ (11bits)<br />U+0800~U+FFFF 1 1 1 0 _ _ _ _ 1 0 _ _ _ _ _ _ 1 0 _ _ _ _ _ _ (16bits)<br />看一看Unicode三种格式提出自由的bits（下划线空位）是否足以用来代表区位中各Unicode码，刚好够用。<br />那么当程序处理UTF-8编码文件时，如何得知一个字符的疆界落在哪里？还有到底他是以三种形式那一中出现呢？<br />每个以UTF-8编码的字符，不管是以一、二、三个bytes出现，第一个byte前端都清楚地标示了该字符的byte总数。如110种有两个1，代表这种字符是以第二种方式出现，由两个bytes组成。而1110有三个1，表示这种字符一点种方式出现，由三个字节组成。<br />每个多重byte的UTF-8编码有一个共同的通性，即其中的第二个第三个byte, 一律以10两个bits开头。由于其中的最高位总设成1，可以很容易和那些在UTF-8中只用一个Byte的ASCII字元区分开来，方便侦错。<br />因为上述设计特点，UTF-8和Unicode之间，可以很容易做双向自由转换，而不会丢失任何资料。</font>
						</span>
				</font>
		</p>
		<font face="verdana, arial, helvetica">
				<span class="javascript">
						<p>
								<font face="verdana, arial, helvetica" size="2">
										<span class="javascript" id="text2297287">解决办法：乱码问题在 NT操作系统我没怎么碰到过，但是在UNIX 或LINUX系统上出现的比较多<br />由于操作系统和使用环境不一样，产生乱码的方式也不一样，但是如果掌握了上面的Unicode 编码原理，自己仔细分析一下，很多问题便可迎刃而解。<br />下面看看几个常见的例子。<br />1． 如果一些网站服务器例如Tomcat 如果遇到中文乱码问题，可以修改conf目录下的server.xml <br />&lt;Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"<br />enableLookups="false" redirectPort="8443" acceptCount="100" debug="0"connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK"/&gt;<br />URIENcoding 设成GBK或GB2312<br />2． 表单中或传递字符串：本来输入的汉字是正常的，但是提交后再显示出来是乱码，因为提交的一般是 ISO8859编码，所以显示的时候要转成GB2312编码：<br /><br />String S=new String(rs.getString("news").getBytes("gb2312"),"ISO8859_1");<br />//rs为待转换的字符串<br />然后使用S字符串的值就可以了<br />3． 有的服务器端的语言环境如果设成简体中文的也可以解决此类问题<br />4． 插入数据库中的字符是乱码<br />看看数据库中支持的是何种编码方式，用类似2中的方式作一下转换即可。<br />5． 总之，用jsp开发，碰到乱码，你得分析是读的时候发生乱码，还是写的时候发生乱码，用2中的转换，基本就能解决问题，有些时候写的时候做一次转换，例如：<br />String S=new String(rs.getString("news").getBytes("gb2312"),"ISO8859_1");<br />//读的时候在转换回来<br />String S=new String(rs.getString("news").getBytes("ISO8859_1"),"GB2312");<br />或者把ISO8859-1和GB2312 的位置换一下，自己多试试，就能找到解决问题的办法。</span>
										<br />
										<br />来自：<a href="http://blog.csdn.net/softj/archive/2006/03/15/625193.aspx">http://blog.csdn.net/softj/archive/2006/03/15/625193.aspx</a></font>
						</p>
				</span>
		</font>
<img src ="http://www.blogjava.net/tanghc/aggbug/35934.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tanghc/" target="_blank">JavaWorm</a> 2006-03-18 14:24 <a href="http://www.blogjava.net/tanghc/archive/2006/03/18/35934.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>