锘??xml version="1.0" encoding="utf-8" standalone="yes"?>aa亚洲一区一区三区,日韩一区二区三区在线播放,热久久一区二区http://www.aygfsteel.com/Collus/category/44985.html鎯滅Е鎬鍙?/description>zh-cnThu, 13 May 2010 03:09:01 GMTThu, 13 May 2010 03:09:01 GMT60StingUtilhttp://www.aygfsteel.com/Collus/articles/320748.html鑹竻鑹竻Wed, 12 May 2010 14:04:00 GMThttp://www.aygfsteel.com/Collus/articles/320748.htmlhttp://www.aygfsteel.com/Collus/comments/320748.htmlhttp://www.aygfsteel.com/Collus/articles/320748.html#Feedback0http://www.aygfsteel.com/Collus/comments/commentRss/320748.htmlhttp://www.aygfsteel.com/Collus/services/trackbacks/320748.html import 聽java.io.IOException; import 聽java.io...  闃呰鍏ㄦ枃

鑹竻 2010-05-12 22:04 鍙戣〃璇勮
]]>
FileUtilhttp://www.aygfsteel.com/Collus/articles/320747.html鑹竻鑹竻Wed, 12 May 2010 14:03:00 GMThttp://www.aygfsteel.com/Collus/articles/320747.htmlhttp://www.aygfsteel.com/Collus/comments/320747.htmlhttp://www.aygfsteel.com/Collus/articles/320747.html#Feedback0http://www.aygfsteel.com/Collus/comments/commentRss/320747.htmlhttp://www.aygfsteel.com/Collus/services/trackbacks/320747.html JAVA FileUtil

import 聽java.io. * ;

/**
*聽FileUtil.聽Simple聽file聽operation聽class.
*
*聽
@author 聽BeanSoft
*
*/
public class 聽FileUtil聽{
聽聽聽聽
/**
聽聽聽聽聽*聽The聽buffer.
聽聽聽聽聽
*/
聽聽聽聽
protected static byte 聽buf[]聽 = new byte [ 1024 ];

聽聽聽聽
/**
聽聽聽聽聽*聽Read聽content聽from聽local聽file.聽FIXME聽How聽to聽judge聽UTF-8聽and聽GBK,聽the
聽聽聽聽聽*聽correct聽code聽should聽be:聽FileReader聽fr聽=聽new聽FileReader(new
聽聽聽聽聽*聽InputStreamReader(fileName,聽"ENCODING"));聽Might聽let聽the聽user聽select聽the
聽聽聽聽聽*聽encoding聽would聽be聽a聽better聽idea.聽While聽reading聽UTF-8聽files,聽the聽content
聽聽聽聽聽*聽is聽bad聽when聽saved聽out.
聽聽聽聽聽*
聽聽聽聽聽*聽
@param 聽fileName聽-
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽local聽file聽name聽to聽read
聽聽聽聽聽*聽
@return
聽聽聽聽聽*聽
@throws 聽Exception
聽聽聽聽聽
*/
聽聽聽聽
public static 聽String聽readFileAsString(String聽fileName)聽 throws 聽Exception聽{
聽聽聽聽聽聽聽聽String聽content聽
= new 聽String(readFileBinary(fileName));

聽聽聽聽聽聽聽聽
return 聽content;
聽聽聽聽}
聽聽聽聽
/**
聽聽聽聽聽*聽璇誨彇鏂囦歡騫惰繑鍥炰負緇欏畾瀛楃闆嗙殑瀛楃涓?
聽聽聽聽聽*聽
@param 聽fileName
聽聽聽聽聽*聽
@param 聽encoding
聽聽聽聽聽*聽
@return
聽聽聽聽聽*聽
@throws 聽Exception
聽聽聽聽聽
*/
聽聽聽聽
public static 聽String聽readFileAsString(String聽fileName,聽String聽encoding)聽 throws 聽Exception聽{
聽聽聽聽聽聽聽聽String聽content聽
= new 聽String(readFileBinary(fileName),聽encoding);

聽聽聽聽聽聽聽聽
return 聽content;
聽聽聽聽}
聽聽聽聽
/**
聽聽聽聽聽*聽璇誨彇鏂囦歡騫惰繑鍥炰負緇欏畾瀛楃闆嗙殑瀛楃涓?
聽聽聽聽聽*聽
@param 聽fileName
聽聽聽聽聽*聽
@param 聽encoding
聽聽聽聽聽*聽
@return
聽聽聽聽聽*聽
@throws 聽Exception
聽聽聽聽聽
*/
聽聽聽聽
public static 聽String聽readFileAsString(InputStream聽in)聽 throws 聽Exception聽{
聽聽聽聽聽聽聽聽String聽content聽
= new 聽String(readFileBinary(in));

聽聽聽聽聽聽聽聽
return 聽content;
聽聽聽聽}

聽聽聽聽
/**
聽聽聽聽聽*聽Read聽content聽from聽local聽file聽to聽binary聽byte聽array.
聽聽聽聽聽*
聽聽聽聽聽*聽
@param 聽fileName聽-
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽local聽file聽name聽to聽read
聽聽聽聽聽*聽
@return
聽聽聽聽聽*聽
@throws 聽Exception
聽聽聽聽聽
*/
聽聽聽聽
public static byte []聽readFileBinary(String聽fileName)聽 throws 聽Exception聽{
聽聽聽聽聽聽聽聽FileInputStream聽fin聽
= new 聽FileInputStream(fileName);
聽聽聽聽聽聽聽聽
return 聽readFileBinary(fin);
聽聽聽聽}
聽聽聽聽
/**
聽聽聽聽聽*聽浠庤緭鍏ユ祦璇誨彇鏁版嵁涓轟簩榪涘埗瀛楄妭鏁扮粍.
聽聽聽聽聽*聽
@param 聽streamIn
聽聽聽聽聽*聽
@return
聽聽聽聽聽*聽
@throws 聽IOException
聽聽聽聽聽
*/
聽聽聽聽
public static byte []聽readFileBinary(InputStream聽streamIn)聽 throws 聽IOException聽{
聽聽聽聽聽聽聽聽BufferedInputStream聽in聽
= new 聽BufferedInputStream(streamIn);

聽聽聽聽聽聽聽聽ByteArrayOutputStream聽out聽
= new 聽ByteArrayOutputStream( 10240 );

聽聽聽聽聽聽聽聽
int 聽len;
聽聽聽聽聽聽聽聽
while 聽((len聽 = 聽in.read(buf))聽 >= 0 )
聽聽聽聽聽聽聽聽聽聽聽聽out.write(buf,聽
0 ,聽len);
聽聽聽聽聽聽聽聽in.close();

聽聽聽聽聽聽聽聽
return 聽out.toByteArray();聽聽聽聽聽聽聽
聽聽聽聽}

聽聽聽聽
/**
聽聽聽聽聽*聽Write聽string聽content聽to聽local聽file.
聽聽聽聽聽*
聽聽聽聽聽*聽
@param 聽fileName聽-
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽local聽file聽name聽will聽write聽to
聽聽聽聽聽*聽
@param 聽content
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽String聽text
聽聽聽聽聽*聽
@return 聽true聽if聽success
聽聽聽聽聽*聽
@throws 聽IOException
聽聽聽聽聽
*/
聽聽聽聽
public static boolean 聽writeFileString(String聽fileName,聽String聽content)
聽聽聽聽聽聽聽聽聽聽聽聽
throws 聽IOException聽{
聽聽聽聽聽聽聽聽FileWriter聽fout聽
= new 聽FileWriter(fileName);
聽聽聽聽聽聽聽聽fout.write(content);
聽聽聽聽聽聽聽聽fout.close();
聽聽聽聽聽聽聽聽
return true ;
聽聽聽聽}

聽聽聽聽
/**
聽聽聽聽聽*聽Write聽string聽content聽to聽local聽file聽using聽given聽character聽encoding.
聽聽聽聽聽*
聽聽聽聽聽*聽
@param 聽fileName聽-
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽local聽file聽name聽will聽write聽to
聽聽聽聽聽*聽
@param 聽content
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽String聽text
聽聽聽聽聽*聽
@param 聽encoding
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽the聽encoding
聽聽聽聽聽*聽
@return 聽true聽if聽success
聽聽聽聽聽*聽
@throws 聽IOException
聽聽聽聽聽
*/
聽聽聽聽
public static boolean 聽writeFileString(String聽fileName,聽String聽content,
聽聽聽聽聽聽聽聽聽聽聽聽String聽encoding)聽
throws 聽IOException聽{
聽聽聽聽聽聽聽聽OutputStreamWriter聽fout聽
= new 聽OutputStreamWriter( new 聽FileOutputStream(
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽fileName),聽encoding);

聽聽聽聽聽聽聽聽fout.write(content);
聽聽聽聽聽聽聽聽fout.close();
聽聽聽聽聽聽聽聽
return true ;
聽聽聽聽}

聽聽聽聽
/**
聽聽聽聽聽*聽Write聽binary聽byte聽array聽to聽local聽file.
聽聽聽聽聽*
聽聽聽聽聽*聽
@param 聽fileName聽-
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽local聽file聽name聽will聽write聽to
聽聽聽聽聽*聽
@param 聽content
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽binary聽byte聽array
聽聽聽聽聽*聽
@return 聽true聽if聽success
聽聽聽聽聽*聽
@throws 聽IOException
聽聽聽聽聽
*/
聽聽聽聽
public static boolean 聽writeFileBinary(String聽fileName,聽 byte []聽content)
聽聽聽聽聽聽聽聽聽聽聽聽
throws 聽IOException聽{
聽聽聽聽聽聽聽聽FileOutputStream聽fout聽
= new 聽FileOutputStream(fileName);
聽聽聽聽聽聽聽聽fout.write(content);
聽聽聽聽聽聽聽聽fout.close();
聽聽聽聽聽聽聽聽
return true ;
聽聽聽聽}

聽聽聽聽
/**
聽聽聽聽聽*聽媯鏌ユ枃浠跺悕鏄惁鍚堟硶.鏂囦歡鍚嶅瓧涓嶈兘鍖呭惈瀛楃\/:*?"<>|
聽聽聽聽聽*
聽聽聽聽聽*聽
@param 聽fileName鏂囦歡鍚?涓嶅寘鍚礬寰?br />聽聽聽聽聽*聽 @return 聽boolean聽is聽valid聽file聽name
聽聽聽聽聽
*/
聽聽聽聽
public static boolean 聽isValidFileName(String聽fileName)聽{
聽聽聽聽聽聽聽聽
boolean 聽isValid聽 = true ;
聽聽聽聽聽聽聽聽String聽errChar聽
= " \\/:*?\ " <>| " ;聽//
聽聽聽聽聽聽聽聽 if 聽(fileName聽 == null || 聽fileName.length()聽 == 0 )聽{
聽聽聽聽聽聽聽聽聽聽聽聽isValid聽
= false ;
聽聽聽聽聽聽聽聽}聽
else 聽{
聽聽聽聽聽聽聽聽聽聽聽聽
for 聽( int 聽i聽 = 0 ;聽i聽 < 聽errChar.length();聽i ++ )聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if 聽(fileName.indexOf(errChar.charAt(i))聽 != - 1 )聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽isValid聽
= false ;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
break ;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
return 聽isValid;
聽聽聽聽}

聽聽聽聽
/**
聽聽聽聽聽*聽鎶婇潪娉曟枃浠跺悕杞崲涓哄悎娉曟枃浠跺悕.
聽聽聽聽聽*
聽聽聽聽聽*聽
@param 聽fileName
聽聽聽聽聽*聽
@return
聽聽聽聽聽
*/
聽聽聽聽
public static 聽String聽replaceInvalidFileChars(String聽fileName)聽{
聽聽聽聽聽聽聽聽StringBuffer聽out聽
= new 聽StringBuffer();

聽聽聽聽聽聽聽聽
for 聽( int 聽i聽 = 0 ;聽i聽 < 聽fileName.length();聽i ++ )聽{
聽聽聽聽聽聽聽聽聽聽聽聽
char 聽ch聽 = 聽fileName.charAt(i);
聽聽聽聽聽聽聽聽聽聽聽聽
// 聽Replace聽invlid聽chars:聽\\/:*?\"<>|
聽聽聽聽聽聽聽聽聽聽聽聽 switch 聽(ch)聽{
聽聽聽聽聽聽聽聽聽聽聽聽
case ' \\ ' :
聽聽聽聽聽聽聽聽聽聽聽聽
case ' / ' :
聽聽聽聽聽聽聽聽聽聽聽聽
case ' : ' :
聽聽聽聽聽聽聽聽聽聽聽聽
case ' * ' :
聽聽聽聽聽聽聽聽聽聽聽聽
case ' ? ' :
聽聽聽聽聽聽聽聽聽聽聽聽
case ' \" ' :
聽聽聽聽聽聽聽聽聽聽聽聽
case ' < ' :
聽聽聽聽聽聽聽聽聽聽聽聽
case ' > ' :
聽聽聽聽聽聽聽聽聽聽聽聽
case ' | ' :
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽out.append(
' _ ' );
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
break ;
聽聽聽聽聽聽聽聽聽聽聽聽
default :
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽out.append(ch);
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}

聽聽聽聽聽聽聽聽
return 聽out.toString();
聽聽聽聽}

聽聽聽聽
/**
聽聽聽聽聽*聽Convert聽a聽given聽file聽name聽to聽a聽URL(URI)聽string.
聽聽聽聽聽*
聽聽聽聽聽*聽
@param 聽fileName聽-
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽the聽file聽to聽parse
聽聽聽聽聽*聽
@return 聽-聽URL聽string
聽聽聽聽聽
*/
聽聽聽聽
public static 聽String聽filePathToURL(String聽fileName)聽{
聽聽聽聽聽聽聽聽String聽fileUrl聽
= new 聽File(fileName).toURI().toString();
聽聽聽聽聽聽聽聽
return 聽fileUrl;
聽聽聽聽}

聽聽聽聽
/**
聽聽聽聽聽*聽Write聽string聽content聽to聽local聽file.
聽聽聽聽聽*
聽聽聽聽聽*聽
@param 聽fileName聽-
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽local聽file聽name聽will聽write聽to
聽聽聽聽聽*聽
@param 聽content
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽String聽text
聽聽聽聽聽*聽
@return 聽true聽if聽success
聽聽聽聽聽*聽
@throws 聽IOException
聽聽聽聽聽
*/
聽聽聽聽
public static boolean 聽appendFileString(String聽fileName,聽String聽content)
聽聽聽聽聽聽聽聽聽聽聽聽
throws 聽IOException聽{
聽聽聽聽聽聽聽聽OutputStreamWriter聽fout聽
= new 聽OutputStreamWriter( new 聽FileOutputStream(
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽fileName,聽
true ),聽 " GBK " );

聽聽聽聽聽聽聽聽fout.write(content);
聽聽聽聽聽聽聽聽fout.close();
聽聽聽聽聽聽聽聽
return true ;
聽聽聽聽}
聽聽聽聽
public static void 聽main(String[]聽args)聽{
聽聽聽聽聽聽聽聽System.out.println(replaceInvalidFileChars(
" http://www.abc.com/ " ));
聽聽聽聽}
}





鑹竻 2010-05-12 22:03 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 凤台县| 南澳县| 闵行区| 新丰县| 九江县| 昭觉县| 小金县| 高碑店市| 乐至县| 安岳县| 安龙县| 习水县| 金塔县| 乌苏市| 昌都县| 钟祥市| 新建县| 台前县| 凌海市| 阿坝县| 永州市| 娄烦县| 天峨县| 东丰县| 甘谷县| 肇源县| 沙河市| 龙岩市| 腾冲县| 龙游县| 金川县| 临潭县| 舞钢市| 赤水市| 新源县| 仲巴县| 榕江县| 惠水县| 扶风县| 梁河县| 洞口县|