<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAVA

    人生若只如初見,何事秋風(fēng)悲畫扇。

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      50 隨筆 :: 25 文章 :: 157 評(píng)論 :: 0 Trackbacks

    經(jīng)常對(duì)一些文件,文件夾操作,自己寫一個(gè)不是難事.但有個(gè)可以撿的應(yīng)該也不是壞事.
    將destFileName解壓到mExtractToDir??目錄:

    public ? void ?extract(destFileName,mExtractToDir?)?
    ?
    {?
    ??String?fileName?
    = ? new ?String();?
    ??String?destFileName?
    = ? new ?String();?
    ??
    ??
    // do?our?own?buffering;?reuse?the?same?
    ??buffer? byte []?buffer? = ? new ? byte [ 16384 ];?
    ??
    try ?
    ??
    {?
    ???ZipFile?archive?
    = ? new ?ZipFile(?mZipFile?);?
    ???
    for ?(?Enumeration?e? = ?archive.entries();?e.hasMoreElements();?)?
    ???
    {?
    ????
    // get?the?next?entry?in?the?archive?
    ????ZipEntry?entry? = ?(ZipEntry)e.nextElement();?
    ????
    ????
    if ?(? ! ?entry.isDirectory()?)?
    ????
    {?
    ?????fileName?
    = ?entry.getName();?
    ?????fileName?
    = ?fileName.replace( ' / ' ,?File.separatorChar);?
    ?????
    ?????destFileName?
    = ?mExtractToDir? + ?fileName;?
    ?????File?destFile?
    = ? new ?File(destFileName);
    ?????
    ?????
    // create?the?destination?path,?if?needed?????
    ?????String?parent? = ?destFile.getParent();?
    ?????
    if ?(?parent? != ? null ?)?
    ?????
    {?
    ??????File?parentFile?
    = ? new ?File(parent);?
    ??????
    if ?(? ! ?parentFile.exists()?)?
    ??????
    {?
    ???????
    // create?the?chain?of?subdirs?to?the?file?
    ???????parentFile.mkdirs();?
    ??????}
    ?
    ?????}
    ?
    ?????
    ?????
    // get?a?stream?of?the?archive?entry's?bytes?
    ?????InputStream?in? = ?archive.getInputStream(entry);?
    ?????
    ?????
    // open?a?stream?to?the?destination?
    ?????file?OutputStream?out? = ? new ?FileOutputStream(destFileName);?
    ?????
    ?????
    // Repeat?reading?into?buffer?and?writing?buffer?to?file,?
    ?????
    // until?done.?Count?will?always?be?#?bytes?read,?until? // EOF?when?it?is?-1.?
    ????? int ?count;?
    ?????
    while ?(?(count? = ?in.read(buffer))? != ? - 1 ?)?
    ?????
    {?
    ??????out.write(buffer,?
    0 ,?count?);?
    ?????}
    ?in.close();?out.close();?
    ?????}
    ?
    ????}
    ?
    ???}
    ?
    ??
    catch (?ZipException?ze?)?
    ??
    {?
    ???ze.printStackTrace();?
    ??}
    ?
    ??
    catch ?(?NullPointerException?npe?)?
    ??
    {?
    ???npe.printStackTrace();?
    ??}
    ?
    ??
    catch ?(?IOException?ioe?)?
    ??
    {?
    ???ioe.printStackTrace();?
    ??}
    ?
    ??
    catch ?(?SecurityException?se?)
    ??
    {?
    ???se.printStackTrace();?
    ??}
    ?
    ?}

    ?

    將某一文件夾的內(nèi)容清空:

    public ? void ?cleanImportDirectory(String?iPath)?
    ?
    {?
    ??
    try ?
    ??
    {?
    ???File?theFile?
    = ? new ?File(iPath);?
    ???File?allFiles[]?
    = ?theFile.listFiles();?
    ???
    ???
    for ?( int ?i? = ? 0 ;?i? < ?allFiles.length;?i ++ )?
    ???
    {?
    ????
    if (allFiles[i].isDirectory())?
    ????
    {?
    ?????cleanImportDirectory(allFiles[i].toString());?
    ?????allFiles[i].delete();?
    ????}
    ?
    ????
    else
    ????
    {?
    ?????allFiles[i].delete();?
    ????}
    ?
    ???}
    ?
    ??}
    ?
    ??
    catch ?(NullPointerException?npe)?
    ??
    {?
    ???mLogger.severe(iPath?
    + ? " ?did?not?exist?and?was?not?cleaned!! " );?
    ??}
    ?
    ?}
    ?

    ?

    將某一文件夾的內(nèi)容移到指定文件夾:

    private ? void ?copyCourse(String?iInFilePath,?String?iOutFilePath)? throws ?IOException?
    ?
    {?
    ??
    try ?
    ??
    {?
    ???String?inDirName?
    = ?iInFilePath;?
    ???inDirName.replace(
    ' / ' ,?java.io.File.separatorChar);?
    ???
    ???File?tempFile?
    = ? new ?File(inDirName);?
    ???File[]?fileNames?
    = ?tempFile.listFiles();?
    ???
    ???String?outDirName?
    = ?iOutFilePath;?
    ???outDirName?
    = ?outDirName.replace( ' / ' ,?java.io.File.separatorChar);?
    ???File?tempDir?
    = ? new ?File(outDirName);?
    ???tempDir.mkdirs();?
    ???
    for ?( int ?i? = ? 0 ;?i? < ?fileNames.length;?i ++ )?
    ???
    {?
    ????String?tempString?
    = ?outDirName? + ?java.io.File.separatorChar? + ?fileNames[i].getName();?
    ????
    if (fileNames[i].isDirectory())?
    ????
    {?
    ?????File?dirToCreate?
    = ? new ?File(tempString);?
    ?????dirToCreate.mkdirs();?
    ?????copyCourse(fileNames[i].getAbsolutePath(),?tempString);?
    ????}

    ????
    else ?
    ????
    {?
    ?????BufferedInputStream?in?
    = ? new ?BufferedInputStream( new ?FileInputStream(fileNames[i]));?
    ?????BufferedOutputStream?out?
    = ? new ?BufferedOutputStream( new ?FileOutputStream(tempString));?
    ?????
    int ?c;?
    ?????
    while ((c? = ?in.read())? != ? - 1 )?
    ?????
    {?
    ??????out.write(c);?
    ?????}

    ?????
    ?????in.close();?
    ?????out.close();?
    ????}
    ?
    ???}
    ?
    ??}
    ?
    ??
    catch ?(IOException?IOE)?
    ??
    {?
    ????IOE.printStackTrace();?
    ??}
    ?

    我暫時(shí)只碰到了這三個(gè)...

    posted on 2006-03-30 17:46 Jkallen 閱讀(1302) 評(píng)論(1)  編輯  收藏 所屬分類: JEE學(xué)習(xí)

    評(píng)論

    # re: 文件夾,壓縮包操作 2006-04-28 10:06 小蔥卷餅
    大哥,有 壓縮包最加方法嗎?  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 啊v在线免费观看| 精品香蕉在线观看免费| 免费中文字幕在线| 国产亚洲女在线线精品| 又黄又爽的视频免费看| 国产亚洲精彩视频| 亚洲国产精品一区二区九九| 美女被免费网站视频在线| 亚洲国产成人久久综合一区77| 免费精品国自产拍在线播放| 亚洲综合无码精品一区二区三区 | 日韩成全视频观看免费观看高清| 亚洲av成人综合网| 国外成人免费高清激情视频| 日本亚洲中午字幕乱码| 精品国产人成亚洲区| 黄页免费在线观看| 亚洲成A∨人片在线观看无码| 免费av欧美国产在钱| 人人狠狠综合久久亚洲| 亚洲中文无韩国r级电影| 免费在线看污视频| 亚洲五月丁香综合视频| 国产伦精品一区二区三区免费下载 | 久久亚洲国产精品| 国产卡一卡二卡三免费入口| 亚洲乱色伦图片区小说| 亚洲视频在线一区二区| 69视频在线观看高清免费| 亚洲色大成网站www| 日韩一卡2卡3卡4卡新区亚洲 | 亚洲国产高清在线精品一区| 免费看美女被靠到爽的视频| 国产免费播放一区二区| 亚洲最大中文字幕| 俄罗斯极品美女毛片免费播放| 免费黄网站在线看| 亚洲精品中文字幕无码A片老| 亚洲综合av永久无码精品一区二区 | 国产成人亚洲精品电影| 亚洲日本中文字幕|