Java IO byte stream copy image implementation code

  • 2020-06-23 00:29:43
  • OfStack

Java IO-- Byte stream copy image instance

Byte streams are used to manipulate images, videos, and audio (base files)

Example code:


package learn;

import java.io.*;

public class Learn{
  public static void main(String[] args) throws IOException {
    File file1=new File("D:/a.jpg");
    File file2=new File("D:/b.jpg");
    byte[] b=new byte[(int)file1.length()];
    FileInputStream in=null;
    FileOutputStream out=null;
    try {
      in=new FileInputStream(file1);
      out=new FileOutputStream(file2);// A file is created without specifying it 
      while(in.read(b)!=-1){    //read()--int . -1 Means that the reading is complete 
        out.write(b);
      }
      out.flush();
      in.close();
      out.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
}

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: