隨筆 - 100  文章 - 50  trackbacks - 0
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          我收藏的一些文章!

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          寫了個(gè)Java操作文件的簡(jiǎn)單例子,希望給需要幫助的兄弟提供點(diǎn)兒幫助,同時(shí)也希望大家給予批評(píng)指正。
          文件操作類,代碼如下:
          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測(cè)試類

           

          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:/復(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 閱讀(167) 評(píng)論(0)  編輯  收藏 所屬分類: java學(xué)習(xí)
          主站蜘蛛池模板: 东莞市| 八宿县| 上高县| 朝阳市| 屏山县| 赞皇县| 衢州市| 枣阳市| 靖西县| 三门峡市| 连平县| 江山市| 河北区| 广水市| 远安县| 石屏县| 双牌县| 长丰县| 西宁市| 垫江县| 弋阳县| 富平县| 诏安县| 积石山| 嘉兴市| 那坡县| 绥江县| 克东县| 石嘴山市| 岳西县| 洛浦县| 措勤县| 定西市| 宽甸| 嘉善县| 望都县| 韶关市| 应城市| 突泉县| 房产| 天峨县|