Java copy file

这几天一直在接受Java的培训,老师讲的很快,知识一过才发现原来自己已经忘了这么多东西了,也好久没写博客了,今天只好再开个头,写下Java中文件的拷贝。

import java.io.BufferedOutputStream;                               
import java.io.File;                                               
import java.io.FileInputStream;                                    
import java.io.FileNotFoundException;                              
import java.io.FileOutputStream;                                   
import java.io.IOException;                                        
import java.io.InputStream;                                        
import java.io.OutputStream;                                       
                                                                   
public class FileCopier {                                          
    public static void main(String[] args) throws IOException {       
        boolean bool = copy("D:\\1.jpg", "D:\\2.jpg");                  
        System.out.println(bool);                                       
    }                                                                 
                                                                   
                                                                      
    /**                                                               
     * Copy File                                                   
     *                                                             
     * @author syq                                                 
     *                                                             
     * @param  srcFile    --    source file                             
     *            tarFile  --     target file                                
     *                                                             
     * @return  {true} if copy success                             
     *             {false}    else                                          
     *                                                             
     */                                                            
    public static boolean copy(String srcFile, String tarFile) {      
        File file = new File(srcFile);                                  
        InputStream is = null;                                          
        OutputStream os = null;                                         
                                                                        
        try {                                                           
            is = new FileInputStream(file);    //输入流,读取文件到内存      
        } catch (FileNotFoundException e) {                             
            //e.printStackTrace();    //可以打印错误类型                    
            return false;                                                 
        }                                                                  
                                                                        
        try {                                                           
            os = new FileOutputStream(tarFile);    //将文件输出到磁盘        
        } catch (FileNotFoundException e) {                             
            //e.printStackTrace();                                        
            return false;                                                 
        }                                                                  
                                                                        
        BufferedOutputStream bos = new BufferedOutputStream(os);        
                                                                        
        int isEnd = 0;                                                  
        try {                                                           
            isEnd = is.read();                                            
        } catch (IOException e) {                                       
            //e.printStackTrace();                                        
            return false;                                                 
        }                                                               
        while(isEnd != -1) {                                            
            try {                                                         
                bos.write(isEnd);                                           
                isEnd = is.read();                                          
            } catch (IOException e) {                                     
                //e.printStackTrace();                                      
                return false;                                               
            }                                                             
        }                                                               
                                                                        
        try {                                                           
            bos.flush();                                                  
        } catch (IOException e) {                                       
            //e.printStackTrace();                                        
            return false;                                                 
        } finally {                                                     
            try {                                                         
                is.close();                                                 
                bos.close();                                                
            } catch (IOException e) {                                     
                //e.printStackTrace();                                      
                return false;                                               
            }                                                             
        }                                                               
                                                                        
        return true;                                                    
    }                                                                 
}                                                                  
                                                                   

 

转载于:https://www.cnblogs.com/yuxiaoqi/archive/2013/02/25/2932885.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值