<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 閱讀(1310) 評論(1)  編輯  收藏 所屬分類: JEE學習

    評論

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

    主站蜘蛛池模板: 免费无遮挡无码永久视频| 国产成人综合亚洲一区| 国产精品免费大片| 久久久久亚洲AV无码去区首| 免费在线看v网址| youjizz亚洲| 四虎免费在线观看| 国产精品亚洲专区无码WEB| 免费观看的毛片手机视频| 亚洲欧洲无码AV电影在线观看| 亚洲最新黄色网址| 猫咪免费人成网站在线观看| 亚洲乱码一二三四区麻豆| 国产香蕉九九久久精品免费| 亚洲精品V天堂中文字幕| 国产中文字幕免费观看| 九九免费精品视频在这里| 亚洲中文字幕无码久久2017 | 免费午夜爽爽爽WWW视频十八禁| 亚洲GV天堂GV无码男同| 亚洲国产精品成人久久蜜臀 | 亚洲欧洲中文日韩久久AV乱码| 一级毛片视频免费观看| 亚洲色自偷自拍另类小说| 99久久综合精品免费| ASS亚洲熟妇毛茸茸PICS| 国产大片线上免费看| 中文字幕免费观看视频| 亚洲精品美女视频| 好大好深好猛好爽视频免费| 五月天国产成人AV免费观看| 亚洲AV午夜成人影院老师机影院| 波多野结衣免费在线观看| 国产精品手机在线亚洲| 亚洲av中文无码乱人伦在线咪咕| 日韩版码免费福利视频| 一级特级aaaa毛片免费观看| 亚洲成人福利在线| 亚洲色婷婷综合开心网| 四虎免费影院ww4164h| 四虎影视永久在线精品免费|