﻿<?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-VIRGIN FOREST OF JAVA-文章分类-J2SE</title><link>http://www.blogjava.net/RR00/category/2691.html</link><description>不要埋头苦干，要学习，学习，再学习。。。。。
&lt;br&gt;
powered  by &lt;font color='orange'&gt;R.Zeus&lt;/font&gt;</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 10:37:16 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 10:37:16 GMT</pubDate><ttl>60</ttl><item><title>Explore Java's static nested classes and inner classes</title><link>http://www.blogjava.net/RR00/articles/78932.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Fri, 03 Nov 2006 07:42:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/78932.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/78932.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/78932.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/78932.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/78932.html</trackback:ping><description><![CDATA[
		<font size="2"> </font>
		<h1>Explore Java's static nested classes and inner classes</h1>
		<br />
		<font class="section">by  </font>
		<font class="contentLink" face="Verdana" color="#0000cc" size="1">
				<a href="mailto:support@techrepublic.com?subject=Explore Java's static nested classes and inner classes">David Petersheim </a>
		</font>
		<font class="section"> | </font>
		<a href="http://builder.com.com/5171-22-5103334.html">
				<font class="contentLink" face="Verdana" color="#000000" size="1">More from David Petersheim </font>
		</a>
		<font class="section"> |  Published: 8/12/05 </font>
		<br />
		<p>
				<span class="normalArial" twffan="done">Starting with JDK 1.1, Java provided the ability to create nested classes. A nested class is defined inside another class. There are two types of nested classes: static nested classes and inner classes. </span>
		</p>
		<p>
				<span class="normalArial" twffan="done">A static nested class is declared inside another class with the static keyword or within a static context of that class. A static class has no access to instance-specific data. </span>
		</p>
		<p>
				<span class="normalArial" twffan="done">An inner class is a nonstatic class declared inside another class. It has access to all of its enclosing class's instance data, including private fields and methods. Inner classes may access static data but may not declare static members unless those members are compile time constants. </span>
		</p>
		<p>
				<span class="normalArial" twffan="done">Deciding which type of nested class to use depends on the data type your nested class needs to access. If you need access to instance data, you'll need an inner class. If you don't need access to instance data, a static nested class will suffice. </span>
		</p>
		<p>
				<span class="normalArial" twffan="done">The most common use of inner classes is as event handlers for GUI-based applications. These classes are usually declared anonymously and as needed for each component that requires an event handler. The advantage of using an inner class for event handling is that you can avoid large If/else statements to decide which component is being handled. Each component gets its own event handler, so each event handler knows implicitly the component for which it's working. </span>
		</p>
		<p>
				<span class="normalArial" twffan="done">The snippet below creates an anonymous inner class to handle the events created by an application's OK button. </span>
		</p>
		<font face="mono" size="-1">
				<p class="code">Button btn = new Button("Ok");<br />btn.addActionListener(<br />    new ActionListener() {<br />        public void actionPerformed(ActionEvent ae) {<br />            okClicked();<br />        }<br />    }<br />); </p>
		</font>
		<p>
				<span class="normalArial" twffan="done">The advantage of a static nested class is that it doesn't need an instance of the containing class to work. This can help you reduce the number of objects your application creates at runtime. </span>
		</p>
		<p>
				<span class="normalArial" twffan="done">The semantics for creating instances of nested classes can be confusing. Below is a simple class that defines a static nested class and an inner class. Pay special attention to the main method, where an instance of each instance class is created. </span>
		</p>
		<font face="mono" size="-1">
				<p class="code">// creating an instance of the enclosing class<br />NestedClassTip nt = new NestedClassTip();<br /><br /><br />// creating an instance of the inner class requires<br />// a reference to an instance of the enclosing class<br />NestedClassTip.NestedOne nco = nt.new NestedOne();<br /><br /><br />// creating an instance of the static nested class<br />// does not require an instance of the enclosing class<br />NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo();<br /><br /><br /><br />public class NestedClassTip {<br />    private String name = "instance name";<br />    private static String staticName = "static name";<br /><br />    public static void main(String args[]) {<br />        NestedClassTip nt = new NestedClassTip();<br /><br />        NestedClassTip.NestedOne nco = nt.new NestedOne();<br /><br />        NestedClassTip.NestedTwo nct = new NestedClassTip.NestedTwo();<br />    }<br /><br />    class NestedOne {<br />        NestedOne() {<br />            System.out.println(name);<br />            System.out.println(staticName);<br />        }<br />    }<br /><br />    static class NestedTwo {<br />        NestedTwo() {<br />            System.out.println(staticName);<br />        }<br />    }<br />} </p>
		</font>
		<p>
				<span class="normalArial" twffan="done">Nested classes can be confusing, but once you understand their purpose and get used to the semantics, there isn't a lot to them. If you'd like to learn more about the details of nested classes, check out the <a href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html" target="_blank"><font color="#0000cc">Java Language Specification</font></a>. </span>
		</p>
		<p>
				<i>
						<a href="http://nl.com.com/MiniFormHandler?brand=techrepublic&amp;list_id=e027">
								<font color="#0000cc">
								</font>
						</a>
				</i> </p>
<img src ="http://www.blogjava.net/RR00/aggbug/78932.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-11-03 15:42 <a href="http://www.blogjava.net/RR00/articles/78932.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>String.intern()</title><link>http://www.blogjava.net/RR00/articles/78242.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Tue, 31 Oct 2006 04:05:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/78242.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/78242.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/78242.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/78242.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/78242.html</trackback:ping><description><![CDATA[mechanisms for ensuring that Strings  which are String.equal() are also ==.<img src ="http://www.blogjava.net/RR00/aggbug/78242.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-10-31 12:05 <a href="http://www.blogjava.net/RR00/articles/78242.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Performance: Hibernate startup time</title><link>http://www.blogjava.net/RR00/articles/78093.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 30 Oct 2006 09:22:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/78093.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/78093.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/78093.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/78093.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/78093.html</trackback:ping><description><![CDATA[
		<p>From: <a href="http://www.hibernate.org/194.html">Performance: Hibernate startup time</a><br /><br />The following tips hints how to make Hibernate startup faster.</p>
		<a name="A2">
		</a>
		<h2>Only add the mapping files you need!</h2>
		<blockquote>
				<p>If you are running some few JUnit tests for a 400++ classes project you probably don't hit every class in those tests and thus do not need to add all those hbm.xml's to the Configuration. Go look at Hibernate's test suite on how you <strong>could</strong> let your TestCase decide what classes should be defined in the mapping.</p>
		</blockquote>
		<a name="A3">
		</a>
		<h2>Use serialized XML documents when configuring Configuration</h2>
		<blockquote>
				<p>When building the configuration 40-60% of the time is used by the XML parsers and Dom4j to read up the XML document. Significant performance increases can be done by serializing the Document object's to disc once, and afterwards just add them to the configuration by deserializing them first.</p>
				<p>In the current cvs we have an experimental Configuration.addCachableFile() method that can be used as inspiration for previous Hibernate versions.</p>
				<pre class="code">public Configuration addCachableFile(String xmlFile) throws MappingException {        
        try {
            File file = new File(xmlFile);
            File lazyfile = new File(xmlFile + ".bin");
            org.dom4j.Document doc = null; 
            List errors = new ArrayList();
            if(file.exists() &amp;&amp; lazyfile.exists() &amp;&amp; file.lastModified()&lt;lazyfile.lastModified()) {
                log.info("Mapping lazy file: " + lazyfile.getPath());
                ObjectInputStream oip = null;
                oip = new ObjectInputStream(new FileInputStream(lazyfile));
                doc = (org.dom4j.Document) oip.readObject();
                oip.close(); 
            } else {
                doc = xmlHelper.createSAXReader(xmlFile, errors, entityResolver).read( file );
                log.info("Writing lazy file to " + lazyfile);
                ObjectOutputStream oup = new ObjectOutputStream(new FileOutputStream(lazyfile));
                oup.writeObject(doc);
                oup.flush();
                oup.close();
            }
            
            if ( errors.size()!=0 ) throw new MappingException( "invalid mapping", (Throwable) errors.get(0) );
            add(doc);
            return this;
        }
        catch (Exception e) {
            log.error("Could not configure datastore from file: " + xmlFile, e);
            throw new MappingException(e);
        }
    }
</pre>
		</blockquote>
		<a name="A4">
		</a>
		<h2>Disable Hibernates usage of cglib reflection optimizer</h2>
		<blockquote>
				<p>Put the following line in hibernate.properties:</p>
				<pre class="code">hibernate.cglib.use_reflection_optimizer=false
</pre>
				<p>It will make Hibernate start faster since it does not try to build cglib-enhanced objects to access getter/setters.</p>
				<p>Note: It will have in impact on overall runtime performance since Hibernate will be forced to use standard JDK reflection for access. So it is most useful during development. (You will also get better error messages in some situations when the optimizer is disabled ;)</p>
		</blockquote>
		<!-- End Body -->
		<div twffan="done">
				<br />
				<table style="BACKGROUND: #a0a0a0; WIDTH: 100%" cellspacing="0" cellpadding="0" border="0">
						<tbody>
								<tr>
										<td width="100%"> </td>
										<td onmouseup="fireClickEventOnChild(this, 'A')" class="topNav2" onmouseover="mover(this, '#cccccc')" onmouseout="mout(this, '#a0a0a0')" valign="center">
												<a onfocus="if (this.blur) this.blur()" href="http://www.hibernate.org/194.html?comid=0&amp;cmd=newcom">
														<span class="topNav2Link" twffan="done">NEW COMMENT</span>
												</a>
										</td>
								</tr>
						</tbody>
				</table>
				<br />
				<table style="BACKGROUND: #cccccc" cellspacing="0" cellpadding="0" width="100%" border="0">
						<tbody>
								<tr>
										<td style="FLOAT: left; MARGIN-LEFT: 4px; WIDTH: 100%" nowrap="">
												<a href="http://www.hibernate.org/194.220.html">Serializing the Configuration object</a>
												<div twffan="done">
												</div>
										</td>
										<td class="label" style="WIDTH: 150px; COLOR: black" nowrap="">04 May 2004, 12:37 
<div twffan="done"></div></td>
										<td class="label" style="COLOR: black" nowrap="">luish 
<div twffan="done"></div></td>
								</tr>
								<tr>
										<td style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; BACKGROUND: #eeeeee; PADDING-BOTTOM: 4px; PADDING-TOP: 4px" colspan="3">
												<pre>Another approach would be to serialize the whole Configuration 
object. What do you think about this? I have submitted a patch to  
the Jira to make the Configuration Serializable (see bugs 492 and 
147).</pre>
										</td>
								</tr>
								<tr>
										<td bgcolor="#ffffff" colspan="3"> </td>
								</tr>
								<tr>
										<td style="FLOAT: left; MARGIN-LEFT: 4px; WIDTH: 100%" nowrap="">
												<a href="http://www.hibernate.org/194.275.html">addLazyFile() not there.</a>
												<div twffan="done">
												</div>
										</td>
										<td class="label" style="WIDTH: 150px; COLOR: black" nowrap="">06 Jul 2004, 11:50 
<div twffan="done"></div></td>
										<td class="label" style="COLOR: black" nowrap="">gstamp 
<div twffan="done"></div></td>
								</tr>
								<tr>
										<td style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; BACKGROUND: #eeeeee; PADDING-BOTTOM: 4px; PADDING-TOP: 4px" colspan="3">
												<pre>I can't fine addLazyFile() in CVS.  Is it still supposed to be there?</pre>
										</td>
								</tr>
								<tr>
										<td bgcolor="#ffffff" colspan="3"> </td>
								</tr>
								<tr>
										<td style="FLOAT: left; MARGIN-LEFT: 4px; WIDTH: 100%" nowrap="">
												<a href="http://www.hibernate.org/194.328.html">Hibernate3 feature</a>
												<div twffan="done">
												</div>
										</td>
										<td class="label" style="WIDTH: 150px; COLOR: black" nowrap="">31 Aug 2004, 11:13 
<div twffan="done"></div></td>
										<td class="label" style="COLOR: black" nowrap="">gavin 
<div twffan="done"></div></td>
								</tr>
								<tr>
										<td style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; BACKGROUND: #eeeeee; PADDING-BOTTOM: 4px; PADDING-TOP: 4px" colspan="3">
												<pre>Try the Hibernate3 module (or just the alpha release)</pre>
										</td>
								</tr>
								<tr>
										<td bgcolor="#ffffff" colspan="3"> </td>
								</tr>
								<tr>
										<td style="FLOAT: left; MARGIN-LEFT: 4px; WIDTH: 100%" nowrap="">
												<a href="http://www.hibernate.org/194.469.html">Information update?</a>
												<div twffan="done">
												</div>
										</td>
										<td class="label" style="WIDTH: 150px; COLOR: black" nowrap="">30 Mar 2005, 07:54 
<div twffan="done"></div></td>
										<td class="label" style="COLOR: black" nowrap="">gruberc 
<div twffan="done"></div></td>
								</tr>
								<tr>
										<td style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; BACKGROUND: #eeeeee; PADDING-BOTTOM: 4px; PADDING-TOP: 4px" colspan="3">
												<pre>The information on this page does not seem to be correct any
more. With Hibernate 3.0rc1, there is no Configuration.addLazyFile()
any more, but addCacheableFile(). How should it be used?</pre>
										</td>
								</tr>
								<tr>
										<td bgcolor="#ffffff" colspan="3"> </td>
								</tr>
								<tr>
										<td style="FLOAT: left; MARGIN-LEFT: 4px; WIDTH: 100%" nowrap="">
												<a href="http://www.hibernate.org/194.664.html">lazy</a>
												<div twffan="done">
												</div>
										</td>
										<td class="label" style="WIDTH: 150px; COLOR: black" nowrap="">06 Mar 2006, 05:09 
<div twffan="done"></div></td>
										<td class="label" style="COLOR: black" nowrap="">steckemetz 
<div twffan="done"></div></td>
								</tr>
								<tr>
										<td style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; BACKGROUND: #eeeeee; PADDING-BOTTOM: 4px; PADDING-TOP: 4px" colspan="3">
												<pre>If you have terrible problems with startup time and
do NOT need certain features like:

* proxy objects
* lazy loading

or if you are using the stateless session,
then you can disable lazyness on class level like:

&lt;class name="myClass" table="myTable" lazy="false"&gt;

The default is true and forces byte code generation of
some proxy class which takes a lot of time.
Perhaps some hibernate guru can tell us, which other
features will be disabled by this.</pre>
										</td>
								</tr>
								<tr>
										<td bgcolor="#ffffff" colspan="3"> </td>
								</tr>
								<tr>
										<td style="FLOAT: left; MARGIN-LEFT: 4px; WIDTH: 100%" nowrap="">
												<a href="http://www.hibernate.org/194.724.html">I solve it.</a>
												<div twffan="done">
												</div>
										</td>
										<td class="label" style="WIDTH: 150px; COLOR: black" nowrap="">02 Aug 2006, 00:12 
<div twffan="done"></div></td>
										<td class="label" style="COLOR: black" nowrap="">cm4ever 
<div twffan="done"></div></td>
								</tr>
								<tr>
										<td style="PADDING-RIGHT: 4px; PADDING-LEFT: 4px; BACKGROUND: #eeeeee; PADDING-BOTTOM: 4px; PADDING-TOP: 4px" colspan="3">
												<pre>The Hibernate Configuration module implement is very bad.
I write a module to realize the dynamic loading mapping files.
But other function I can't resolve...

Hibernate Dynamic Module
This project is only a module of Hibernate <a href="http://www.hibernate.org/" target="_blank">http://www.hibernate.org</a> Read
mapping file until insert/update/delete/select the persistent class in
Hibernate.
<a href="http://sourceforge.net/projects/hbn-dyn-mod/" target="_blank">http://sourceforge.net/projects/hbn-dyn-mod/</a></pre>
										</td>
								</tr>
						</tbody>
				</table>
		</div>
<img src ="http://www.blogjava.net/RR00/aggbug/78093.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-10-30 17:22 <a href="http://www.blogjava.net/RR00/articles/78093.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>an easy example for Serializable from web</title><link>http://www.blogjava.net/RR00/articles/78083.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 30 Oct 2006 08:40:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/78083.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/78083.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/78083.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/78083.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/78083.html</trackback:ping><description><![CDATA[
		<p>
				<font color="#ff1493">        Configuration configuration=null;<br />        try {<br />            Configuration configurationSerializable = new Configuration();<br />            FileOutputStream fos = new FileOutputStream("serial");<br />            ObjectOutputStream oos = new ObjectOutputStream(fos);<br />            oos.writeObject(configurationSerializable);<br />            oos.flush();<br />            oos.close();<br />        } catch (FileNotFoundException e) {<br />            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.<br />        }<br />        catch (IOException e) {<br />            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.<br />        }</font>
		</p>
		<p>
				<font color="#ff1493">        try {<br />            FileInputStream fis = new FileInputStream("serial");<br />            ObjectInputStream ois = new ObjectInputStream(fis);<br />            configuration = (Configuration) ois.readObject();<br />            ois.close();<br />        } catch (FileNotFoundException e) {<br />            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.<br />        }<br />        catch (IOException e) {<br />            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.<br />        }<br />        catch (ClassNotFoundException e) {<br />            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.<br />        }<br />        if(configuration!=null)<br />        {<br />        SessionFactory sessionFactory = configuration.configure().buildSessionFactory();<br />        Session session = sessionFactory.openSession();<br />        Transaction transaction = session.beginTransaction();<br />        callBack.doing(session);<br />        transaction.commit();<br />        }<br />    }<br /></font>
				<font color="#000000">when will Configuration serialize and why it's some field seted transient?the example will be error because some field transient is null after serialize.</font>
		</p>
<img src ="http://www.blogjava.net/RR00/aggbug/78083.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-10-30 16:40 <a href="http://www.blogjava.net/RR00/articles/78083.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>HashMap and TreeMap</title><link>http://www.blogjava.net/RR00/articles/78080.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 30 Oct 2006 08:32:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/78080.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/78080.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/78080.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/78080.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/78080.html</trackback:ping><description><![CDATA[public class <font color="#ff1493">HashMap</font>&lt;K,V&gt; extends AbstractMap&lt;K,V&gt; implements <font color="#ff1493">Map</font>&lt;K,V&gt;, Cloneable, Serializable<br /><br />public class <font color="#ff1493">TreeMap</font>&lt;K,V&gt;<br />    extends AbstractMap&lt;K,V&gt;<br />    implements <font color="#ff1493">SortedMap</font>&lt;K,V&gt;, Cloneable, java.io.Serializable<br /><br />Obviously,TreeMap store elements sorted.But for the chinese word ,the default <font color="#ff1493">Comparator</font> don't  do it rightly,so you must write your <font color="#ff1493">Comparator</font> .this is an example from web:<br /><br /><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">import</span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"></span><span lang="EN-US" style="BACKGROUND: yellow; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: yellow" twffan="done">java.text.CollationKey</span><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">;</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">import</span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"> java.text.Collator;</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">import</span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"> java.util.Comparator;</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p><font size="3"> </font></o:p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><span lang="EN-US" style="BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">/**</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"><span style="mso-spacerun: yes" twffan="done"> </span></span><span lang="EN-US" style="BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">*</span><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"></span><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f9fbf; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">@author </span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">www</span><span lang="EN-US" style="COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done">.<span style="BACKGROUND: white; mso-highlight: white" twffan="done">inspiresky</span>.<span style="BACKGROUND: white; mso-highlight: white" twffan="done">com</span></span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"><span style="mso-spacerun: yes" twffan="done"> </span></span><span lang="EN-US" style="BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">*</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"><span style="mso-spacerun: yes" twffan="done"> </span></span><span lang="EN-US" style="BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">*/</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">public</span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"></span><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">class</span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"> CollatorComparator </span><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">implements</span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"> Comparator {</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"><span style="mso-tab-count: 1" twffan="done"></span><font size="3">Collator </font></span><font size="3"><span lang="EN-US" style="BACKGROUND: white; COLOR: #0000c0; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">collator</span><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"> = Collator.<i>getInstance</i>();</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p><font size="3"> </font></o:p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"><span style="mso-tab-count: 1" twffan="done"></span></span><font size="3"><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">public</span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"></span><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">int</span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"> compare(Object element1, Object element2) {</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"><span style="mso-tab-count: 2" twffan="done">    </span></span><span lang="EN-US" style="BACKGROUND: yellow; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: yellow" twffan="done">CollationKey</span><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"> key1 = </span><span lang="EN-US" style="BACKGROUND: white; COLOR: #0000c0; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">collator</span><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">.getCollationKey(element1.toString());</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"><span style="mso-tab-count: 2" twffan="done">    </span></span><span lang="EN-US" style="BACKGROUND: yellow; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: yellow" twffan="done">CollationKey</span><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"> key2 = </span><span lang="EN-US" style="BACKGROUND: white; COLOR: #0000c0; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">collator</span><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">.getCollationKey(element2.toString());</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><font size="3"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"><span style="mso-tab-count: 2" twffan="done">    </span></span><b><span lang="EN-US" style="BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">return</span></b><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"> key1.compareTo(key2);</span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-ALIGN: left; mso-layout-grid-align: none; mso-para-margin-left: 1.71gd" align="left"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done"><span style="mso-tab-count: 1" twffan="done"></span><font size="3">}</font></span><span lang="EN-US" style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><font size="3"><span lang="EN-US" style="BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-highlight: white" twffan="done">}<br /><br /><br /></span><span lang="EN-US" style="COLOR: black; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt" twffan="done"><o:p></o:p></span></font></p><font face="Courier New">  to use this <font color="#ff1493">CollatorComparator</font> ,use TreeMap constructor<br />Mothod :<br /><br /><font color="#ff1493"> public TreeMap(Comparator&lt;? super K&gt; c) {<br />        this.comparator = c;<br />    }<br /></font><br /></font><br /><img src ="http://www.blogjava.net/RR00/aggbug/78080.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-10-30 16:32 <a href="http://www.blogjava.net/RR00/articles/78080.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java access specifiers ---protected</title><link>http://www.blogjava.net/RR00/articles/78045.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 30 Oct 2006 06:17:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/78045.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/78045.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/78045.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/78045.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/78045.html</trackback:ping><description><![CDATA[
		<p>Sometimes the creator of the base class would like to take a particular member and grant access to derived classes but not the world in general. That’s what <b>protected</b> does. <b>protected</b> also gives package access—that is, other classes in the same package may access <b>protected</b> elements.<br /></p>
<img src ="http://www.blogjava.net/RR00/aggbug/78045.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-10-30 14:17 <a href="http://www.blogjava.net/RR00/articles/78045.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>浅析Java中Date类的应用</title><link>http://www.blogjava.net/RR00/articles/74033.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 09 Oct 2006 04:18:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/74033.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/74033.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/74033.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/74033.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/74033.html</trackback:ping><description><![CDATA[
		<br />
		<b>创建一个日期对象 </b>
		<br />让我们看一个使用系统的当前日期和时间创建一个日期对象并返回一个长整数的简单例子. 这个时间通常被称为Java 虚拟机(JVM)主机环境的系统时间. <br /><pre class="overflow">import java.util.Date; <br /><br />public class DateExample1 { <br />    public static void main(String［］ args) { //自己替换［］<br />    // Get the system date/time <br />    Date date = new Date(); <br /><br />    System.out.println(date.getTime()); <br />    } <br />} </pre><br /><br />在星期六, 2001年9月29日, 下午大约是6:50的样子, 上面的例子在系统输出设备上显示的结果是 1001803809710. 在这个例子中,值得注意的是我们使用了Date 构造函数创建一个日期对象, 这个构造函数没有接受任何参数. 而这个构造函数在内部使用了System.currentTimeMillis() 方法来从系统获取日期. <br /><br />那么, 现在我们已经知道了如何获取从1970年1月1日开始经历的毫秒数了. 我们如何才能以一种用户明白的格式来显示这个日期呢? 在这里类java.text.SimpleDateFormat 和它的抽象基类 java.text.DateFormat 就派得上用场了. <br /><br /><br /><b>日期数据的定制格式 </b><br />假如我们希望定制日期数据的格式, 比方星期六-9月-29日-2001年. 下面的例子展示了如何完成这个工作: <br /><br /><pre class="overflow">import java.text.SimpleDateFormat; <br />import java.util.Date; <br /><br />public class DateExample2 { <br /><br />    public static void main(String［］ args) { //自己替换［］<br /><br />        SimpleDateFormat bartDateFormat = <br />        new SimpleDateFormat("EEEE-MMMM-dd-yyyy"); <br /><br />        Date date = new Date(); <br /><br />        System.out.println(bartDateFormat.format(date)); <br />    } <br />}</pre><br /><br />只要通过向SimpleDateFormat 的构造函数传递格式字符串"EEE-MMMM-dd-yyyy", 我们就能够指明自己想要的格式. 你应该可以看见, 格式字符串中的ASCII 字符告诉格式化函数下面显示日期数据的哪一个部分. EEEE是星期, MMMM是月, dd是日, yyyy是年. 字符的个数决定了日期是如何格式化的.传递"EE-MM-dd-yy"会显示 Sat-09-29-01. 请察看Sun 公司的Web 站点获取日期格式化选项的完整的指示. <br /><br /><b>将文本数据解析成日期对象 </b><br />假设我们有一个文本字符串包含了一个格式化了的日期对象, 而我们希望解析这个字符串并从文本日期数据创建一个日期对象. 我们将再次以格式化字符串"MM-dd-yyyy" 调用SimpleDateFormat类, 但是这一次, 我们使用格式化解析而不是生成一个文本日期数据. 我们的例子, 显示在下面, 将解析文本字符串"9-29-2001"并创建一个值为001736000000 的日期对象. <br /><br />例子程序: <br /><br /><pre class="overflow">import java.text.SimpleDateFormat; <br />import java.util.Date; <br /><br />public class DateExample3 { <br /><br />    public static void main(String［］args) { //自己替换［］<br />        // Create a date formatter that can parse dates of <br />        // the form MM-dd-yyyy. <br />        SimpleDateFormat bartDateFormat = <br />        new SimpleDateFormat("MM-dd-yyyy"); <br /><br />        // Create a string containing a text date to be parsed. <br />        String dateStringToParse = "9-29-2001"; <br /><br />        try { <br />            // Parse the text version of the date. <br />            // We have to perform the parse method in a <br />            // try-catch construct in case dateStringToParse <br />            // does not contain a date in the format we are expecting. <br />            Date date = bartDateFormat.parse(dateStringToParse); <br /><br />            // Now send the parsed date as a long value <br />            // to the system output. <br />            System.out.println(date.getTime()); <br />        } <br />        catch (Exception ex) { <br />            System.out.println(ex.getMessage()); <br />        } <br />    } <br />} </pre><br /><br /><b>使用标准的日期格式化过程 </b><br />既然我们已经可以生成和解析定制的日期格式了, 让我们来看一看如何使用内建的格式化过程. 方法 DateFormat.getDateTimeInstance() 让我们得以用几种不同的方法获得标准的日期格式化过程. 在下面的例子中, 我们获取了四个内建的日期格式化过程. 它们包括一个短的, 中等的, 长的, 和完整的日期格式. <br /><br /><pre class="overflow">import java.text.DateFormat; <br />import java.util.Date; <br /><br />public class DateExample4 { <br /><br />    public static void main(String［］ args) { //自己替换［］<br />        Date date = new Date(); <br /><br />        DateFormat shortDateFormat = <br />        DateFormat.getDateTimeInstance( <br />        DateFormat.SHORT, <br />        DateFormat.SHORT); <br /><br />        DateFormat mediumDateFormat = <br />        DateFormat.getDateTimeInstance( <br />        DateFormat.MEDIUM, <br />        DateFormat.MEDIUM); <br /><br />        DateFormat longDateFormat = <br />        DateFormat.getDateTimeInstance( <br />        DateFormat.LONG, <br />        DateFormat.LONG); <br /><br />        DateFormat fullDateFormat = <br />        DateFormat.getDateTimeInstance( <br />        DateFormat.FULL, <br />        DateFormat.FULL); <br /><br />        System.out.println(shortDateFormat.format(date)); <br />        System.out.println(mediumDateFormat.format(date)); <br />        System.out.println(longDateFormat.format(date)); <br />        System.out.println(fullDateFormat.format(date)); <br />    } <br />} </pre><br /><br /><br /><br />注意我们在对 getDateTimeInstance的每次调用中都传递了两个值. 第一个参数是日期风格, 而第二个参数是时间风格. 它们都是基本数据类型int(整型). 考虑到可读性, 我们使用了DateFormat 类提供的常量: SHORT, MEDIUM, LONG, 和 FULL. 要知道获取时间和日期格式化过程的更多的方法和选项, 请看Sun 公司Web 站点上的解释. <br /><br />运行我们的例子程序的时候, 它将向标准输出设备输出下面的内容: <br />9/29/01 8:44 PM <br />Sep 29, 2001 8:44:45 PM <br />September 29, 2001 8:44:45 PM EDT <br />Saturday, September 29, 2001 8:44:45 PM EDT <br /><img src ="http://www.blogjava.net/RR00/aggbug/74033.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-10-09 12:18 <a href="http://www.blogjava.net/RR00/articles/74033.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>DATE and TIMESTAMP </title><link>http://www.blogjava.net/RR00/articles/74029.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 09 Oct 2006 03:54:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/74029.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/74029.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/74029.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/74029.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/74029.html</trackback:ping><description><![CDATA[
		<p class="pBody">
				<span style="FONT-WEIGHT: bold">DATE</span>
		</p>
		<a name="wp845595">
		</a>
		<p class="pBody">The DATE data type stores date and time information. For each DATE value, Oracle stores the following information: year, month, day, hour, minute, and second. </p>
		<a name="wp845596">
		</a>
		<p class="pBody">The date value can be specified as an ANSI date literal, an Oracle date literal or can be converted from a character or numeric value with the TO_DATE function. The ANSI date literal contains <span style="FONT-STYLE: italic">no</span> time portion, and must be specified in the format 'YYYY-MM-DD'. The default date format for an Oracle date literal can be changed by the initialization parameter NLS_DATE_FORMAT. </p>
		<a name="wp845597">
		</a>
		<p class="pBody">Date data can range from January 1, 4712 BC to December 31, 9999. </p>
		<a name="wp845598">
		</a>
		<p class="pBody">If a date value is specified without a time component, then the default time is 12:00:00 AM. If a date value is specified without a date, then the default date is the first day of the current month. <br /></p>
		<a name="wp845599">
		</a>
		<p class="pBody">
				<span style="FONT-WEIGHT: bold">
						<font style="BACKGROUND-COLOR: #ff0000">---------------------------------------------------------------------------------------------------------------------------------------------------<br /></font>
						<br />TIMESTAMP</span>[(fractional_seconds_precision)] </p>
		<a name="wp845600">
		</a>
		<p class="pBody">The TIMESTAMP data type is an extension of the DATE data type. For each TIMESTAMP value, Oracle stores the following information: year, month, day, hour, minute, second and fraction of second. </p>
		<a name="wp845601">
		</a>
		<p class="pBody">fractional_seconds_precision optionally specifies the number of digits in the fractional part of second and can be a number in the range 0 to 9. The default is 6. </p>
		<a name="wp845602">
		</a>
		<p class="pBody">The TIMESTAMP data type is available in Oracle 9i Release 1 (9.0.1) or later. </p>
<img src ="http://www.blogjava.net/RR00/aggbug/74029.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-10-09 11:54 <a href="http://www.blogjava.net/RR00/articles/74029.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java中在程序中设置代理服务器</title><link>http://www.blogjava.net/RR00/articles/73823.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Sun, 08 Oct 2006 07:04:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/73823.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/73823.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/73823.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/73823.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/73823.html</trackback:ping><description><![CDATA[
		<font color="#333333">　在 Java 中代理服务器的基本设置是通过设置系统属性来完成的。而代理服务器的验证则是通过设置 Http 请求头来完成的。 <br />　　下面的是一个简单的例子供大家参考： <br />　　<br />　　// 根据地址 url 打开 Http 连接 <br />　　HttpURLConnection con = (HttpURLConnection)( new URL( url ) ).openConnection(); <br />　　if (proxy.hasProxy()) { <br />　　// 注意： 如果 proxySet 为 false 时，依然设置了 proxyHost 和 proxyPort，代理设置仍会起作用。 <br />　　// 如果 proxyPort 设置有问题，代理设置不会起作用。 <br />　　System.getProperties().put( "proxySet", "true" ); <br />　　System.getProperties().put( "proxyHost", proxy.getProxyHost() ); <br />　　System.getProperties().put( "proxyPort", String.valueOf( proxy.getProxyPort() ) ); <br />　　<br />　　// 如果需要代理服务器验证，在 Http 请求头中加入 Proxy-Authorization 头， <br />　　// 格式为： "Basic " + ("代理服务器用户名：密码"的 BASE64 编码) <br />　　if (proxy.needAuth()) { <br />　<font color="#a52a2a">　con.setRequestProperty( "Proxy-Authorization", "Basic " + Encoder.base64Encode( proxy.getProxyUser() + ":" + proxy.getProxyPass() ) );</font><font color="#ffa500"><br /></font>　　} <br />　　} </font>
		<br />
<img src ="http://www.blogjava.net/RR00/aggbug/73823.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-10-08 15:04 <a href="http://www.blogjava.net/RR00/articles/73823.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mappings.setDefaultLazy(dlNode == null || dlNode.getValue().equals("true"));</title><link>http://www.blogjava.net/RR00/articles/73081.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Sat, 30 Sep 2006 10:07:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/73081.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/73081.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/73081.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/73081.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/73081.html</trackback:ping><description><![CDATA[
		<font color="#ff0000">1st:</font>   mappings.setDefaultLazy(dlNode == null || dlNode.getValue().equals("true"));<br /><font color="#ff0000">2ed:</font>  mappings.setAutoImport((aiNode == null) ? true : "true".equals(aiNode.getValue()));<br /><br />the first is seems a bit more effective than the second but less readable ,hence we choose the second!<br />bad programmer write code readed by machine and by contraries good programmer write code readed by human!<img src ="http://www.blogjava.net/RR00/aggbug/73081.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-30 18:07 <a href="http://www.blogjava.net/RR00/articles/73081.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>about callback and decorator pattern</title><link>http://www.blogjava.net/RR00/articles/72855.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Fri, 29 Sep 2006 08:17:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/72855.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/72855.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/72855.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/72855.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/72855.html</trackback:ping><description><![CDATA[callback is using when many object has the same pre-init and finally work.<br />decorator is using when one object has may ways to do pre-init and finally work.<img src ="http://www.blogjava.net/RR00/aggbug/72855.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-29 16:17 <a href="http://www.blogjava.net/RR00/articles/72855.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>refill of if-else or switch</title><link>http://www.blogjava.net/RR00/articles/72633.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Thu, 28 Sep 2006 09:30:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/72633.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/72633.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/72633.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/72633.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/72633.html</trackback:ping><description><![CDATA[strategy pattern and state pattern  are good way for replacing if-else or switch.strategy pattern is for choose appropriate arithmetic,state is for automatically changing state,single factory pattern for create appropriate instance and abstract factory    choose appropriate  instance by programmer.<img src ="http://www.blogjava.net/RR00/aggbug/72633.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-28 17:30 <a href="http://www.blogjava.net/RR00/articles/72633.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>another method equals abstract factory method pattern </title><link>http://www.blogjava.net/RR00/articles/72630.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Thu, 28 Sep 2006 09:20:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/72630.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/72630.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/72630.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/72630.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/72630.html</trackback:ping><description><![CDATA[abstract factory method pattern create an abstract method for uncertain logic application which used in this class's other method so the class can complete  the logic operate. there is another way to do so that is <font style="BACKGROUND-COLOR: #ee82ee">nested public static interface</font> .<br />I found this when i read the org.hibernate.hql.ast.util.NodeTraverser.by the way,the command pattern is somewhat like those way.<img src ="http://www.blogjava.net/RR00/aggbug/72630.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-28 17:20 <a href="http://www.blogjava.net/RR00/articles/72630.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>use abstract class or interface as new instance</title><link>http://www.blogjava.net/RR00/articles/72529.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Thu, 28 Sep 2006 04:29:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/72529.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/72529.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/72529.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/72529.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/72529.html</trackback:ping><description><![CDATA[if you want to use abstract class or interface as new instance and don't want to create a class that extends abtract class or implements interface ,you can do like this:<br /><br />interface=new interface(or abtract class)<br />{<br />//in this rigion ,implements all the method needed(e.g. abstract method,method in interface)<br />.......<br />}<br /><br />this use in the case that one interface has many implements which  just be used once ,so this way is a lazy but good way.<br /><br />this way called "Anonymous class";<img src ="http://www.blogjava.net/RR00/aggbug/72529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-28 12:29 <a href="http://www.blogjava.net/RR00/articles/72529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Mappings for many properties</title><link>http://www.blogjava.net/RR00/articles/71802.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 25 Sep 2006 09:39:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/71802.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/71802.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/71802.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/71802.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/71802.html</trackback:ping><description><![CDATA[in class Configuration ,there is many properties to init.see how hibernate deal with all the properties.it use class Mappings to include all the proterties which had a constructor needed all the proterties.as we know,Configuration deal with the hibernate.cfg.xml and HbmBinder with the *.hbm.xml,but all the properties are stored in Configuration then used to build<br />SessionFactory.How Configuration get the properties analyzed in HbmBinder .The bridge is the Mappings .Configuration <br />had many private property fields,e.g.  classes,collections,tables,imports and so on.when call HbmBinder to work,Configuration first createMappings with its property fields then pass the Mappings  to the HbmBinder .In HbmBinder ,what Mappings   had done is also the  Configuration 's field done.<br /><br />e.g. Configuration  has a field "imports" ,it gone with the Mappings as a parameter in Mappings constructor to HbmBinder .<br />in HbmBinder class ,HbmBinder use Mappings .addImport() to operate it and at the same time point to the Configuration's "imports".This is correlative to java's clone mechanism.<br /><br />this gives us a exquisite way to deal with more properties among several class.<br /><br /><br /><br /><img src ="http://www.blogjava.net/RR00/aggbug/71802.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-25 17:39 <a href="http://www.blogjava.net/RR00/articles/71802.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用for 代替 while</title><link>http://www.blogjava.net/RR00/articles/70328.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 18 Sep 2006 08:08:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/70328.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/70328.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/70328.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/70328.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/70328.html</trackback:ping><description><![CDATA[好像effective java上面也这么说！<br />in my opinion, when useing  "for" clause ,the local variable's range are restricted in the "for" clause not in all method range.this is better than in "while" clause.<img src ="http://www.blogjava.net/RR00/aggbug/70328.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-18 16:08 <a href="http://www.blogjava.net/RR00/articles/70328.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>迭代中的wile和if要注意</title><link>http://www.blogjava.net/RR00/articles/70306.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Mon, 18 Sep 2006 07:22:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/70306.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/70306.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/70306.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/70306.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/70306.html</trackback:ping><description><![CDATA[看了一个下午，终于找到bug，迭代要注意是使用while还是if语句。<img src ="http://www.blogjava.net/RR00/aggbug/70306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-18 15:22 <a href="http://www.blogjava.net/RR00/articles/70306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Using the java.lang.reflect.Modifier Class</title><link>http://www.blogjava.net/RR00/articles/69394.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Wed, 13 Sep 2006 08:10:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/69394.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/69394.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/69394.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/69394.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/69394.html</trackback:ping><description><![CDATA[
		<div class="" style="MARGIN-TOP: 10px; FONT-WEIGHT: bold; FONT-SIZE: 18px; MARGIN-LEFT: 10px; COLOR: #ff6600; FONT-FAMILY: Arial, Helvetica, Sans-Serif">Using the java.lang.reflect.Modifier Class</div>
		<div class="" style="PADDING-RIGHT: 10px; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; PADDING-TOP: 10px">You know that you can extract constructors, methods, and variables from a Java class file. Generally, when you use <pre><code>
	Field fields[] = c.getDeclaredFields( );
</code></pre>where <span class="pf">c</span> is initialized using <pre><code>
	Class c = Class.forName(className);
</code></pre>and print the <span class="pf">fields</span> array, you get all the elements declared in the class. 
<p>However, suppose you want to restrict the output to all fields <i>other</i> than those declared as private. In this case, you would use the following code, where the <span class="pf">Modifier</span> class is a part of the <span class="pf">java.lang.reflect</span> package: </p><pre><code>
if(!Modifier.isPrivate(fields[i].getModifiers( )){
	System.out.println(fields[i]+"\n");
}
</code></pre>Using <span class="pf">Modifier.isPrivate()</span> ensures that the private variables are not printed. 
<p>The same can be used for methods as well. 
</p><p></p>author: MS Sridhar</div>
<img src ="http://www.blogjava.net/RR00/aggbug/69394.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-13 16:10 <a href="http://www.blogjava.net/RR00/articles/69394.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ListIterator in hibernate</title><link>http://www.blogjava.net/RR00/articles/69392.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Wed, 13 Sep 2006 08:01:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/69392.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/69392.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/69392.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/69392.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/69392.html</trackback:ping><description><![CDATA[ArrayList orderedFromElements = new ArrayList();<br />  ListIterator liter = fromClause.getFromElements().listIterator( fromClause.getFromElements().size() );<br />  while ( liter.hasPrevious() ) {<br />            log.debug("add previous");<br />            orderedFromElements.add( liter.previous() );<br />  }<img src ="http://www.blogjava.net/RR00/aggbug/69392.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-09-13 16:01 <a href="http://www.blogjava.net/RR00/articles/69392.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java中ThreadLocal的设计与使用</title><link>http://www.blogjava.net/RR00/articles/64067.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Thu, 17 Aug 2006 02:43:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/64067.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/64067.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/64067.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/64067.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/64067.html</trackback:ping><description><![CDATA[早在Java 1.2推出之时，Java平台中就引入了一个新的支持：java.lang.ThreadLocal，给我们在编写多线程程序时提供了一种新的选择。使用这个工具类可以很简洁地编写出优美的多线程程序，虽然ThreadLocal非常有用，但是似乎现在了解它、使用它的朋友还不多。
<p class="main"><br />　<strong>　一、ThreadLocal是什么</strong></p><p class="main">　　ThreadLocal并非是一个线程的本地实现版本，它并不是一个Thread，而是thread local variable（线程局部变量）。也许把它命名为ThreadLocalVar更加合适。线程局部变量（ThreadLocal）其实的功用非常简单，就是为每一个使用该变量的线程都提供一个变量值的副本，是每一个线程都可以独立地改变自己的副本，而不会和其它线程的副本冲突。从线程的角度看，就好像每一个线程都完全拥有该变量。线程局部变量并不是Java的新发明，在其它的一些语言编译器实现（如IBM XL FORTRAN）中，它在语言的层次提供了直接的支持。因为Java中没有提供在语言层次的直接支持，而是提供了一个ThreadLocal的类来提供支持，所以，在Java中编写线程局部变量的代码相对比较笨拙，这也许是线程局部变量没有在Java中得到很好的普及的一个原因吧。</p><p class="main"><br /><br />　　<strong>二、ThreadLocal的设计</strong></p><p class="main">　　首先看看ThreadLocal的接口：</p><p class="main">Object get() ;</p><p class="main">// 返回当前线程的线程局部变量副本 protected Object initialValue(); // 返回该线程局部变量的当前线程的初始值</p><p class="main">void set(Object value); </p><p class="main">// 设置当前线程的线程局部变量副本的值</p><p class="main"><br />　　ThreadLocal有3个方法，其中值得注意的是initialValue()，该方法是一个protected的方法，显然是为了子类重写而特意实现的。该方法返回当前线程在该线程局部变量的初始值，这个方法是一个延迟调用方法，在一个线程第1次调用get()或者set(Object)时才执行，并且仅执行1次。ThreadLocal中的确实实现直接返回一个null：</p><p class="main">protected Object initialValue() { return null; }</p><p class="main">　　ThreadLocal是如何做到为每一个线程维护变量的副本的呢？其实实现的思路很简单，在ThreadLocal类中有一个Map，用于存储每一个线程的变量的副本。比如下面的示例实现：</p><p class="main">public class ThreadLocal</p><p class="main">{</p><p class="main">private Map values = Collections.synchronizedMap(new HashMap());</p><p class="main">public Object get()</p><p class="main">{</p><p class="main">Thread curThread = Thread.currentThread();</p><p class="main">Object o = values.get(curThread);</p><p class="main">if (o == null &amp;&amp; !values.containsKey(curThread))</p><p class="main">{</p><p class="main">o = initialValue();</p><p class="main">values.put(curThread, o);</p><p class="main">}</p><p class="main">return o;</p><p class="main">}</p><p class="main"><br />public void set(Object newValue)</p><p class="main">{</p><p class="main">values.put(Thread.currentThread(), newValue);</p><p class="main">}</p><p class="main"><br />public Object initialValue()</p><p class="main">{</p><p class="main">return null;</p><p class="main">}</p><p class="main">}</p><p class="main"><br />当然，这并不是一个工业强度的实现，但JDK中的ThreadLocal的实现总体思路也类似于此。</p><p class="main"><br />　　<strong>三、ThreadLocal的使用</strong></p><p class="main">　　如果希望线程局部变量初始化其它值，那么需要自己实现ThreadLocal的子类并重写该方法，通常使用一个内部匿名类对ThreadLocal进行子类化，比如下面的例子，SerialNum类为每一个类分配一个序号：</p><p class="main"><br />public class SerialNum</p><p class="main">{</p><p class="main">// The next serial number to be assigned</p><p class="main"><br />private static int nextSerialNum = 0;</p><p class="main">private static ThreadLocal serialNum = new ThreadLocal()</p><p class="main">{</p><p class="main">protected synchronized Object initialValue()</p><p class="main">{</p><p class="main">return new Integer(nextSerialNum++);</p><p class="main">}</p><p class="main">};</p><p class="main"><br />public static int get()</p><p class="main">{</p><p class="main">return ((Integer) (serialNum.get())).intValue();</p><p class="main">}</p><p class="main">}</p><p class="main"><br />　　SerialNum类的使用将非常地简单，因为get()方法是static的，所以在需要获取当前线程的序号时，简单地调用：</p><p class="main">int serial = SerialNum.get();</p><p class="main">即可。</p><p class="main"><br />　　在线程是活动的并且ThreadLocal对象是可访问的时，该线程就持有一个到该线程局部变量副本的隐含引用，当该线程运行结束后，该线程拥有的所以线程局部变量的副本都将失效，并等待垃圾收集器收集。<br /><br /><strong>四、ThreadLocal与其它同步机制的比较</strong></p><p class="main">　　ThreadLocal和其它同步机制相比有什么优势呢？ThreadLocal和其它所有的同步机制都是为了解决多线程中的对同一变量的访问冲突，在普通的同步机制中，是通过对象加锁来实现多个线程对同一变量的安全访问的。这时该变量是多个线程共享的，使用这种同步机制需要很细致地分析在什么时候对变量进行读写，什么时候需要锁定某个对象，什么时候释放该对象的锁等等很多。所有这些都是因为多个线程共享了资源造成的。ThreadLocal就从另一个角度来解决多线程的并发访问，ThreadLocal会为每一个线程维护一个和该线程绑定的变量的副本，从而隔离了多个线程的数据，每一个线程都拥有自己的变量副本，从而也就没有必要对该变量进行同步了。ThreadLocal提供了线程安全的共享对象，在编写多线程代码时，可以把不安全的整个变量封装进ThreadLocal，或者把该对象的特定于线程的状态封装进ThreadLocal。</p><p class="main"><br />　　由于ThreadLocal中可以持有任何类型的对象，所以使用ThreadLocal get当前线程的值是需要进行强制类型转换。但随着新的Java版本（1.5）将模版的引入，新的支持模版参数的ThreadLocal类将从中受益。也可以减少强制类型转换，并将一些错误检查提前到了编译期，将一定程度地简化ThreadLocal的使用。</p><p class="main"><br />　　<strong>五、总结</strong></p><p class="main">　　当然ThreadLocal并不能替代同步机制，两者面向的问题领域不同。同步机制是为了同步多个线程对相同资源的并发访问，是为了多个线程之间进行通信的有效方式；而ThreadLocal是隔离多个线程的数据共享，从根本上就不在多个线程之间共享资源（变量），这样当然不需要对多个线程进行同步了。所以，如果你需要进行多个线程之间进行通信，则使用同步机制；如果需要隔离多个线程之间的共享冲突，可以使用ThreadLocal，这将极大地简化你的程序，使程序更加易读、简洁。</p><img src ="http://www.blogjava.net/RR00/aggbug/64067.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2006-08-17 10:43 <a href="http://www.blogjava.net/RR00/articles/64067.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java MD5算法返回数字型字串</title><link>http://www.blogjava.net/RR00/articles/18191.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Fri, 04 Nov 2005 12:20:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/18191.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/18191.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/18191.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/18191.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/18191.html</trackback:ping><description><![CDATA[&amp;lt;P&amp;gt;常有人问及MD5算法为何有些程序片断返回完全数字型结果而有些返回数字与字母的混合字串。&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;其实两种返回结果只是因为加密结果的不同显示形式，Blog中已经有.Net的实现，在此附加JAVA实现，供参考。&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;JAVA的标准类库理论上功能也很强大，但由于虚拟机/运行时的实现太多，加之版本差异，有些代码在不同环境下运行会出现奇怪的异常结果，尤其以涉及字符集的操作为甚。&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;package com.bee.framework.common;&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;import java.security.MessageDigest;&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;public class MD5Encrypt {&amp;lt;BR&amp;gt;&amp;amp;nbsp; public MD5Encrypt() {&amp;lt;BR&amp;gt;&amp;amp;nbsp; }&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp; private final static String[] hexDigits = {&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; &amp;quot;0&amp;quot;, &amp;quot;1&amp;quot;, &amp;quot;2&amp;quot;, &amp;quot;3&amp;quot;, &amp;quot;4&amp;quot;, &amp;quot;5&amp;quot;, &amp;quot;6&amp;quot;, &amp;quot;7&amp;quot;,&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; &amp;quot;8&amp;quot;, &amp;quot;9&amp;quot;, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;, &amp;quot;e&amp;quot;, &amp;quot;f&amp;quot;};&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp; /**&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp; * 转换字节数组为16进制字串&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp; * @param b 字节数组&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp; * @return 16进制字串&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp; */&amp;lt;BR&amp;gt;&amp;amp;nbsp; public static String byteArrayToString(byte[] b) {&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; StringBuffer resultSb = new StringBuffer();&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; for (int i = 0; i &amp;amp;lt; b.length; i++) {&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; //resultSb.append(byteToHexString(b[i]));//若使用本函数转换则可得到加密结果的16进制表示，即数字字母混合的形式&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; resultSb.append(byteToNumString(b[i]));//使用本函数则返回加密结果的10进制数字字串，即全数字形式&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; }&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; return resultSb.toString();&amp;lt;BR&amp;gt;&amp;amp;nbsp; }&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp; private static String byteToNumString(byte b) {&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; int _b = b;&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; if (_b &amp;amp;lt; 0) {&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; _b = 256 + _b;&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; }&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; return String.valueOf(_b);&amp;lt;BR&amp;gt;&amp;amp;nbsp; }&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp; private static String byteToHexString(byte b) {&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; int n = b;&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; if (n &amp;amp;lt; 0) {&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; n = 256 + n;&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; }&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; int d1 = n / 16;&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; int d2 = n % 16;&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; return hexDigits[d1] + hexDigits[d2];&amp;lt;BR&amp;gt;&amp;amp;nbsp; }&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp; public static String MD5Encode(String origin) {&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; String resultString = null;&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; try {&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; resultString = new String(origin);&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; MessageDigest md = MessageDigest.getInstance(&amp;quot;MD5&amp;quot;);&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; resultString =&amp;lt;BR&amp;gt;byteArrayToString(md.digest(resultString.getBytes()));&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; }&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; catch (Exception ex) {&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; }&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; return resultString;&amp;lt;BR&amp;gt;&amp;amp;nbsp; }&amp;lt;/P&amp;gt;
&amp;lt;P&amp;gt;&amp;amp;nbsp; public static void main(String[] args) {&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; MD5Encrypt md5encrypt = new MD5Encrypt();&amp;lt;BR&amp;gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp; System.out.println(MD5Encode(&amp;quot;10000000&amp;quot;));&amp;lt;BR&amp;gt;&amp;amp;nbsp; }&amp;lt;BR&amp;gt;}&amp;lt;BR&amp;gt;&amp;lt;/P&amp;gt;<img src ="http://www.blogjava.net/RR00/aggbug/18191.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2005-11-04 20:20 <a href="http://www.blogjava.net/RR00/articles/18191.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java中涉及byte、short和char类型的运算操作</title><link>http://www.blogjava.net/RR00/articles/18186.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Fri, 04 Nov 2005 11:56:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/18186.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/18186.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/18186.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/18186.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/18186.html</trackback:ping><description><![CDATA[<TABLE cellSpacing=0 cellPadding=0 width="98%" align=center border=0>
<TBODY>
<TR>
<TD>Java中涉及byte、short和char类型的运算操作首先会把这些值转换为int类型，然后对int类型值进行运算，最后得到int类型的结果。因此，如果把两个byte类型值相加，最后会得到一个int类型的结果。如果需要得到byte类型结果，必须将这个int类型的结果显式转换为byte类型。例如，下面的代码会导致编译失败： 
<P align=left><FONT style="BACKGROUND-COLOR: #f5f5dc">class BadArithmetic {<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp; static byte addOneAndOne() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte a = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte b = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte c = (a + b);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return c;<BR>&nbsp;&nbsp;&nbsp; }<BR>}</FONT></P>
<P align=left></P>
<P align=left>当遇到上述代码时，javac会给出如下提示：</P>
<P align=left><FONT color=#ff0000>type.java:6: possible loss of precision<BR>found&nbsp;&nbsp; : int<BR>required: byte<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte c = (a + b);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ^<BR>1 error<BR></FONT></P>
<P align=left>为了对这种情况进行补救，必须把a + b所获得的int类型结果显式转换为byte类型。代码如下：</P>
<P align=left><FONT style="BACKGROUND-COLOR: #f5f5dc">class GoodArithmetic {<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp; static byte addOneAndOne() {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte a = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte b = 1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte c = (byte)(a + b);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return c;<BR>&nbsp;&nbsp;&nbsp; }<BR>}</FONT></P>
<P align=left>该操作能够通过javac的编译，并产生GoodArithmetic.class文件。<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">char ccc=98;<BR>System.err.println( ccc);//out put is <FONT color=#ff0000>b</FONT></FONT><BR></P></TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/RR00/aggbug/18186.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2005-11-04 19:56 <a href="http://www.blogjava.net/RR00/articles/18186.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>File.createNewFile()</title><link>http://www.blogjava.net/RR00/articles/18021.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Thu, 03 Nov 2005 14:01:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/18021.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/18021.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/18021.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/18021.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/18021.html</trackback:ping><description><![CDATA[String indexPath = "E:\\Dsdfownloadsdftestr";//一个完整的路径 createNewFile()根据它创建文件！<BR>File lockFile = new File(indexPath);<BR>P.p("a"+lockFile.createNewFile()+"");<BR><FONT style="BACKGROUND-COLOR: #ffa500">lockFile.createNewFile()<BR><CODE>true</CODE> if the named file does not exist and was successfully created; <CODE>false</CODE> if the named file already exists</FONT> <BR><BR>lucene里利用这个进行锁定一些什么东西！<img src ="http://www.blogjava.net/RR00/aggbug/18021.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2005-11-03 22:01 <a href="http://www.blogjava.net/RR00/articles/18021.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Generics in J2SE 5.0（翻译）</title><link>http://www.blogjava.net/RR00/articles/10378.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Wed, 17 Aug 2005 13:56:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/10378.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/10378.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/10378.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/10378.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/10378.html</trackback:ping><description><![CDATA[<P>原文：<A href="http://www.onjava.com/pub/a/onjava/2005/07/06/generics.html">http://www.onjava.com/pub/a/onjava/2005/07/06/generics.html</A><BR>作者：<A href="http://www.onjava.com/pub/au/136"><FONT color=#1d58d1>Budi Kurniawan</FONT></A><BR>翻译：<A href="mailto:di_feng_ro@hotmail.com">di_feng_ro@hotmail.com</A><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp; 泛型是J2SE 5.0最重要的特性。他们让你写一个type(类或接口）和创建一个实例通过传递一个或多个引用类型。这个实例受限于只能作用于这些类型。比如，在java 5，java.util.List 已经被泛化。当建立一个list对象时，你通过传递一个java类型建立一个List实例，此list实例只能作用于所传递的类型。这意味着如果你传递一个String ,此List实例只能拥有String对象；如果你传递一个Integer，此实例只能存贮Integer对象。除了创建参数化的类型，你还能创建参数化的函数。<BR>&nbsp;&nbsp; &nbsp; 泛型的第一个好处是编译时的严格类型检查。这是Collections framework最重要的特点。此外,泛型消除了绝大多数的类型转换。在JDK 5.0之前，当你使用Collections framework时，你不得不进行类型转换。<BR>&nbsp;&nbsp;&nbsp;&nbsp; 本文将教你如何操作泛型类型。它的第一部分是“没有泛型的日子”，先让我们回忆老版本JDK的不便。然后，举一些泛型的例子。在讨论完语法以及有界泛型的使用之后，文章最后一章将解释如何写泛型。</P>
<P>&nbsp; </P>
<P><BR><STRONG>&nbsp; 没有泛型的日子</STRONG></P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; 所有的java类都源自java.lang.Object,这意味着所有的JAVA对象能转换成Object。因此，在之前的JDK的版本中，很多Collections framework的函数接受一个Object参数。所以，collections是一个能持有任何对象的多用途工具，但带来了不良的后果。<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;举个简单的例子，在JDK 5.0的之前版本中，类List的函数add接受一个Object参数：<BR><BR>&nbsp;<FONT style="BACKGROUND-COLOR: #f5f5dc">public boolean add(java.lang.Object element)<BR></FONT><BR>&nbsp;所以你能传递任何类型给add。这是故意这么设计的。否则，它只能传递某种特定的对象，这样就会出现各种List类型，如，StringList, EmployeeList, AddressList等。<BR>&nbsp;&nbsp;&nbsp; &nbsp;add通过Object传递能带来好处,现在我们考虑get函数(返回List中的一个元素).如下是JDK 5之前版本的定义：<BR>&nbsp;<BR><FONT style="BACKGROUND-COLOR: #f5f5dc"> public java.lang.Object get(int index)&nbsp;throws IndexOutOfBoundsException</FONT></P>
<P>&nbsp;<BR>get返回一个Object.不幸的事情从此开始了.假如你储存了两个String对象在一个List中:<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">List stringList1 = new ArrayList();<BR>stringList1.add("Java 5");<BR>stringList1.add("with generics");<BR></FONT><BR>当你想从stringList1取得一个元素时,你得到了一个Object.为了操作原来的类型元素,你不得不把它转换String。<BR><BR><FONT style="BACKGROUND-COLOR: #deb887"><FONT style="BACKGROUND-COLOR: #f5f5dc">String s1 = (String) stringList1.get(0);</FONT><BR></FONT><BR>但是,假如你曾经把一个non-String对象加入stringList1中,上面的代码会抛出一个ClassCastException.<BR>&nbsp;&nbsp; 有了泛型,你能创建一个单一用途的List实例.比如,你能创建一个只接受String对象的List实例,另外一个实例只能接受Employee对象.这同样适用于Collections framework中的其他类型.</P>
<P>&nbsp;</P>
<P><STRONG>泛型入门</STRONG></P>
<P><BR>&nbsp;&nbsp; 像一个函数能接受参数一样,一个泛型类也能接受参数.这就是一个泛型类经常被称为一个parameterized type的原因.但是不像函数用()传递参数,泛型类是用&lt;&gt;传递参数的.声明一个泛型类和声明一个普通类没有什么区别,只不过你把泛型的变量放在&lt;&gt;中.<BR>&nbsp;&nbsp; 比如,在JDK 5中,你可以这样声明一个java.util.List :&nbsp; List&lt;E&gt; myList;E 称为type variable.意味着一个变量将被一个类型替代.替代type variable的值将被当作参数或返回类型.对于List接口来说,当一个实例被创建以后,E 将被当作一个add或别的函数的参数.E 也会使get或别的参数的返回值.下面是add和get的定义:<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">boolean add&lt;E o&gt;<BR>E get(int index)</FONT></P>
<P><STRONG>NOTE</STRONG>:一个泛型在声明或例示时允许你传递特定的type variable: E.除此之外,如果E是个类，你可以传递子类；如果E是个接口，你可以传递实现接口的类；<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">-----------------------------译者添加--------------------<BR>&nbsp;List&lt;Number&gt; numberList= new ArrayList&lt;Number&gt;();<BR>&nbsp;&nbsp; numberList.add(2.0);<BR>&nbsp;&nbsp; numberList.add(2);<BR>-----------------------------译者添加--------------------</FONT></P>
<P>如果你传递一个String给一个List，比如：<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">List&lt;String&gt; myList;<BR></FONT><BR>那么mylist的add函数将接受一个String作为他的参数，而get函数将返回一个String.因为返回了一个特定的类型，所以不用类型转化了。</P>
<P><STRONG>NOTE</STRONG>：根据惯例，我们使用一个唯一的大写字目表示一个type variable。为了创建一个泛型类，你需在声明时传递同样的参数列表。比如，你要想创建一个ArrayList来操作String ，你必须把String放在&lt;&gt;<BR>中。如：<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">List&lt;String&gt; myList = new ArrayList&lt;String&gt;();</FONT><BR><BR>再比如，java.util.Map 是这么定义的：<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">public interface Map&lt;K,V&gt;</FONT><BR><BR>K用来声明map密钥(KEY)的类型而V用来表示值(VALUE)的类型。put和values是这么定义的：<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">V put(K key, V value)<BR>Collection&lt;V&gt; values()</FONT></P>
<P><STRONG>NOTE</STRONG>:一个泛型类不准直接的或间接的是java.lang.Throwable的子类。因为异常是在run time抛出的,所以它不可能预言什么类型的异常将在compile time抛出.</P>
<P>列表1的例子将比较List在JDK 1.4 和JDK1.5的不同<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc"></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">package com.brainysoftware.jdk5.app16;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">import java.util.List;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">import java.util.ArrayList;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">public class GenericListTest {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>public static void main(String[] args) {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>// in JDK 1.4<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>List stringList1 = new ArrayList();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>stringList1.add("Java 1.0 - 5.0");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>stringList1.add("without generics");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>// cast to java.lang.String<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>String s1 = (String) stringList1.get(0);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>System.out.println(s1.toUpperCase());<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>// now with generics in JDK 5<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>List&lt;String&gt; stringList2 = new ArrayList&lt;String&gt;();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>stringList2.add("Java 5.0");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>stringList2.add("with generics");<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>// no need for type casting<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>String s2 = stringList2.get(0);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN>System.out.println(s2.toUpperCase());<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt" align=left><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: #003366; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">}</SPAN><SPAN lang=EN-US style="FONT-SIZE: 12pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"><o:p></o:p></SPAN></P>
<P></FONT>在列表1中，stringList2是个泛型类。声明List&lt;String&gt;告诉编译器List的实例能接受一个String对象。当然，在另外的情况中，你能新建能接受各种对象的List实例。注意，当从List实例中返回成员元素时，不需要对象转化，因为他返回的了你想要的类型，也就是String.</P>
<P><STRONG>NOTE</STRONG>:泛型的类型检查(type checking)是在compile time完成的.</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;最让人感兴趣的事情是，一个泛型类型是个类型并且能被当作一个type variable。比如，你想你的List储存lists of Strings,你能通过把List&lt;String&gt;作为他的type variable来声明List。比如：<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">List&lt;List&lt;String&gt;&gt; myListOfListsOfStrings;</FONT><BR><BR>要从myList中的第一个List重新取得String，你可以这么用：<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">String s = myListOfListsOfStrings.get(0).get(0);</FONT></P>
<P>下一个列表中的ListOfListsTest类示范了一个List（命名为listOfLists）接受一个String List作为参数。<BR><FONT style="BACKGROUND-COLOR: #f5f5dc">package com.brainysoftware.jdk5.app16;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">import java.util.ArrayList;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">import java.util.List;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">public class ListOfListsTest {</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp; public static void main(String[] args) {</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; List&lt;String&gt; listOfStrings = new ArrayList&lt;String&gt;();</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; listOfStrings.add("Hello again");</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; List&lt;List&lt;String&gt;&gt; listOfLists = new ArrayList&lt;List&lt;String&gt;&gt;();</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; listOfLists.add(listOfStrings);</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; String s = listOfLists.get(0).get(0);</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; System.out.println(s); // prints "Hello again"</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp; }</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">}</FONT></P>
<P>另外，一个泛型类型接受一个或多个type variable。比如，java.util.Map有两个type variables。第一个定义了密钥（key）的类型，第二个定义了值（value)的类型。下面的例子讲教我们如何使用个一个泛型Map.<BR><FONT style="BACKGROUND-COLOR: #f5f5dc">package com.brainysoftware.jdk5.app16;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">import java.util.HashMap;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">import java.util.Map;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">public class MapTest {</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp; public static void main(String[] args) {</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; map.put("key1", "value1");</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; map.put("key2", "value2");</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp;&nbsp;&nbsp; String value1 = map.get("key1");</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">&nbsp; }</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">}<BR></FONT>在这个例子中，重新得到一个key1代表的String值，我们不需要任何类型转换。</P>
<P>&nbsp;</P>
<P><STRONG>没有参数的情况下使用泛型</STRONG></P>
<P><BR>&nbsp;&nbsp;&nbsp;&nbsp;既然在J2SE 5.0中收集类型已经泛型化，那么，原来的使用这些类型的代码将如何呢？很幸运，他们在JAVA 5中将继续工作，因为你能使用没有参数的泛型类型。比如，你能继续像原来一样使用List接口，正如下面的例子一样。<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">List stringList1 = new ArrayList();<BR>stringList1.add("Java 1.0 - 5.0");<BR>stringList1.add("without generics");<BR>String s1 = (String) stringList1.get(0);</FONT><BR><BR>一个没有任何参数的泛型类型被称为raw type。它意味着这些为JDK1.4或更早的版本而写的代码将继续在java 5中工作。</P>
<P>尽管如此，一个需要注意的事情是，JDK５编译器希望你使用带参数的泛型类型。否则，编译器将提示警告，因为他认为你可能忘了定义type variables。比如，编译上面的代码的时候你会看到下面这些警告，因为第一个List被认为是 raw type。</P>
<P><STRONG>Note: com/brainysoftware/jdk5/app16/GenericListTest.java<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uses unchecked or unsafe operations.<BR>Note: Recompile with -Xlint:unchecked for details.</STRONG></P>
<P><BR>当你使用raw type时，如果你不想看到这些警告，你有几个选择来达到目的：</P>
<P>1.编译时带上参数-source 1.4<BR>2.使用@SupressWarnings("unchecked")注释<BR>3.更新你的代码，使用List&lt;Object&gt;. List&lt;Object&gt;的实例能接受任何类型的对象，就像是一个raw type List。然而，编译器不会发脾气。</P>
<P>&nbsp;</P>
<P><STRONG>使用 ？ 通配符</STRONG></P>
<P>&nbsp;&nbsp; 前面提过，如果你声明了一个List&lt;aType&gt;, 那么这个List对aType起作用，所以你能储存下面这些类型的对象：<BR>1.一个aType的实例<BR>2.它的子类的实例(如果aType是个类)<BR>3.实现aType接口的类实例(如果aType是个接口)</P>
<P>但是，请注意，一个泛型本身是个JAVA类型，就像java.lang.String或java.io.File一样。传递不同的type variable给泛型可以创建不同的JAVA类型。比如，下面例子中list1和list2引用了不同的类型对象。<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">List&lt;Object&gt; list1 = new ArrayList&lt;Object&gt;();<BR>List&lt;String&gt; list2 = new ArrayList&lt;String&gt;();</FONT><BR><BR>list1指向了一个type variables为java.lang.Objects 的List而list2指向了一个type variables为String 的List。所以传递一个List&lt;String&gt;给一个参数为List&lt;Object&gt;的函数将导致compile time错误。下面列表可以说明：</P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">package com.brainysoftware.jdk5.app16;<BR>import java.util.ArrayList;<BR>import java.util.List;<BR>&nbsp;<BR>public class AllowedTypeTest {<BR>&nbsp; public static void doIt(List&lt;Object&gt; l) {<BR>&nbsp; }<BR>&nbsp; public static void main(String[] args) {<BR>&nbsp;&nbsp;&nbsp; List&lt;String&gt; myList = new ArrayList&lt;String&gt;();<BR>&nbsp;&nbsp;&nbsp; // 这里将产生一个错误<BR>&nbsp;&nbsp;&nbsp; doIt(myList);<BR>&nbsp; }<BR>}</FONT><BR>上面的代码无法编译，因为你试图传递一个错误的类型给函数doIt。doIt的参数是List&lt;Object&gt;二你传递的参数是List&lt;String&gt;。</P>
<P>可以使用 ？ 通配符解决这个难题。List&lt;?&gt; 意味着一个对任何对象起作用的List。所以，doIt可以改为：<BR>&nbsp;<BR><FONT style="BACKGROUND-COLOR: #f5f5dc">public static void doIt(List&lt;?&gt; l) {}</FONT></P>
<P>&nbsp;&nbsp;&nbsp; 在某些情况下你会考虑使用 ? 通配符。比如，你有一个printList函数，这个函数打印一个List的所有成员，你想让这个函数对任何类型的List起作用时。否则，你只能累死累活的写很多printList的重载函数。下面的列表引用了使用 ? 通配符的printList函数。</P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">package com.brainysoftware.jdk5.app16;<BR>import java.util.ArrayList;<BR>import java.util.List;<BR>&nbsp;<BR>public class WildCardTest {<BR>&nbsp;<BR>&nbsp; public static void printList(List&lt;?&gt; list) {<BR>&nbsp;&nbsp;&nbsp; for (Object element : list) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(element);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }<BR>&nbsp; public static void main(String[] args) {<BR>&nbsp;&nbsp;&nbsp; List&lt;String&gt; list1 = new ArrayList&lt;String&gt;();<BR>&nbsp;&nbsp;&nbsp; list1.add("Hello");<BR>&nbsp;&nbsp;&nbsp; list1.add("World");<BR>&nbsp;&nbsp;&nbsp; printList(list1);<BR>&nbsp;<BR>&nbsp;&nbsp;&nbsp; List&lt;Integer&gt; list2 = new ArrayList&lt;Integer&gt;();<BR>&nbsp;&nbsp;&nbsp; list2.add(100);<BR>&nbsp;&nbsp;&nbsp; list2.add(200);<BR>&nbsp;&nbsp;&nbsp; printList(list2);<BR>&nbsp; }<BR>}<BR></FONT>这些代码说明了在printList函数中，List&lt;?&gt;表示各种类型的List对象。然而，请注意，在声明的时候使用 ? 通配符是不合法的，像这样：<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">List&lt;?&gt; myList = new ArrayList&lt;?&gt;(); // 不合法</FONT><BR><BR>如果你想创建一个接收任何类型对象的List，你可以使用Object作为type variable，就像这样：<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">List&lt;Object&gt; myList = new ArrayList&lt;Object&gt;();</FONT></P>
<P><BR>在函数中使用界限通配符</P>
<P>在之前的章节中，你学会了通过传递不同的type variables来创建不同JAVA类型的泛型，但并不考虑type variables之间的继承关系。在很多情况下，你想一个函数有不同的List参数。比如，你有一个函数getAverage，他返回了一个List中成员的平均值。然而，如果你把List&lt;Number&gt;作为etAverage的参数，你就没法传递List&lt;Integer&gt; 或List&lt;Double&gt;参数，因为List&lt;Number&gt;和List&lt;Integer&gt; 和List&lt;Double&gt;不是同样的类型。你能使用raw type 或使用通配符，但这样无法在compile time进行安全类型检查，因为你能传递任何任何类型的List，比如List&lt;String&gt;的实例。你可以使用List&lt;Number&gt;作为参数，但是你就只能传递List&lt;Number&gt;给函数。但这样就使你的函数功能减少，因为你可能更多的时候要操作List&lt;Integer&gt;或List&lt;Long&gt;，而不是List&lt;Number&gt;。</P>
<P>J2SE5.0增加了一个规则来解决了这种约束，这个规则就是允许你定义一个上界(upper bound) type variable.在这种方式中，你能传递一个类型或它的子类。在上面getAverage函数的例子中，你能传递一个List&lt;Number&gt;或它的子类的实例，比如List&lt;Integer&gt; or List&lt;Float&gt;。</P>
<P>使用上界规则的语法这么定义的：GenericType&lt;? extends upperBoundType&gt;. 比如，对getAverage函数的参数，你可以这么写List&lt;? extends Number&gt;. 下面例子说明了如何使用这种规则。</P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc">package com.brainysoftware.jdk5.app16;<BR>import java.util.ArrayList;<BR>import java.util.List;<BR>public class BoundedWildcardTest {<BR>&nbsp; public static double getAverage(List&lt;? extends Number&gt; numberList)<BR>&nbsp; {<BR>&nbsp;&nbsp;&nbsp; double total = 0.0;<BR>&nbsp;&nbsp;&nbsp; for (Number number : numberList)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; total += number.doubleValue();<BR>&nbsp;&nbsp;&nbsp; return total/numberList.size();<BR>&nbsp; }<BR>&nbsp;<BR>&nbsp; public static void main(String[] args) {<BR>&nbsp;&nbsp;&nbsp; List&lt;Integer&gt; integerList = new ArrayList&lt;Integer&gt;();<BR>&nbsp;&nbsp;&nbsp; integerList.add(3);<BR>&nbsp;&nbsp;&nbsp; integerList.add(30);<BR>&nbsp;&nbsp;&nbsp; integerList.add(300);<BR>&nbsp;&nbsp;&nbsp; System.out.println(getAverage(integerList)); // 111.0<BR>&nbsp;&nbsp;&nbsp; List&lt;Double&gt; doubleList = new ArrayList&lt;Double&gt;();<BR>&nbsp;&nbsp;&nbsp; doubleList.add(3.0);<BR>&nbsp;&nbsp;&nbsp; doubleList.add(33.0);<BR>&nbsp;&nbsp;&nbsp; System.out.println(getAverage(doubleList)); // 18.0<BR>&nbsp; }<BR>}<BR></FONT>由于有了上界规则，上面例子中的getAverage函数允许你传递一个List&lt;Number&gt; 或一个type variable是任何java.lang.Number子类的List。</P>
<P><BR>下界规则</P>
<P>关键字extends定义了一个type variable的上界。通过使用super关键字，我们可以定义一个type variable的下界，尽管通用的情况不多。比如，如果一个函数的参数是List&lt;? super Integer&gt;，那么意味着你可以传递一个List&lt;Integer&gt;的实例或者任何java.lang.Integer的超类(superclass)。</P>
<P><BR>创建泛型类</P>
<P><BR>前面的章节主要说明了如何使使用泛型类，特别是Collections framework中的类。现在我们开始学习如何写自己的泛型类。</P>
<P>基本上，除了声明一些你想要使用的type variables外，一个泛型类和别的类没有什么区别。这些type variables位于类型后面的&lt;&gt;中。比如，下面的Point就是个泛型类。一个Point对象代表了一个系统中的点，它有横坐标和纵坐标。通过使Point泛型化，你能定义一个点实例的精确程度。比如，一个Point对象需要非常精确，你能把Double作为type variable。否则，Integer 就够了。</P>
<P><FONT style="BACKGROUND-COLOR: #f5f5dc" color=#000000>package com.brainysoftware.jdk5.app16;<BR>public class Point&lt;T&gt; {<BR>&nbsp; T x;<BR>&nbsp; T y;<BR>&nbsp; public Point(T x, T y) {<BR>&nbsp;&nbsp;&nbsp; this.x = x;<BR>&nbsp;&nbsp;&nbsp; this.y = y;<BR>&nbsp; }<BR>&nbsp; public T getX() {<BR>&nbsp;&nbsp;&nbsp; return x;<BR>&nbsp; }<BR>&nbsp; public T getY() {<BR>&nbsp;&nbsp;&nbsp; return y;<BR>&nbsp; }<BR>&nbsp; public void setX(T x) {<BR>&nbsp;&nbsp;&nbsp; this.x = x;<BR>&nbsp; }<BR>&nbsp; public void setY(T y) {<BR>&nbsp;&nbsp;&nbsp; this.y = y;<BR>&nbsp; }<BR>}<BR><BR></FONT>在这个例子中，T是Point的type variable 。T是getX和getY的返回值类型，也是setX和setY的参数类型。此外，构造函数结合两个T参数。</P>
<P>使用point类就像使用别的类一样。比如，下面的例子创建了两个Point对象：ponint1和point2。前者把Integer作为type variable，而后者把Double作为type variable。<BR><BR><FONT style="BACKGROUND-COLOR: #f5f5dc">Point&lt;Integer&gt; point1 = new Point&lt;Integer&gt;(4, 2);<BR>point1.setX(7);<BR>Point&lt;Double&gt; point2 = new Point&lt;Double&gt;(1.3, 2.6);<BR>point2.setX(109.91);</FONT></P>
<P>总结</P>
<P>泛型使代码在compile time有了更严格的类型检查。特别是在Collections framework中，泛型有两个作用。第一，他们增加了对收集类型(collection types）在compile time的类型检查，所以收集类所能持有的类型对传递给它的参数类型起了限制作用。比如你创建了一个持有strings的java.util.List实例，那么他就将不能接受Integers或别的类型。其次，当你从一个收集中取得一个元素时，泛型消除了类型转换的必要。</P>
<P>泛型能够在没有type variable的情况下使用，比如，作为raw types。这些措施让Java 5之前的代码能够运行在JRE 5中。但是，对新的应用程序，你最好不要使用raw types，因为以后Java可能不支持他们。</P>
<P><BR>你已经知道通过传递不同类型的type variable给泛型类可以产生不同的JAVA类型。就是说List&lt;String&gt;和List&lt;Object&gt;的类型是不同的。尽管String是java.lang.Object。但是传递一个List&lt;String&gt;给一个参数是List&lt;Object&gt;的函数会参数会产生编译错误（compile error）。函数能用 ? 通配符使其接受任何类型的参数。List&lt;?&gt; 意味着任何类型的对象。</P>
<P>最后，你已经看到了写一个泛型类和别的一般JAVA类没有什么区别。你只需要在类型名称后面的&lt;&gt;中声明一系列的type variables就行了。这些type variables就是返回值类型或者参数类型。根据惯例，一个<BR>type variable用一个大写字母表示。</P><img src ="http://www.blogjava.net/RR00/aggbug/10378.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2005-08-17 21:56 <a href="http://www.blogjava.net/RR00/articles/10378.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Generics in J2SE 5.0</title><link>http://www.blogjava.net/RR00/articles/10046.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Sat, 13 Aug 2005 10:31:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/10046.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/10046.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/10046.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/10046.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/10046.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: by Budi Kurniawan&nbsp;&nbsp;&nbsp; Generics are the most important feature in J2SE 5.0. They enable you to write a type (a class or an interface) and create an instance of it by passing a reference...&nbsp;&nbsp;<a href='http://www.blogjava.net/RR00/articles/10046.html'>阅读全文</a><img src ="http://www.blogjava.net/RR00/aggbug/10046.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2005-08-13 18:31 <a href="http://www.blogjava.net/RR00/articles/10046.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>read properties from file</title><link>http://www.blogjava.net/RR00/articles/9862.html</link><dc:creator>R.Zeus</dc:creator><author>R.Zeus</author><pubDate>Thu, 11 Aug 2005 17:04:00 GMT</pubDate><guid>http://www.blogjava.net/RR00/articles/9862.html</guid><wfw:comment>http://www.blogjava.net/RR00/comments/9862.html</wfw:comment><comments>http://www.blogjava.net/RR00/articles/9862.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/RR00/comments/commentRss/9862.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/RR00/services/trackbacks/9862.html</trackback:ping><description><![CDATA[<P><FONT style="BACKGROUND-COLOR: #ffc0cb"><FONT style="BACKGROUND-COLOR: #ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;before taday,I always read properties from file use by useing classes&nbsp;defined ,It is good luck for me to find the existent class to use when I read source of&nbsp; log4j<BR></FONT><BR>import java.io.IOException;<BR>import java.lang.reflect.InvocationTargetException;<BR>import java.lang.reflect.Method;<BR>import java.net.MalformedURLException;<BR>import java.net.URL;<BR>import java.util.Properties;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">public class test {</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;//static Logger logger = Logger.getLogger(test.class);</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;private static ClassLoader getTCL() throws IllegalAccessException,<BR>&nbsp;&nbsp;&nbsp;InvocationTargetException {</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;&nbsp;// Are we running on a JDK 1.2 or later system?<BR>&nbsp;&nbsp;Method method = null;<BR>&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;method = Thread.class.getMethod("getContextClassLoader", null);<BR>&nbsp;&nbsp;} catch (NoSuchMethodException e) {<BR>&nbsp;&nbsp;&nbsp;// We are running on JDK 1.1<BR>&nbsp;&nbsp;&nbsp;return null;<BR>&nbsp;&nbsp;}</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;&nbsp;return (ClassLoader) method.invoke(Thread.currentThread(), null);<BR>&nbsp;}</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;static public URL getResource(String resource) {<BR>&nbsp;&nbsp;ClassLoader classLoader = null;<BR>&nbsp;&nbsp;URL url = null;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;classLoader = getTCL();<BR>&nbsp;&nbsp;} catch (IllegalAccessException e) {<BR>&nbsp;&nbsp;&nbsp;// TODO 自动生成 catch 块<BR>&nbsp;&nbsp;&nbsp;e.printStackTrace();<BR>&nbsp;&nbsp;} catch (InvocationTargetException e) {<BR>&nbsp;&nbsp;&nbsp;// TODO 自动生成 catch 块<BR>&nbsp;&nbsp;&nbsp;e.printStackTrace();<BR>&nbsp;&nbsp;}</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;&nbsp;url = classLoader.getResource(resource);</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;&nbsp;return url;</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;}</FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffc0cb">&nbsp;public static void main(String argv[]) {<BR>&nbsp;&nbsp;//&nbsp;&nbsp;BasicConfigurator.configure();<BR>&nbsp;&nbsp;//&nbsp;&nbsp;logger.debug("Hello world.");<BR>&nbsp;&nbsp;//&nbsp;&nbsp;logger.info("What a beatiful day.");<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp;Properties props = new Properties();<BR>&nbsp;&nbsp;java.net.URL configURL;<BR>&nbsp;&nbsp;try {<BR>&nbsp;&nbsp;&nbsp;configURL = getResource("b.txt");<BR>&nbsp;&nbsp;&nbsp;props.load(configURL.openStream());<BR>&nbsp;&nbsp;&nbsp;String value = props.getProperty("a");<BR>&nbsp;&nbsp;&nbsp;System.out.print(value);<BR>&nbsp;&nbsp;} catch (MalformedURLException e) {<BR>&nbsp;&nbsp;&nbsp;// TODO 自动生成 catch 块<BR>&nbsp;&nbsp;&nbsp;e.printStackTrace();<BR>&nbsp;&nbsp;} catch (IOException e1) {<BR>&nbsp;&nbsp;&nbsp;// TODO 自动生成 catch 块<BR>&nbsp;&nbsp;&nbsp;e1.printStackTrace();<BR>&nbsp;&nbsp;}<BR>&nbsp;}<BR>}<BR><BR><FONT style="BACKGROUND-COLOR: #ffffff">in properties file,# is remark</FONT><BR></FONT></P><img src ="http://www.blogjava.net/RR00/aggbug/9862.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/RR00/" target="_blank">R.Zeus</a> 2005-08-12 01:04 <a href="http://www.blogjava.net/RR00/articles/9862.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>