﻿<?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-轮上飞-随笔分类-&lt;b&gt; CODE DEMO - JAVA  &lt;/b&gt;</title><link>http://www.blogjava.net/zhyiwww/category/12118.html</link><description>zhyiwww</description><language>zh-cn</language><lastBuildDate>Wed, 31 Oct 2007 11:54:04 GMT</lastBuildDate><pubDate>Wed, 31 Oct 2007 11:54:04 GMT</pubDate><ttl>60</ttl><item><title>实现和 jdk\bin\native2ascii.exe 同样的功能(转载)</title><link>http://www.blogjava.net/zhyiwww/archive/2006/07/12/57780.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Wed, 12 Jul 2006 04:40:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/07/12/57780.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/57780.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/07/12/57780.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/57780.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/57780.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 实现和 jdk\bin\native2ascii.exe 同样的功能&nbsp;&nbsp;<a href='http://www.blogjava.net/zhyiwww/archive/2006/07/12/57780.html'>阅读全文</a><img src ="http://www.blogjava.net/zhyiwww/aggbug/57780.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-07-12 12:40 <a href="http://www.blogjava.net/zhyiwww/archive/2006/07/12/57780.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[26] Getting the Size of the Java Memory Heap</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52437.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 05:10:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52437.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52437.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52437.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52437.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52437.html</trackback:ping><description><![CDATA[The heap is the area in memory in which objects are created.<br />                <br />                        <br />// Get current size of heap in bytes<br />    long heapSize = Runtime.getRuntime().totalMemory();<br />    <br />    // Get maximum size of heap in bytes. The heap cannot grow beyond this size.<br />    // Any attempt will result in an OutOfMemoryException.<br />    long heapMaxSize = Runtime.getRuntime().maxMemory();<br />    <br />    // Get amount of free memory within the heap in bytes. This size will increase<br />    // after garbage collection and decrease as new objects are created.<br />    long heapFreeSize = Runtime.getRuntime().freeMemory();<br />                <br />                <br /><pre></pre><img src ="http://www.blogjava.net/zhyiwww/aggbug/52437.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 13:10 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52437.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[25] Computing Elapsed Time</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52435.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 05:07:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52435.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52435.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52435.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52435.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52435.html</trackback:ping><description><![CDATA[
		<h3>
				<font size="5">    // Get current time<br />    long start = System.currentTimeMillis();<br />    <br />    // Do something ...<br />    <br />    // Get elapsed time in milliseconds<br />    long elapsedTimeMillis = System.currentTimeMillis()-start;<br />    <br />    // Get elapsed time in seconds<br />    float elapsedTimeSec = elapsedTimeMillis/1000F;<br />    <br />    // Get elapsed time in minutes<br />    float elapsedTimeMin = elapsedTimeMillis/(60*1000F);<br />    <br />    // Get elapsed time in hours<br />    float elapsedTimeHour = elapsedTimeMillis/(60*60*1000F);<br />    <br />    // Get elapsed time in days<br />    float elapsedTimeDay = elapsedTimeMillis/(24*60*60*1000F);<br /></font>
		</h3>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52435.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 13:07 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52435.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[21] Serializing an Object</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52413.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 04:09:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52413.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52413.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52413.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52413.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52413.html</trackback:ping><description><![CDATA[
		<h3>The object to be serialized must implement <code>java.io.Serializable</code>. This example serializes a <code>javax.swing.JButton</code> object. </h3>
		<p>See also <a class="eglink" href="http://javaalmanac.com/egs/java.io/DeserializeObj.html"><font color="#002c99"><b>e45</b> Deserializing an Object</font></a>. </p>
		<pre>
				<font size="6">
						<strong>Object object = <font color="#0066ff"><i>new javax.swing.JButton("push me")</i></font>;
    
    try {
        // Serialize to a file
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream(<font color="#0066ff"><i>"filename.ser"</i></font>));
        out.writeObject(object);
        out.close();
    
        // Serialize to a byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
        out = new ObjectOutputStream(bos) ;
        out.writeObject(object);
        out.close();
    
        // Get the bytes of the serialized object
        byte[] buf = bos.toByteArray();
    } catch (IOException e) {
    }</strong>
				</font>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52413.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 12:09 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52413.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[21]  Writing UTF-8 Encoded Data</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52412.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 04:08:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52412.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52412.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52412.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52412.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52412.html</trackback:ping><description><![CDATA[
		<h3>
				<font size="6">    try {<br />        Writer out = new BufferedWriter(new OutputStreamWriter(<br />            new FileOutputStream(<font color="#0066ff"><i>"outfilename"</i></font>), "UTF8"));<br />        out.write(<font color="#0066ff"><i>aString</i></font>);<br />        out.close();<br />    } catch (UnsupportedEncodingException e) {<br />    } catch (IOException e) {<br />    }<br /></font>
		</h3>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52412.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 12:08 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52412.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[20] Reading UTF-8 Encoded Data</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52411.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 04:07:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52411.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52411.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52411.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52411.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52411.html</trackback:ping><description><![CDATA[
		<h3>
				<font size="6">    try {<br />        BufferedReader in = new BufferedReader(<br />            new InputStreamReader(new FileInputStream(<font color="#0066ff"><i>"infilename"</i></font>), "UTF8"));<br />        String str = in.readLine();<br />    } catch (UnsupportedEncodingException e) {<br />    } catch (IOException e) {<br />    }<br /></font>
		</h3>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52411.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 12:07 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52411.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[19]  Using a Random Access File</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52410.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 04:06:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52410.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52410.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52410.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52410.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52410.html</trackback:ping><description><![CDATA[
		<h3>    try {<br />        File f = new File(<font color="#0066ff"><i>"filename"</i></font>);<br />        RandomAccessFile raf = new RandomAccessFile(f, "rw");<br />    <br />        // Read a character<br />        char ch = raf.readChar();<br />    <br />        // Seek to end of file<br />        raf.seek(f.length());<br />    <br />        // Append to the end<br />        raf.writeChars(<font color="#0066ff"><i>"aString"</i></font>);<br />        raf.close();<br />    } catch (IOException e) {<br />    }<br /></h3>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52410.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 12:06 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52410.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[17] Reading a File into a Byte Array</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52408.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 04:05:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52408.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52408.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52408.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52408.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52408.html</trackback:ping><description><![CDATA[
		<h3>This example implements a method that reads the entire contents of a file into a byte array.</h3>
		<h3>
				<br />
		</h3>
		<h3> // Returns the contents of the file in a byte array.<br /></h3>
		<pre>
				<font size="2">
						<strong>
								<font color="#000000">    public static byte[] getBytesFromFile(File file) throws IOException {<br />        InputStream is = new FileInputStream(file);<br /><br />        // Get the size of the file<br />        long length = file.length();<br /><br />        // You cannot create an array using a long type.<br />        // It needs to be an int type.<br />        // Before converting to an int type, check<br />        // to ensure that file is not larger than Integer.MAX_VALUE.<br />        if (length &gt; Integer.MAX_VALUE) {<br />            // File is too large<br />        }<br /><br />        // Create the byte array to hold the data<br />        byte[] bytes = new byte[(int)length];<br /><br />        // Read in the bytes<br />        int offset = 0;<br />        int numRead = 0;<br />        while (offset &lt; bytes.length<br />               &amp;&amp; (numRead=is.read(bytes, offset, bytes.length-offset)) &gt;= 0) {<br />            offset += numRead;<br />        }<br /><br />        // Ensure all the bytes have been read in<br />        if (offset &lt; bytes.length) {<br />            throw new IOException("Could not completely read file "+file.getName());<br />        }<br /><br />        // Close the input stream and return bytes<br />        is.close();<br />        return bytes;<br />    }</font>
								<br />
						</strong>
				</font>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52408.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 12:05 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52408.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[14] Reading Text from Standard Input</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52406.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 04:03:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52406.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52406.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52406.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52406.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52406.html</trackback:ping><description><![CDATA[
		<h3>    try {<br />        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));<br />        String str = "";<br />        while (str != null) {<br />            System.out.print(<font color="#0066ff"><i>"&gt; prompt "</i></font>);<br />            str = in.readLine();<br />            <font color="#0066ff"><i>process</i></font>(str);<br />        }<br />    } catch (IOException e) {<br />    }<br /></h3>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52406.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 12:03 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52406.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[12] Getting and Setting the Modification Time of a File or Directory</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52403.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 04:02:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52403.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52403.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52403.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52403.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52403.html</trackback:ping><description><![CDATA[
		<h3>This example gets the last modified time of a file or directory and then sets it to the current time. </h3>
		<pre>
				<font size="3">
						<strong> File file = new File(<font color="#0066ff"><i>"filename"</i></font>);
    
    // Get the last modified time
    long modifiedTime = file.lastModified();
    // 0L is returned if the file does not exist
    
    // Set the last modified time
    long newModifiedTime = <font color="#0066ff"><i>System.currentTimeMillis()</i></font>;
    boolean success = file.setLastModified(newModifiedTime);
    if (!success) {
        // operation failed.
    }</strong>
				</font>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52403.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 12:02 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52403.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[11] Forcing Updates to a File to the Disk</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52402.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 04:01:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52402.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52402.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52402.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52402.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52402.html</trackback:ping><description><![CDATA[
		<h3>In some applications, such as transaction processing, it is necessary to ensure that an update has been made to the disk. <code>FileDescriptor.sync()</code> blocks until all changes to a file are written to disk. </h3>
		<pre>
				<font size="5">
						<strong>    try {
        // Open or create the output file
        FileOutputStream os = new FileOutputStream(<font color="#0066ff"><i>"outfilename"</i></font>);
        FileDescriptor fd = os.getFD();
    
        // Write some data to the stream
        byte[] data = new byte[]{(byte)<font color="#0066ff"><i>0xCA</i></font>, (byte)<font color="#0066ff"><i>0xFE</i></font>, (byte)<font color="#0066ff"><i>0xBA</i></font>, (byte)<font color="#0066ff"><i>0xBE</i></font>};
        os.write(data);
    
        // Flush the data from the streams and writers into system buffers.
        // The data may or may not be written to disk.
        os.flush();
    
        // Block until the system buffers have been written to disk.
        // After this method returns, the data is guaranteed to have
        // been written to disk.
        fd.sync();
    } catch (IOException e) {
    }</strong>
				</font>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52402.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 12:01 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52402.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[10] Creating a Temporary File</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52401.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 04:00:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52401.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52401.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52401.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52401.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52401.html</trackback:ping><description><![CDATA[
		<h3>    try {<br />        // Create temp file.<br />        File temp = File.createTempFile(<font color="#0066ff"><i>"pattern"</i></font>, <font color="#0066ff"><i>".suffix"</i></font>);<br />    <br />        // Delete temp file when program exits.<br />        temp.deleteOnExit();<br />    <br />        // Write to temp file<br />        BufferedWriter out = new BufferedWriter(new FileWriter(temp));<br />        out.write(<font color="#0066ff"><i>"aString"</i></font>);<br />        out.close();<br />    } catch (IOException e) {<br />    }<br /></h3>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52401.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 12:00 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52401.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[9] Copying One File to Another</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52400.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 03:58:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52400.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52400.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52400.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52400.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52400.html</trackback:ping><description><![CDATA[
		<h3>This example uses file streams to copy the contents of one file to another file. See <a class="eglink" href="http://javaalmanac.com/egs/java.nio/File2File.html"><font color="#002c99"><b>e172</b> Copying One File to Another</font></a> for an example that uses file channels. </h3>
		<pre>
				<font color="#000000" size="5">
						<strong>  // Copies src file to dst file.
    // If the dst file does not exist, it is created
    void copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);
    
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) &gt; 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }</strong>
				</font>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52400.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 11:58 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52400.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[8] Determining If Two Filename Paths Refer to the Same File</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52399.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 03:56:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52399.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52399.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52399.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52399.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52399.html</trackback:ping><description><![CDATA[
		<h3>A filename path may include redundant names such as `<code>.</code>' or `<code>..</code>' or symbolic links (on UNIX platforms). <code>File.getCanonicalFile()</code> converts a filename path to a unique canonical form suitable for comparisons. </h3>
		<pre>
				<strong>
						<font size="5">    File file1 = new File(<font color="#0066ff"><i>"./filename"</i></font>);
    File file2 = new File(<font color="#0066ff"><i>"filename"</i></font>);
    
    // Filename paths are not equal
    boolean b = file1.equals(file2);      // false
    
    // Normalize the paths
    try {
        file1 = file1.getCanonicalFile(); // c:\almanac1.4\filename
        file2 = file2.getCanonicalFile(); // c:\almanac1.4\filename
    } catch (IOException e) {
    }
    
    // Filename paths are now equal
    b = file1.equals(file2);              // true</font>
				</strong>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52399.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 11:56 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52399.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[7] Getting an Absolute Filename Path from a Relative Filename Path</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52398.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 03:55:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52398.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52398.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52398.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52398.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52398.html</trackback:ping><description><![CDATA[
		<h3>    File file = new File(<font color="#0066ff"><i>"filename.txt"</i></font>);<br />    file = file.getAbsoluteFile();  // c:\temp\filename.txt<br />    <br />    file = new File(<font color="#0066ff"><i>"dir"+File.separatorChar+"filename.txt"</i></font>);<br />    file = file.getAbsoluteFile();  // c:\temp\dir\filename.txt<br />    <br />    file = new File(<font color="#0066ff"><i>".."+File.separatorChar+"filename.txt"</i></font>);<br />    file = file.getAbsoluteFile();  // c:\temp\..\filename.txt<br />    <br />    // Note that filename.txt does not need to exist<br /></h3>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52398.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 11:55 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52398.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[6] Converting Between a Filename Path and a URL</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52396.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 03:53:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52396.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52396.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52396.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52396.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52396.html</trackback:ping><description><![CDATA[
		<h3>    // Create a file object<br />    File file = new File(<font color="#0066ff"><i>"filename"</i></font>);<br />    <br />    // Convert the file object to a URL<br />    URL url = null;<br />    try {<br />        // The file need not exist. It is made into an absolute path<br />        // by prefixing the current working directory<br />        url = file.toURL();          // file:/d:/almanac1.4/java.io/filename<br />    } catch (MalformedURLException e) {<br />    }<br />    <br />    // Convert the URL to a file object<br />    file = new File(url.getFile());  // d:/almanac1.4/java.io/filename<br />    <br />    // Read the file contents using the URL<br />    try {<br />        // Open an input stream<br />        InputStream is = url.openStream();<br />    <br />        // Read from is<br />    <br />        is.close();<br />    } catch (IOException e) {<br />        // Could not open the file<br />    }<br /></h3>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52396.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 11:53 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52396.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[5] Constructing a Filename Path</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52395.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 03:52:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52395.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52395.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52395.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52395.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52395.html</trackback:ping><description><![CDATA[
		<h3>A <code>File</code> object is used to represent a filename. Creating the <code>File</code> object has no effect on the file system; the filename need not exist nor is it created. </h3>
		<p>
				<font size="5">On Windows, this example creates the path<font color="#000080"><strong><code>\a\b</code></strong></font>. On Unix, the path would be <code><font color="#0000ff"><strong>/a/b</strong></font></code>. </font>
		</p>
		<pre>
				<font size="5">
						<strong>    String path = File.separator + <font color="#0066ff"><i>"a"</i></font> + File.separator + <font color="#0066ff"><i>"b"</i></font>;</strong>
				</font>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52395.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 11:52 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52395.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[4] Traversing the Files and Directories Under a Directory</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52394.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 03:49:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52394.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52394.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52394.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52394.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52394.html</trackback:ping><description><![CDATA[
		<h3>
				<font size="5">This example implements methods that recursively visits all files and directories under a directory. </font>
		</h3>
		<pre>
				<font size="5">
						<strong>// Process all files and directories under dir
    public static void visitAllDirsAndFiles(File dir) {
        <font color="#0066ff">process</font>(dir);
    
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i&lt;children.length; i++) {
                visitAllDirsAndFiles(new File(dir, children[i]));
            }
        }
    }
    
    // Process only directories under dir
    public static void visitAllDirs(File dir) {
        if (dir.isDirectory()) {
            <font color="#0066ff">process</font>(dir);
    
            String[] children = dir.list();
            for (int i=0; i&lt;children.length; i++) {
                visitAllDirs(new File(dir, children[i]));
            }
        }
    }
    
    // Process only files under dir
    public static void visitAllFiles(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i&lt;children.length; i++) {
                visitAllFiles(new File(dir, children[i]));
            }
        } else {
            <font color="#0066ff">process</font>(dir);
        }
    }</strong>
				</font>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52394.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 11:49 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52394.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[3]  Listing the Files or Subdirectories in a Directory</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52392.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 03:45:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52392.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52392.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52392.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52392.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52392.html</trackback:ping><description><![CDATA[
		<h3>
				<font size="5">This example lists the files and subdirectories in a directory. To list all descendant files and subdirectories under a directory, see </font>
				<a class="eglink" href="http://javaalmanac.com/egs/java.io/TraverseTree.html">
						<font size="5">
								<b>e33</b> Traversing the Files and Directories Under a Directory</font>
				</a>
				<font size="5">. </font>
		</h3>
		<pre>
				<font size="5">
						<strong>File dir = new File(<font color="#0066ff"><i>"directoryName"</i></font>);
    
    String[] children = dir.list();
    if (children == null) {
        // Either dir does not exist or is not a directory
    } else {
        for (int i=0; i&lt;children.length; i++) {
            // Get filename of file or directory
            String filename = children[i];
        }
    }
    
    // It is also possible to filter the list of returned files.
    // This example does not return any files that start with `.'.
    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return !name.startsWith(".");
        }
    };
    children = dir.list(filter);
    
    
    // The list of files can also be retrieved as File objects
    File[] files = dir.listFiles();
    
    // This filter only returns directories
    FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory();
        }
    };
    files = dir.listFiles(fileFilter);</strong>
				</font>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52392.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 11:45 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52392.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[2] Getting the Current Working Directory</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52391.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 03:43:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52391.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52391.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52391.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52391.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52391.html</trackback:ping><description><![CDATA[
		<h3>
				<font size="5">
						<strong>
						</strong>
				</font> </h3>
		<h3>
				<font size="5">
						<strong>The working directory is the location in the file system from where the <code>java</code> command was invoked. </strong>
				</font>
		</h3>
		<pre>
				<font color="#000000" size="5">
						<strong>
								<font color="#000080">
										<font color="#000000">
										</font>String curDir = System.getProperty("user.dir");</font>
						</strong>
				</font>
		</pre>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52391.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 11:43 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52391.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[1]   Copying a Directory</title><link>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52389.html</link><dc:creator>zhyiwww</dc:creator><author>zhyiwww</author><pubDate>Tue, 13 Jun 2006 03:37:00 GMT</pubDate><guid>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52389.html</guid><wfw:comment>http://www.blogjava.net/zhyiwww/comments/52389.html</wfw:comment><comments>http://www.blogjava.net/zhyiwww/archive/2006/06/13/52389.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zhyiwww/comments/commentRss/52389.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zhyiwww/services/trackbacks/52389.html</trackback:ping><description><![CDATA[
		<h3>
				<font size="5">    // Copies all files under srcDir to dstDir.<br />    // If dstDir does not exist, it will be created.<br />    public void copyDirectory(File srcDir, File dstDir) throws IOException {<br />        if (srcDir.isDirectory()) {<br />            if (!dstDir.exists()) {<br />                dstDir.mkdir();<br />            }<br />    <br />            String[] children = srcDir.list();<br />            for (int i=0; i&lt;children.length; i++) {<br />                copyDirectory(new File(srcDir, children[i]),<br />                                     new File(dstDir, children[i]));<br />            }<br />        } else {<br />            // This method is implemented in </font>
				<a class="eglink" href="http://javaalmanac.com/egs/java.io/CopyFile.html">
						<font size="5">
								<b>e1071</b> Copying a File</font>
				</a>
				<br />
				<font size="5">            <font color="#0066ff"><i>copyFile</i></font>(srcDir, dstDir);<br />        }<br />    }</font>
				<br />
		</h3>
<img src ="http://www.blogjava.net/zhyiwww/aggbug/52389.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zhyiwww/" target="_blank">zhyiwww</a> 2006-06-13 11:37 <a href="http://www.blogjava.net/zhyiwww/archive/2006/06/13/52389.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>