• 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;                                                    
        }                                                                 
    }                                                                  
                                                                       
  • 相关阅读:
    团队的最佳诠释
    诸葛烤鱼
    Agile Web Development with Rails 读书笔记 (四)
    Agile Web Development with Rails 读书笔记 (三)
    客户教育
    To Be Or Not To Be Is The Question
    [zz]记录一linux命令:xargs
    Ubuntu 12.04 开启 wubi 安装
    [zz]mknod用法以及主次设备号
    [zz]centos下安装KVM方法
  • 原文地址:https://www.cnblogs.com/yuxiaoqi/p/2932885.html
Copyright © 2020-2023  润新知