• JAVA通过I/O流复制文件


    JAVA通过I/O流复制文件

    本文是对字节流操作,可以多音频视频文件进行操作,亲测有效。

    个人感觉这个东西就是靠记的, 没什么好解释的,,,,

      

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.FileOutputStream;
     5 import java.io.IOException;
     6 
     7 
     8 public class CopyMove {
     9 
    10     public static void main(String[] args){
    11         
    12         //时间
    13         long starttime = 0;
    14         long finishtime = 0;
    15         Calendar calendar = null;
    16      
    17     /*    String oldPath = "D:/test/gktgd.mp4";
    18         String newPath = "D:/test/new4.mp4";*/
    19         String oldPath = "D:/test/fff.flv";
    20         String newPath = "D:/test/ff.flv";
    21         
    22         File oldFile = new File(oldPath);
    23         File newFile = new File(newPath);
    24       
    25         //若文件不存在则创建
    26      if(!newFile.exists()){
    27             try {
    28                 newFile.createNewFile();
    29             } catch (IOException e) {
    30                 // TODO Auto-generated catch block
    31                 e.printStackTrace();
    32             }
    33         } 
    34         
    35         FileInputStream fIS = null;
    36         FileOutputStream fOS = null;
    37        
    38      //声明用于传递字节的byte 数组大小要是2的整数倍
    39         byte[] ttStream = new byte[4096];
    40         
    41         int len = 0;
    42         
    43         try {
    44             fIS = new FileInputStream(oldFile);
    45             fOS = new FileOutputStream(newFile);
    46              
    47         //获取开始时间       
    48             starttime = System.currentTimeMillis();
    49             
    50             System.out.println(starttime);
    51             len = fIS.read(ttStream);
    52        //若len为-1 证明读取完,若不为-1 则为读取的字节数 
    53             while((len)!=-1){
    54           //将读出的字节写入文件中
    55                 fOS.write(ttStream, 0, len);
    56            //再次读取 
    57                 len=fIS.read(ttStream);
    58             }
    59        //获得结束时间 
    60             finishtime = System.currentTimeMillis();
    61             System.out.println(finishtime);
    62             System.out.println("时间间隔为:" +(finishtime-starttime));
    63         } catch (FileNotFoundException e) {
    64             // TODO Auto-generated catch block
    65             e.printStackTrace();
    66         } catch (IOException e) {
    67             // TODO Auto-generated catch block
    68             e.printStackTrace();
    69         } finally {
    70             try {
    71          //按照先使用后关闭的原则关闭 
    72                 fOS.close();
    73                 fIS.close();
    74             } catch (IOException e) {
    75                 // TODO Auto-generated catch block
    76                 e.printStackTrace();
    77             }   
    78         }
    79      }
    80 }
  • 相关阅读:
    **RESTful API版本控制策略
    HTTP协议header标头详解
    $headers = $this->input->request_headers();返回请求头(header)数组
    ****Web API 版本控制的几种方式
    ****RESTful API 设计最佳实践(APP后端API设计参考典范)
    php怎么获取checkbox复选框的内容?
    Linux中Samba详细安装【转】
    linux中serial driver理解【转】
    Linux内核中进程上下文、中断上下文、原子上下文、用户上下文的理解【转】
    八、mini2440裸机程序之UART(2)UART0与PC串口通信【转】
  • 原文地址:https://www.cnblogs.com/Mr-Dawei/p/7375778.html
Copyright © 2020-2023  润新知