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

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

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

    隨筆 - 100  文章 - 50  trackbacks - 0
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(3)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    我收藏的一些文章!

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    寫了個Java操作文件的簡單例子,希望給需要幫助的兄弟提供點兒幫助,同時也希望大家給予批評指正。
    文件操作類,代碼如下:
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import org.apache.log4j.LogManager;
    import org.apache.log4j.Logger;

     

    public class FileTools {
     private static final String SPECIAL_FILE_STYLE_PICTURECACHE = "Thumbs.db";
     public static final Logger logger = LogManager.getLogger(FileTools.class);
     
     public FileTools()
     {
      
     }
     /*
      * @discription:Delete a folder
      * @param:dirPath String
      * @return boolean
      */
     public static boolean deleteDirectory(String dirPath,boolean deteDirRoot)
     {
      File parentFile = new File(dirPath);
      File[] chirenFile = null;
      if(parentFile.exists() && parentFile.isDirectory())
      {
        chirenFile = parentFile.listFiles();
        for (int i = 0; i < chirenFile.length; i++) {
         if(!chirenFile[i].isDirectory())
         {
          chirenFile[i].delete();
         }
         else
         {
          deleteDirectory(chirenFile[i].getAbsolutePath(),true);     
          chirenFile[i].delete();
         }
        }
        if(deteDirRoot)
        {
         parentFile.delete();
        }
        
        return true;  
      }
      else
      {
       FileTools.logger.error("The Floder Is Not Exist!");
       return false;
      }
     }
     
     /*
      * @discription:Copy Folder
      * @param:sourceFilePath String,targetFilePath String
      * @return boolean
      */
     public static boolean copyDirectory(String sourceFilePath,String targetFilePath) throws IOException
     {
      File sourceFile = new File(sourceFilePath);
      File targetFile = new File(targetFilePath);
      
      if(!targetFile.exists())
      {
       targetFile.mkdir();
      }
      
      File[] sourceChirenFile = null;
      FileInputStream fileInputStream = null;
      FileOutputStream fileOutputStream = null;
      FileTools.logger.debug("Copy "+sourceFile.getName()+" Floder Start......");
      if(sourceFile.exists() && sourceFile.isDirectory())
      {
       sourceChirenFile = sourceFile.listFiles();
       for (int i = 0; i < sourceChirenFile.length; i++) {
        File tempFile = null;
        
        if(!sourceFilePath.endsWith(File.separator))
        {
         tempFile = new File(targetFilePath+File.separator+sourceChirenFile[i].getName());
        }
        else
        {
         tempFile = new File(targetFilePath+sourceChirenFile[i].getName());
        }
        if(!FileTools.SPECIAL_FILE_STYLE_PICTURECACHE.equalsIgnoreCase(sourceChirenFile[i].getName()))
        {
         if(!sourceChirenFile[i].isDirectory())
         {
          fileInputStream = new FileInputStream(sourceChirenFile[i]);
          fileOutputStream = new FileOutputStream(tempFile);
          byte[] b = new byte[1024];
          FileTools.logger.debug("Copy "+sourceChirenFile[i].getName()+" File Start......");
             while(fileInputStream.read(b)!=-1)
             {
              fileOutputStream.write(b);
             }
             FileTools.logger.debug("Copy "+sourceChirenFile[i].getName()+"  File End!");
             fileInputStream.close();
             fileOutputStream.close(); 
         }
         else
         {
          copyDirectory(sourceFilePath+File.separator+sourceChirenFile[i].getName(),
                  targetFilePath+File.separator+sourceChirenFile[i].getName());
         }
        }
        
       }
       FileTools.logger.debug("Copy "+sourceFile.getName()+" Floder End!");
       return true;
      }
      else
      {
       FileTools.logger.error("The Floder Is Not Exist!");
       return false;
      }
     }
    }
    Junit測試類

     

    import java.io.IOException;

    import org.coolyongzi.FileTools;

    import junit.framework.TestCase;

    public class FileToolsTest extends TestCase {

     protected void setUp() throws Exception {
      super.setUp();
     }

     protected void tearDown() throws Exception {
      super.tearDown();
     }

     public void testDeleteDirectory()
     {
      FileTools.deleteDirectory("F:/復件 圖片",false);
     }
     
     public void testCopyDirectory()
     {
      try {
       FileTools.copyDirectory("F:/Game","F:/Game1");
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
    }

    posted on 2010-08-24 14:52 fly 閱讀(172) 評論(0)  編輯  收藏 所屬分類: java學習
    主站蜘蛛池模板: 亚洲 另类 无码 在线| 亚洲国产成人久久三区| 久久亚洲国产视频| 成人久久久观看免费毛片| 69式互添免费视频| 亚洲综合无码一区二区| 无码午夜成人1000部免费视频| 在线观看国产区亚洲一区成人| 亚洲精品无码不卡在线播放| 精品国产一区二区三区免费看| 亚洲成a人片在线观| 成人A级毛片免费观看AV网站| 亚洲乱码在线播放| 亚洲成年网站在线观看| 国产亚洲精品资在线| 香蕉成人免费看片视频app下载| aa午夜免费剧场| 337p日本欧洲亚洲大胆色噜噜| 久久久久久国产a免费观看黄色大片| 亚洲日本一区二区三区| 成年人网站在线免费观看| a高清免费毛片久久| 久久免费线看线看| 亚洲中文字幕无码亚洲成A人片| 国精无码欧精品亚洲一区| 日韩免费视频在线观看| 免费91最新地址永久入口| 4hu四虎最新免费地址| 久久爰www免费人成| 亚洲av成人无码网站…| 亚洲永久中文字幕在线| 久久精品国产亚洲AV不卡| 亚洲精品成人网站在线播放| 亚洲欧洲AV无码专区| 一区二区视频在线免费观看| 在线观看亚洲网站| 亚洲av最新在线观看网址| 亚洲高清中文字幕免费| 男女午夜24式免费视频| 国产美女无遮挡免费网站| 狼友av永久网站免费观看|