zoukankan      html  css  js  c++  java
  • 文件的复制

      /** 

       * 下载本地文件 

       * @author 柳松 

       * @date 2015-12-30 下午3:27:02 

       * @throws Exception 

       */  

      private static void downLoadLocalFile() throws Exception{  

          File file = new File("D:/file/1.txt");  

          FileInputStream in = new FileInputStream(file);  

      //定义输出的路径  

          File saveDir = new File("D:/file/fileCopy");  

      if (!saveDir.exists()) {  

              saveDir.mkdirs();//创建多重目录  

          }  

          FileOutputStream os = new FileOutputStream(saveDir+"/"+file.getName());  

      //创建缓冲区  

      byte buffer[] = new byte[1024];  

      int len = 0;  

      // 循环将输入流中的内容读取到缓冲区当中  

      while ((len = in.read(buffer)) > 0) {  

             os.write(buffer, 0, len);  

          }  

          in.close();  

          os.close();  

      } 

     

      /** 

           * 下载网络文件 

           * @author 柳松 

           * @date 2015-12-30 下午3:27:19 

           * @throws Exception 

           */  

      private static void downLoadRemoteFile() throws Exception{  

              URL url = new URL("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");  

              InputStream in = url.openStream();  

      //定义输出的路径  

              File saveDir = new File("D:/file/fileCopy");  

      if (!saveDir.exists()) {  

                  saveDir.mkdirs();//创建多重目录  

              }  

              FileOutputStream os = new FileOutputStream(saveDir+"/"+"downLoad.jpg");  

      //创建缓冲区  

      byte buffer[] = new byte[1024];  

      int len = 0;  

      // 循环将输入流中的内容读取到缓冲区当中  

      while ((len = in.read(buffer)) > 0) {  

                  os.write(buffer, 0, len);  

              }  

              in.close();  

              os.close();  

          } 

  • 相关阅读:
    Prometheus监控告警浅析
    BaikalDB技术实现内幕(三)--代价模型实现
    微服务的熔断原理与实现
    2020双11,阿里巴巴集团数万数据库系统全面上云揭秘
    平行进化论再添证据 牙形刺远隔千里却发育模式相同
    如何利用设计模式改善业务代码?
    SpringBoot 无侵入式实现 API 接口统一 JSON 格式返回
    独家 | 这可能会引领通用AI的下一个重大突破
    iOS 网络优化: 使你的 App 网络交互更流畅
    Java Web整合开发(17) -- Struts 2.x 高级应用
  • 原文地址:https://www.cnblogs.com/zhaoleigege/p/7595906.html
Copyright © 2011-2022 走看看