Home >Java >Javagetting Started >How to use io stream to copy pictures in java

How to use io stream to copy pictures in java

王林
王林forward
2020-08-03 16:44:174685browse

How to use io stream to copy pictures in java

java.io provides many stream classes. Here we are going to use FileInputStream/FileOutputStream----for reading raw bytes

( Recommended tutorial: java introductory tutorial)

First we copy a picture into the current directory, for example: "d:/java/TestPicture/image1.jpg", and then we use FileInputStream Read the picture, and finally FileOutputStream generates a picture as image2.jpg in the current directory.

(Video tutorial recommendation: java video tutorial)

java code:

import java.io.*;
public class TestPictureCopy{
    public static void main(String []args)throws Exception{
    //会抛异常,所以我们在主函数中给抛出去
   //输入流
     FileInputStream fis=new FileInputStream("image1.jpg");
     //输出流
     FileOutputStream fos=new FileOutputStream("image2.jpg");
     
        //我们用read(byte[]b,int off,int len)函数来读-确保两张图片大小一致,且效率还高
     byte[] b=new byte[1024];
       int num=0;
       while((num=fis.read(b,10,1000))!=-1){
            fos.write(b,10,num);
       }
       fis.close();
       fos.close();
   }
}

See if image1.jpg has been generated in the current directory Replicaimage2.jpg.

The above is the detailed content of How to use io stream to copy pictures in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete