zoukankan      html  css  js  c++  java
  • 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;                                                    
        }                                                                 
    }                                                                  
                                                                       
  • 相关阅读:
    Install Jetty web server on CentOS 7 / RHEL 7
    Linux MYSQL:dead but pid file exists
    Tomcat7注册为Linux服务
    CentOS查看版本及架构信息
    CentOS(6.8)7 安装 Mysql 5.7
    CentOS7 截图
    Kitematic when login show Error:Tunning socket could not be established
    Installing and removing VNC Connect | Red Hat | VNC Connect
    使用 Nexus Repository Manager 搭建私有docker仓库
    Docker attach
  • 原文地址:https://www.cnblogs.com/yuxiaoqi/p/2932885.html
Copyright © 2011-2022 走看看