孤燈野火
          暢想的天空
          posts - 2,comments - 4,trackbacks - 0
          文件操作類
          package fileOperation;

          import java.io.File;
          /**
           * 查看,修改文件或目錄的屬性
           * 
          @author wakin
           *
           
          */
          public class Attribute {
              
          /**
               * 查看路徑名所表示文件或目錄的屬性。
               * 
          @param fileName 路徑名
               
          */
              
          public void lookAttribute(String fileName) {
                  
          boolean canRead;
                  
          boolean canWrite;
                  
          boolean canExecute;
                  File file 
          = new File(fileName);
                  
          if(!file.exists())
                      
          throw new RuntimeException("File:"+fileName+"is not exist");
                  canRead 
          = file.canRead();
                  canWrite 
          = file.canWrite();
                  canExecute 
          = file.canExecute();
                  System.out.println(
          "Can read:"+canRead+"    Can write:"+canWrite+"    Can Execute:"+canExecute);
              }
              
          /**
               * 設(shè)置路徑名所表示的文件或目錄的的屬性。?部分功能可能在windows下無效。
               * 
          @param fileName 路徑名
               * 
          @param readable 是否可讀
               * 
          @param writable 是否可寫
               * 
          @param executable 是否可執(zhí)行
               * 
          @param ownerOnly  是否用戶獨(dú)享
               * 
          @return 屬性設(shè)置成功,返回true,否則返回false
               
          */
              
          public boolean setAttribute(
                      String fileName,
                      
          boolean readable,
                      
          boolean writable,
                      
          boolean executable,
                      
          boolean ownerOnly)
              {
                  
          boolean isDone = false;
                  File file 
          = new File(fileName);
                  isDone 
          = file.setReadable(readable, ownerOnly) 
                          
          && file.setWritable(writable, ownerOnly)
                          
          && file.setExecutable(executable, ownerOnly);
                  
          return isDone;
              }
          }


          package fileOperation;

          import java.io.BufferedInputStream;
          import java.io.BufferedOutputStream;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileOutputStream;
          import java.io.IOException;

          /**
           * 復(fù)制文件和文件夾工具類,能判斷源文件不存在,源文件不可讀,目標(biāo)文件已經(jīng)存在,
           * 目標(biāo)路徑不存在,目標(biāo)路徑不可寫等情況 
           * 
          @author wakin
           *
           
          */
          public class Copy 
          {

              
          /**根據(jù)源路徑名和目標(biāo)路徑名復(fù)制文件。
               * 
               * 
          @param source_name 源路徑名
               * 
          @param dest_name 目標(biāo)路徑名
               * 
          @param type  值為判斷如果目標(biāo)路徑存在是否覆蓋,1為覆蓋舊的文件,2為不覆蓋,操作取消。 
               * 
          @return 當(dāng)復(fù)制成功時(shí)返回1, 當(dāng)目標(biāo)文件存在且type值為2時(shí)返回2,其他情況返回0
               * 
          @throws IOException  發(fā)生I/O錯(cuò)誤
               
          */
              
          public int copyFile(
                      String source_name,
                      String dest_name,
                      
          int type) throws IOException {
                  
          int result = 0;
                  
          int byte_read;
                  
          byte [] buffer;
                  File source_file 
          = new File(source_name);
                  File dest_file 
          = new File(dest_name);
                  FileInputStream source 
          = null;
                  BufferedInputStream bis 
          = null;
                  BufferedOutputStream bos 
          = null;
                  FileOutputStream dest 
          = null;
                  
          try {
                      
          if(!source_file.exists() || !source_file.isFile()) //不存在
                          throw new RuntimeException("FileCopy: no such source file:"+source_name);
                      
          if(!source_file.canRead()) //不可讀
                          throw new RuntimeException("FileCopy: source file"+source_name
                                  
          +"is unreadable");
                      
          if(dest_file.exists()) {
                          
          if(dest_file.isFile()) {
                              
          if(type==1) { 
                                  dest_file.delete();   
          //覆蓋
                                  result = 1 ;
                              }
                              
          else if(type==2) {
                                  result 
          = 2;
                                  
          return result;    //不覆蓋 ,程序返回。
                              }
                          }
                          
          else
                              
          throw new RuntimeException("FileCopy: destination"+dest_name
                                      
          +"is not a file.");
                      }
                      
          else {
                          File parentDir 
          = new File(dest_file.getParent());  //獲得目錄信息
                          if(!parentDir.exists()) {
                              
          throw new RuntimeException("FileCopy: destination"+dest_name
                                      
          +"directory doesn't exist.");    //目錄不存在
                          }
                          
          if(!parentDir.canWrite())
                              
          throw new RuntimeException("FileCopy: destination"+dest_name
                                      
          +"is unwriteable.");            //目錄不可寫
                      }
                      
                      
          //開始復(fù)制文件
                      
          //輸入流
                      source = new FileInputStream(source_file);
                      bis 
          = new BufferedInputStream(source);
                      
          //輸出流
                      dest = new FileOutputStream(dest_file);
                      bos 
          = new BufferedOutputStream(dest);
                      buffer 
          = new byte[1024*5];
                      
          while((byte_read=bis.read(buffer))!=-1) {
                          bos.write(buffer, 
          0, byte_read);
                      }
                      result 
          = 1;
                  } 
          catch (IOException e) {
                      
          // TODO: handle exception
                      e.printStackTrace();
                  } 
          finally {
                      
          if(source!=null){
                          bis.close();
                          source.close();
                      }
                      
          if(dest!=null) {
                          bos.flush();
                          bos.close();
                          dest.flush();
                          dest.close();
                      }
                  }
                  
          return result;
                  
              }
              
              
          /**根據(jù)源路徑名和目標(biāo)路徑名復(fù)制目錄。
               * 復(fù)制目錄以復(fù)制文件為基礎(chǔ),通過遞歸復(fù)制子目錄實(shí)現(xiàn)。
               * 
          @param source_name 源路徑名
               * 
          @param dest_name 目標(biāo)路徑名
               * 
          @param type 值為判斷如果目標(biāo)路徑存在是否覆蓋,1為覆蓋舊的目錄,2為不覆蓋,操作取消。
               * 
          @return 當(dāng)復(fù)制成功時(shí)返回1, 當(dāng)目標(biāo)目錄存在且type值為2時(shí)返回2,其他情況返回0
               * 
          @throws IOException  發(fā)生I/O錯(cuò)誤
               
          */
              
          public int copyDirectory(
                      String source_name,
                      String dest_name,
                      
          int type
                      ) 
          throws IOException {
                  File source_file 
          = new File(source_name);
                  File dest_file 
          = new File(dest_name);
                  
          int result = 0;
                  Delete del 
          = new Delete();         //用于刪除目錄文件
                  if(!source_file.exists()||source_file.isFile())  //不存在
                      throw new RuntimeException("DirCopy: no such dir"+source_name);
                  
          if(!source_file.canRead()) //不可讀
                      throw new RuntimeException("DirCopy: source file"+source_name
                              
          +"is unreadable");
                  
          if(dest_file.exists()) {
                      
          if(type==1) {
                          del.deleteDir(dest_name);   
          //覆蓋
                          result = 1;
                      }
                      
          if(type==2) {
                          result 
          = 2;                 //不覆蓋
                          return result;
                      }
                  }
                  
          if(!dest_file.exists()) {
                      
          new File(dest_name).mkdirs(); //創(chuàng)建目標(biāo)目錄
                      File[] fileList = source_file.listFiles();
                      
          for(int i=0;i<fileList.length;i++){
                          System.out.println(fileList[i].getName());
                          
          if(fileList[i].isFile()){
                              
          //用copyFile函數(shù)復(fù)制文件
                              this.copyFile(
                                      source_name
          +"/"+fileList[i].getName(), 
                                      dest_name
          +"/"+fileList[i].getName(), 
                                      type);
                          }
                          
          else if(fileList[i].isDirectory()){
                              
          //遞歸
                              copyDirectory(
                                      source_name
          +"/"+fileList[i].getName(), 
                                      dest_name
          +"/"+fileList[i].getName(), type);
                          }
                      }
                      result 
          = 1;
                      
                  }
                  
                  
          return result;
              }
          }
          package fileOperation;

          import java.io.File;

          /**創(chuàng)建一個(gè)新的文件或目錄
           * 
          @author wakin
           *
           
          */
          public class Create 
          {
              
          /**
               * 根據(jù)路徑名創(chuàng)建文件
               * 
          @param filePath 路徑名
               * 
          @return 當(dāng)創(chuàng)建文件成功后,返回true,否則返回false.
               * 
               
          */
              
          public boolean createFile(String filePath) {
                  
          boolean isDone = false;
                  File file 
          = new File(filePath);
                  
          if(file.exists())
                      
          throw new RuntimeException("File: "+filePath+" is already exist");
                  
          try {
                      isDone 
          = file.createNewFile();
                  } 
          catch (Exception e) {
                      
          // TODO: handle exception
                      e.printStackTrace();
                  }
                  
          return isDone;
                  
              }
              
              
          /**
               * 根據(jù)路徑名創(chuàng)建目錄
               * 
          @param filePath 路徑名
               * 
          @return 當(dāng)創(chuàng)建目錄成功后,返回true,否則返回false.
               
          */
              
          public boolean createDir(String filePath) {
                  
          boolean isDone = false;
                  File file 
          = new File(filePath);
                  
          if(file.exists())
                      
          throw new RuntimeException("FileDirectory: "+filePath+" is already exist");
                  isDone 
          = file.mkdirs();
                  
          return isDone;
              }

          }
          package fileOperation;

          import java.io.File;
          import java.io.IOException;

          /**
           * 刪除一個(gè)文件或目錄
           * 
          @author wakin
           *
           
          */
          public class Delete 
          {

              
          /**
               * 刪除路徑名所代表的文件。
               * 
          @param filePath 路徑名
               * 
          @return 當(dāng)文件成功刪除,返回true,否則返回false.
               
          */
              
          public boolean deleteFile(String filePath) throws IOException{
                  File file 
          = new File(filePath);
                  
          if(file.exists()) {
                      file.delete();
                      
          //System.out.println(filePath+"文件已刪除.");
                      return true;
                  }
                  
          else {
                      
          //System.out.println("邏輯錯(cuò)誤:"+filePath+"文件不存在.");
                      return false;
                  }
              }
              
              
          /**
               * 刪除路徑名所代表的目錄
               * 
          @param filePath 路徑名
               * 
          @return 當(dāng)目錄成功刪除,返回true,否則返回false.
               
          */
              
          public boolean deleteDir(String filePath) throws IOException{
                  
          boolean isDone = false;
                  File file 
          = new File(filePath);
                  
          //判斷是文件還是目錄
                  if(file.exists()&&file.isDirectory()){
                      
          if(file.listFiles().length==0){
                          file.delete();
                          isDone 
          = true;
                      }
                      
          else {
                          File [] delFiles 
          = file.listFiles();
                          
          for(int i=0;i<delFiles.length;i++){
                              
          if(delFiles[i].isDirectory()){
                                  deleteDir(delFiles[i].getAbsolutePath()); 
          //遞歸調(diào)用deleteDir函數(shù)
                              }
                              
          else {
                                  delFiles[i].delete();
                              }
                          }
                      }
                      
          //刪除最后剩下的目錄名。
                      deleteDir(filePath);
                      isDone 
          = true;
                  }
                  
          else
                      
          return false;
                  
          return isDone;
              }

          }
          package fileOperation;

          import java.io.File;
          import java.io.IOException;
          /**
           * 移動(dòng)文件/目錄,利用Delete類和Copy類來實(shí)現(xiàn)。
           * 
          @author wakin
           *
           
          */
          public class Move {
              
          /**
               * 利用Copy類的函數(shù)和Delete類來完成移動(dòng)文件/目錄的操作。
               * 
          @param source_name 源路徑名
               * 
          @param dest_name   目標(biāo)路徑名
               * 
          @param type type  值為判斷如果目標(biāo)路徑存在是否覆蓋,1為覆蓋舊的文件/目錄,2為不覆蓋操作取消。 
               * 
          @return 當(dāng)移動(dòng)成功后返回1,當(dāng)目標(biāo)目錄存在且type值為2時(shí)返回2,其他情況返回0
               * 
          @throws IOException  發(fā)生I/O錯(cuò)誤
               
          */
              
          public int move(String source_name,String dest_name,int type) throws IOException{
                  
          int result = 0;
                  Copy copy 
          = new Copy();
                  Delete delete 
          = new Delete();
                  File source_file 
          = new File(source_name);
                  
          //File dest_file = new File(dest_name);
                  if(!source_file.exists())
                      
          throw new RuntimeException("FileMove: no such source file:"+source_name);
                  
          if(source_file.isFile()){
                      result 
          = copy.copyFile(source_name, dest_name, type); //調(diào)用Copy類的copyFile函數(shù)
                      if(result ==1)
                          delete.deleteFile(source_name);   
          //調(diào)用Delete類的deleteFile函數(shù)刪除源文件
                  }
                  
          else {
                      result 
          = copy.copyDirectory(source_name, dest_name, type);//調(diào)用Copy類的copyDirectory函數(shù)
                      if(result == 1)
                          delete.deleteDir(source_name);    
          //調(diào)用Delete類的deleteDir函數(shù)刪除源目錄
                  }
                  
          return result;
              }
          }
          package fileOperation;

          import java.io.File;
          import java.io.IOException;
          /**
           * 計(jì)算文件或目錄的空間大小。
           * 
          @author wakin
           *
           
          */
          public class Size {
              
          /**
               * 計(jì)算路徑名代表的文件或目錄所占空間的大小。
               * 
          @param fileName 路徑名
               * 
          @return 所占字節(jié)數(shù)
               * 
          @throws IOException 發(fā)生I/O錯(cuò)誤
               
          */
              
          public static long getSize(String fileName) throws IOException {
                  
          long result = 0;
                  File file 
          = new File(fileName);
                  
          if(!file.exists())
                      
          throw new RuntimeException("No such source file:"+fileName);
                  
          if(file.isFile()){
                      
          return file.length();
                  }
                  
          else {
                      String [] FileList 
          = file.list();
                      
          for(int i =0;i<FileList.length;i++) {
                          result 
          += getSize(fileName+"/"+FileList[i]);   //迭代
                      }
                  }
                  
          return result;
              }
          }







          posted on 2009-05-15 01:01 孤飛燕 閱讀(1337) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 广元市| 拜城县| 河间市| 策勒县| 宁陕县| 银川市| 武陟县| 安多县| 镇安县| 赞皇县| 建昌县| 漳州市| 防城港市| 康保县| 镇沅| 永寿县| 涟源市| 博兴县| 浮山县| 家居| 沾益县| 庐江县| 皮山县| 宜城市| 遵义县| 胶南市| 都安| 连平县| 鄂托克前旗| 民丰县| 博爱县| 德江县| 林口县| 雷山县| 河津市| 灵寿县| 景谷| 沙河市| 宁国市| 麻城市| 伊川县|