﻿<?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-Collus-文章分类-JAVA Code Share</title><link>http://www.blogjava.net/Collus/category/44985.html</link><description>惜秦怀古</description><language>zh-cn</language><lastBuildDate>Thu, 13 May 2010 03:09:01 GMT</lastBuildDate><pubDate>Thu, 13 May 2010 03:09:01 GMT</pubDate><ttl>60</ttl><item><title>StingUtil</title><link>http://www.blogjava.net/Collus/articles/320748.html</link><dc:creator>良帅</dc:creator><author>良帅</author><pubDate>Wed, 12 May 2010 14:04:00 GMT</pubDate><guid>http://www.blogjava.net/Collus/articles/320748.html</guid><wfw:comment>http://www.blogjava.net/Collus/comments/320748.html</wfw:comment><comments>http://www.blogjava.net/Collus/articles/320748.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Collus/comments/commentRss/320748.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Collus/services/trackbacks/320748.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: JAVA StringUitl												Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->				import				 java.io.IOException;				import				 java.io...&nbsp;&nbsp;<a href='http://www.blogjava.net/Collus/articles/320748.html'>阅读全文</a><img src ="http://www.blogjava.net/Collus/aggbug/320748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Collus/" target="_blank">良帅</a> 2010-05-12 22:04 <a href="http://www.blogjava.net/Collus/articles/320748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>FileUtil</title><link>http://www.blogjava.net/Collus/articles/320747.html</link><dc:creator>良帅</dc:creator><author>良帅</author><pubDate>Wed, 12 May 2010 14:03:00 GMT</pubDate><guid>http://www.blogjava.net/Collus/articles/320747.html</guid><wfw:comment>http://www.blogjava.net/Collus/comments/320747.html</wfw:comment><comments>http://www.blogjava.net/Collus/articles/320747.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Collus/comments/commentRss/320747.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Collus/services/trackbacks/320747.html</trackback:ping><description><![CDATA[
		<b>
				<font color="#0000ff">JAVA FileUtil<br /><br /></font>
		</b>
		<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;">
				<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
				<span style="color: rgb(0, 0, 255);">import</span>
				<span style="color: rgb(0, 0, 0);"> java.io.</span>
				<span style="color: rgb(0, 0, 0);">*</span>
				<span style="color: rgb(0, 0, 0);">;<br /><br /></span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />* FileUtil. Simple file operation class.<br />*<br />* </span>
				<span style="color: rgb(128, 128, 128);">@author</span>
				<span style="color: rgb(0, 128, 0);"> BeanSoft<br />*<br /></span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">class</span>
				<span style="color: rgb(0, 0, 0);"> FileUtil {<br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * The buffer.<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">protected</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);"> buf[] </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);">[</span>
				<span style="color: rgb(0, 0, 0);">1024</span>
				<span style="color: rgb(0, 0, 0);">];<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Read content from local file. FIXME How to judge UTF-8 and GBK, the<br />     * correct code should be: FileReader fr = new FileReader(new<br />     * InputStreamReader(fileName, "ENCODING")); Might let the user select the<br />     * encoding would be a better idea. While reading UTF-8 files, the content<br />     * is bad when saved out.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name to read<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> Exception<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String readFileAsString(String fileName) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> Exception {<br />        String content </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> String(readFileBinary(fileName));<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> content;<br />    }<br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 读取文件并返回为给定字符集的字符串.<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> encoding<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> Exception<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String readFileAsString(String fileName, String encoding) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> Exception {<br />        String content </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> String(readFileBinary(fileName), encoding);<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> content;<br />    }<br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 读取文件并返回为给定字符集的字符串.<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> encoding<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> Exception<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String readFileAsString(InputStream in) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> Exception {<br />        String content </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> String(readFileBinary(in));<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> content;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Read content from local file to binary byte array.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name to read<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> Exception<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);">[] readFileBinary(String fileName) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> Exception {<br />        FileInputStream fin </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileInputStream(fileName);<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> readFileBinary(fin);<br />    }<br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 从输入流读取数据为二进制字节数组.<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> streamIn<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);">[] readFileBinary(InputStream streamIn) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        BufferedInputStream in </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> BufferedInputStream(streamIn);<br /><br />        ByteArrayOutputStream out </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> ByteArrayOutputStream(</span>
				<span style="color: rgb(0, 0, 0);">10240</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> len;<br />        </span>
				<span style="color: rgb(0, 0, 255);">while</span>
				<span style="color: rgb(0, 0, 0);"> ((len </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> in.read(buf)) </span>
				<span style="color: rgb(0, 0, 0);">&gt;=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">)<br />            out.write(buf, </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">, len);<br />        in.close();<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> out.toByteArray();       <br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Write string content to local file.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name will write to<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> content<br />     *            String text<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> true if success<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> writeFileString(String fileName, String content)<br />            </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        FileWriter fout </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileWriter(fileName);<br />        fout.write(content);<br />        fout.close();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Write string content to local file using given character encoding.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name will write to<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> content<br />     *            String text<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> encoding<br />     *            the encoding<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> true if success<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> writeFileString(String fileName, String content,<br />            String encoding) </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        OutputStreamWriter fout </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> OutputStreamWriter(</span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileOutputStream(<br />                fileName), encoding);<br /><br />        fout.write(content);<br />        fout.close();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Write binary byte array to local file.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name will write to<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> content<br />     *            binary byte array<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> true if success<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> writeFileBinary(String fileName, </span>
				<span style="color: rgb(0, 0, 255);">byte</span>
				<span style="color: rgb(0, 0, 0);">[] content)<br />            </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        FileOutputStream fout </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileOutputStream(fileName);<br />        fout.write(content);<br />        fout.close();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 检查文件名是否合法.文件名字不能包含字符\/:*?"&lt;&gt;|<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName文件名,不包含路径<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> boolean is valid file name<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> isValidFileName(String fileName) {<br />        </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> isValid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />        String errChar </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">\\/:*?\</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">&lt;&gt;|</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">; //</span>
				<span style="color: rgb(0, 0, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">        </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (fileName </span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">null</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">||</span>
				<span style="color: rgb(0, 0, 0);"> fileName.length() </span>
				<span style="color: rgb(0, 0, 0);">==</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">) {<br />            isValid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">false</span>
				<span style="color: rgb(0, 0, 0);">;<br />        } </span>
				<span style="color: rgb(0, 0, 255);">else</span>
				<span style="color: rgb(0, 0, 0);"> {<br />            </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> (</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> i </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">; i </span>
				<span style="color: rgb(0, 0, 0);">&lt;</span>
				<span style="color: rgb(0, 0, 0);"> errChar.length(); i</span>
				<span style="color: rgb(0, 0, 0);">++</span>
				<span style="color: rgb(0, 0, 0);">) {<br />                </span>
				<span style="color: rgb(0, 0, 255);">if</span>
				<span style="color: rgb(0, 0, 0);"> (fileName.indexOf(errChar.charAt(i)) </span>
				<span style="color: rgb(0, 0, 0);">!=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">-</span>
				<span style="color: rgb(0, 0, 0);">1</span>
				<span style="color: rgb(0, 0, 0);">) {<br />                    isValid </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">false</span>
				<span style="color: rgb(0, 0, 0);">;<br />                    </span>
				<span style="color: rgb(0, 0, 255);">break</span>
				<span style="color: rgb(0, 0, 0);">;<br />                }<br />            }<br />        }<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> isValid;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * 把非法文件名转换为合法文件名.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String replaceInvalidFileChars(String fileName) {<br />        StringBuffer out </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> StringBuffer();<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">for</span>
				<span style="color: rgb(0, 0, 0);"> (</span>
				<span style="color: rgb(0, 0, 255);">int</span>
				<span style="color: rgb(0, 0, 0);"> i </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">0</span>
				<span style="color: rgb(0, 0, 0);">; i </span>
				<span style="color: rgb(0, 0, 0);">&lt;</span>
				<span style="color: rgb(0, 0, 0);"> fileName.length(); i</span>
				<span style="color: rgb(0, 0, 0);">++</span>
				<span style="color: rgb(0, 0, 0);">) {<br />            </span>
				<span style="color: rgb(0, 0, 255);">char</span>
				<span style="color: rgb(0, 0, 0);"> ch </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> fileName.charAt(i);<br />            </span>
				<span style="color: rgb(0, 128, 0);">//</span>
				<span style="color: rgb(0, 128, 0);"> Replace invlid chars: \\/:*?\"&lt;&gt;|</span>
				<span style="color: rgb(0, 128, 0);">
						<br />
				</span>
				<span style="color: rgb(0, 0, 0);">            </span>
				<span style="color: rgb(0, 0, 255);">switch</span>
				<span style="color: rgb(0, 0, 0);"> (ch) {<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">\\</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">/</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">*</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">?</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">\"</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">&lt;</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">&gt;</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />            </span>
				<span style="color: rgb(0, 0, 255);">case</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">|</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">:<br />                out.append(</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">_</span>
				<span style="color: rgb(0, 0, 0);">'</span>
				<span style="color: rgb(0, 0, 0);">);<br />                </span>
				<span style="color: rgb(0, 0, 255);">break</span>
				<span style="color: rgb(0, 0, 0);">;<br />            </span>
				<span style="color: rgb(0, 0, 255);">default</span>
				<span style="color: rgb(0, 0, 0);">:<br />                out.append(ch);<br />            }<br />        }<br /><br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> out.toString();<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Convert a given file name to a URL(URI) string.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            the file to parse<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> - URL string<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> String filePathToURL(String fileName) {<br />        String fileUrl </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> File(fileName).toURI().toString();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> fileUrl;<br />    }<br /><br />    </span>
				<span style="color: rgb(0, 128, 0);">/**</span>
				<span style="color: rgb(0, 128, 0);">
						<br />     * Write string content to local file.<br />     *<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> fileName -<br />     *            local file name will write to<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@param</span>
				<span style="color: rgb(0, 128, 0);"> content<br />     *            String text<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@return</span>
				<span style="color: rgb(0, 128, 0);"> true if success<br />     * </span>
				<span style="color: rgb(128, 128, 128);">@throws</span>
				<span style="color: rgb(0, 128, 0);"> IOException<br />     </span>
				<span style="color: rgb(0, 128, 0);">*/</span>
				<span style="color: rgb(0, 0, 0);">
						<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">boolean</span>
				<span style="color: rgb(0, 0, 0);"> appendFileString(String fileName, String content)<br />            </span>
				<span style="color: rgb(0, 0, 255);">throws</span>
				<span style="color: rgb(0, 0, 0);"> IOException {<br />        OutputStreamWriter fout </span>
				<span style="color: rgb(0, 0, 0);">=</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> OutputStreamWriter(</span>
				<span style="color: rgb(0, 0, 255);">new</span>
				<span style="color: rgb(0, 0, 0);"> FileOutputStream(<br />                fileName, </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">), </span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">GBK</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">);<br /><br />        fout.write(content);<br />        fout.close();<br />        </span>
				<span style="color: rgb(0, 0, 255);">return</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">true</span>
				<span style="color: rgb(0, 0, 0);">;<br />    }<br />    </span>
				<span style="color: rgb(0, 0, 255);">public</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">static</span>
				<span style="color: rgb(0, 0, 0);"> </span>
				<span style="color: rgb(0, 0, 255);">void</span>
				<span style="color: rgb(0, 0, 0);"> main(String[] args) {<br />        System.out.println(replaceInvalidFileChars(</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">http://www.abc.com/</span>
				<span style="color: rgb(0, 0, 0);">"</span>
				<span style="color: rgb(0, 0, 0);">));<br />    }<br />}</span>
		</div>
		<br />
		<br />
		<br />
<img src ="http://www.blogjava.net/Collus/aggbug/320747.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Collus/" target="_blank">良帅</a> 2010-05-12 22:03 <a href="http://www.blogjava.net/Collus/articles/320747.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>