IO stream: byte stream copy video

Byte stream:

Byte output stream: OutputStream

There are many subclasses of OutputStream , and the subclass FileOutputStream can be used to write data to a file.

The FileOutputStream class , the file output stream , is an output stream used to write data to File .

  FileOutputStream (String name): Create an output file stream that writes data in a file with the specified name

Byte input stream: InputStream

 

   int read (): read a byte and return, return -1 if there is no byte.

 

  int read (byte []): Read a certain number of bytes and store them in a byte array, and return the number of bytes read.

There are many subclasses of InputStream , and the subclass FileInputStream can be used to read the file content.

FileInputStream gets input bytes from a file in the file system.

Here is a small case of copying video from a byte stream:

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class Work01 {
 public static void main (String [] args) throws IOException {
  // One array one array copy
  Date date = new Date ();
  System.out.println ("Start time:" + date.getTime ()) ;
  // Explicit data source
  FileInputStream fis = new FileInputStream ("D: \\ io1227 \\ video \\ AMG_GT_ALL_4_original.mp4");
  // Explicit destination
  FileOutputStream fos = new FileOutputStream ("D: \\ io1227 \\ mp4. mp4 ");
  // Start to copy
  byte [] bytes = new byte [1024];
  int len ​​= 0;
  while ((len = fis.read (bytes))! =-1) {
   fos.write (bytes);
  }
  fis.close ();
  fos.close ();
  Date date2 = new Date ();
  System.out.println ("Array copy end time:" + date2.getTime ());
  // One byte one byte copy
  FileInputStream fis2 = new FileInputStream ("D: \\ io1227 \\ video \\ AMG_GT_ALL_4_original.mp4");
  FileOutputStream fos2 = new FileOutputStream ("D: \\ io1227 \\ mp5.mp4");
  int len2 = 0;
  while ( (len2 = fis2.read ())! =-1) {
   fos2.write (len2);
  }
  fis2.close ();
  fos2.close ();
  Date date3 = new Date ();
  System.out.println (" Byte copy end time: "+ date3.getTime ());
  System.out.println (" Array copy time: "+ (date2.getTime ()-date.getTime ()) +" ms ");
  System.out .println ("Time for copying bytes:" + (date3.getTime ()-date2.getTime ()) + "ms");
 }
}

 

This is a small video with a size of 2330KB, it is very obvious that the array is much more efficient than bytes

Guess you like

Origin www.cnblogs.com/nbkls/p/12743512.html