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

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

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

    JAVA

    人生若只如初見,何事秋風悲畫扇。

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      50 隨筆 :: 25 文章 :: 157 評論 :: 0 Trackbacks

    經常對一些文件,文件夾操作,自己寫一個不是難事.但有個可以撿的應該也不是壞事.
    將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();?
    ??}
    ?
    ?}

    ?

    將某一文件夾的內容清空:

    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!! " );?
    ??}
    ?
    ?}
    ?

    ?

    將某一文件夾的內容移到指定文件夾:

    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();?
    ??}
    ?

    我暫時只碰到了這三個...

    posted on 2006-03-30 17:46 Jkallen 閱讀(1303) 評論(1)  編輯  收藏 所屬分類: JEE學習

    評論

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

    主站蜘蛛池模板: 99久久久国产精品免费牛牛四川 | 亚洲成色www久久网站夜月| 丁香花免费高清视频完整版| 精品多毛少妇人妻AV免费久久 | 日韩中文字幕在线免费观看 | 亚洲嫩模在线观看| 亚洲性日韩精品国产一区二区| 成人免费视频试看120秒| 91麻豆国产免费观看| 青柠影视在线观看免费高清 | 在线观看免费成人| 在线看无码的免费网站| 国产精品美女免费视频观看| 精品国产亚洲AV麻豆| 亚洲综合无码无在线观看| 亚洲国产模特在线播放| 91亚洲va在线天线va天堂va国产| 亚洲真人无码永久在线| 亚洲男人的天堂一区二区| 国产一区视频在线免费观看| 午夜免费不卡毛片完整版| 无码一区二区三区免费视频 | 亚洲三级在线视频| 亚洲乱码日产精品BD在线观看| 亚洲视频精品在线观看| 久久久久亚洲AV无码专区体验| 久久精品国产亚洲| 亚洲国产二区三区久久| 亚洲国产成人精品不卡青青草原| 国产V亚洲V天堂无码| 亚洲av日韩av高潮潮喷无码| 亚洲av无码潮喷在线观看| 亚洲综合成人网在线观看| 亚洲欧洲精品一区二区三区| 亚洲日韩在线视频| 久久精品国产亚洲AV忘忧草18| 国产AV旡码专区亚洲AV苍井空| 成人亚洲国产va天堂| 美女视频黄频a免费大全视频| 在线看亚洲十八禁网站| 一个人看的免费视频www在线高清动漫|