java转移文件

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4.   
  5. public class TestZip {  
  6.   
  7.     public static void main(String argv[]) {  
  8.         try {  
  9.             TestZip tz = new TestZip();  
  10.             tz.transferFile("d:/2.doc","e:/2.doc");  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.     }              
  15.         /** 
  16.          * 把文件从原始路径拷贝到新路径下并删除原来路径下的文件 
  17.          *@author: jwdstef 
  18.          *Date:Sep 28, 2010 4:45:39 PM 
  19.          *@param oldPath 旧的文件目录 
  20.          *@param newPath 新的文件目录 
  21.          *@throws Exception 
  22.          */  
  23.     private  void transferFile(String oldPath,String newPath) throws Exception {  
  24.               
  25.             int byteread = 0;  
  26.             File oldFile = new File(oldPath);  
  27.             FileInputStream fin = null;  
  28.             FileOutputStream fout = null;  
  29.             try{  
  30.                 if(oldFile.exists()){  
  31.                     fin = new FileInputStream(oldFile);  
  32.                     fout = new FileOutputStream(newPath);  
  33.                     byte[] buffer = new byte[fin.available()];  
  34.                     while( (byteread = fin.read(buffer)) != -1){  
  35.                         fout.write(buffer,0,byteread);  
  36.                     }  
  37.                     if(fin != null){  
  38.                         fin.close();  
  39.                         delFile(oldFile);  
  40.                     }  
  41.                 }else{  
  42.                     throw new Exception("需要转移的文件不存在!");  
  43.                 }  
  44.             }catch(Exception e){  
  45.                 e.printStackTrace();  
  46.                 throw e;  
  47.             }finally{  
  48.                 if(fin != null){  
  49.                     fin.close();  
  50.                 }  
  51.             }  
  52.         }  
  53.   
  54.   
  55.         /** 
  56.          * 删除文件,只支持删除文件,不支持删除目录 
  57.          * @param file 要删除的文件 
  58.          * @throws Exception 
  59.          */  
  60.         private static void delFile(File file) throws Exception {  
  61.             if(!file.exists()) {  
  62.                 throw new Exception("文件"+file.getName()+"不存在,请确认!");  
  63.             }  
  64.             if(file.isFile()){  
  65.                 if(file.canWrite()){  
  66.                     file.delete();  
  67.                 }else{  
  68.                     throw new Exception("文件"+file.getName()+"只读,无法删除,请手动删除!");  
  69.                 }  
  70.             }else{  
  71.                 throw new Exception("文件"+file.getName()+"不是一个标准的文件,有可能为目录,请确认!");  
  72.             }  
  73.         }  
  74.           
  75. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值