<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)論
      

    主站蜘蛛池模板: 中国国语毛片免费观看视频| 亚洲一级二级三级不卡| 国产在线观看免费不卡| 99热在线精品免费全部my| www.免费在线观看| 黄+色+性+人免费| 99久久这里只精品国产免费| 1000部拍拍拍18勿入免费视频软件| 美女内射无套日韩免费播放| 免费国产黄网站在线观看可以下载| 久久久久国产精品免费网站| 一区二区三区在线免费看| 99久久精品免费精品国产| 91在线老王精品免费播放| 色播精品免费小视频| 一二三四在线观看免费高清中文在线观看| 国产香蕉九九久久精品免费| 成人免费一区二区无码视频| 色播在线永久免费视频| 免费jjzz在线播放国产| 亚洲人成人网站在线观看| 国产亚洲综合色就色| 亚洲情a成黄在线观看动漫尤物| 亚洲人成电影亚洲人成9999网| 亚洲国产美女在线观看| 亚洲第一男人天堂| 国产精品亚洲一区二区在线观看| 麻豆高清免费国产一区| aⅴ在线免费观看| 免费无码黄动漫在线观看| 亚洲成AV人网址| 国产亚洲综合久久系列| 亚洲综合网美国十次| 亚洲色偷偷偷综合网| 特级一级毛片免费看| a级成人毛片免费图片| 1000部夫妻午夜免费 | 亚洲国产精品无码久久久| 亚洲国产日韩视频观看| 免费人成大片在线观看播放电影 | 亚洲成人黄色在线|