Home > Java > Javagetting Started > body text

How to use io stream to copy pictures in java

王林
Release: 2020-08-03 16:44:17
forward
4666 people have browsed it

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();
   }
}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template