文件移动

  1.   /**  
  2.      * //1.从旧文件拷贝内容到新文件  
  3.      * //2.删除旧文件  
  4.      * @param oldPath the path+name of old file  
  5.      * @param newPath the path+name of new file  
  6.      * @throws Exception  
  7.      */  
  8. private void transferFile(String oldPath,String newPath) throws Exception {   
  9.            
  10.         int byteread = 0;   
  11.         File oldFile = new File(oldPath);   
  12.         FileInputStream fin = null;   
  13.         FileOutputStream fout = null;   
  14.         try{   
  15.             if(oldFile.exists()){   
  16.                 fin = new FileInputStream(oldFile);   
  17.                 fout = new FileOutputStream(newPath);   
  18.                 byte[] buffer = new byte[fin.available()]; 
  19.                 while( (byteread = fin.read(buffer)) != -1){   
  20.                     logger.debug("byteread=="+byteread);   
  21.                     fout.write(buffer,0,byteread);   
  22.                 }   
  23.                 if(fin != null){   
  24.                     fin.close();//如果流不关闭,则删除不了旧文件   
  25.                     this.delFile(oldFile);   
  26.                 }   
  27.             }else{   
  28.                 throw new Exception("需要转移的文件不存在!");   
  29.             }   
  30.         }catch(Exception e){   
  31.             e.printStackTrace();   
  32.             throw e;   
  33.         }finally{   
  34.             if(fin != null){   
  35.                 fin.close();   
  36.             }   
  37.         }   
  38.     }   
  39.   
  40.   
  41.     /**  
  42.      * 删除文件,只支持删除文件,不支持删除目录  
  43.      * @param file  
  44.      * @throws Exception  
  45.      */  
  46.     private void delFile(File file) throws Exception {   
  47.         if(!file.exists()) {   
  48.             throw new Exception("文件"+file.getName()+"不存在,请确认!");   
  49.         }   
  50.         if(file.isFile()){   
  51.             if(file.canWrite()){   
  52.                 file.delete();   
  53.             }else{   
  54.                 throw new Exception("文件"+file.getName()+"只读,无法删除,请手动删除!");   
  55.             }   
  56.         }else{   
  57.             throw new Exception("文件"+file.getName()+"不是一个标准的文件,有可能为目录,请确认!");   
  58.         }   
  59.     }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值