Use FileInputStream, FileOutputStream in Java to copy pictures __java Basics

Source: Internet
Author: User

1.FileInputStream, FileOutputStream Two streams are processing binary files. Mainly processing pictures, videos and so on files.

2.FileInputStream: Reads the contents of the binary file.

3.FileOutputStream: Write binary file.

4.FileInputStream, FileOutputStream Method please refer to:
Http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

5. Copying pictures is the process of reading and writing.

Example one: Copy Picture code Demo

Package com.

Stream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;

Import java.io.IOException;
        public class Testblob {public static void main (string[] args) {file F=new file ("D:\\1.jpg");
        File Destination =new file ("E:\\1.jpg");
        char context;
            try {//1. Create a file input stream to get bytes from the system file FileInputStream fis=new FileInputStream (f);
            2. Create a file output stream to write the data to the system file FileOutputStream fos=new FileOutputStream (destination);
            SYSTEM.OUT.PRINTLN ("File Size" +fis.available ());
            3. Save byte number byte[] B=new byte[fis.available ()];
            4. The input stream reads the byte number and saves it to the byte array fis.read (b);
            5. Writes a byte array to the destination file Fos.write (b);
            System.out.println ("Copy pictures successfully");
            6. Finally closes the input and output stream, releases the connection resource Fis.close ();
        Fos.close (); catch (FileNotFoundException e) {System.out.println("FileNotFoundException");
        E.printstacktrace (); 
            catch (IOException e) {System.out.println ("IOException");
        E.printstacktrace ();
 }

    }

}
Package com.

Stream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;

Import java.io.IOException;
        public class CopyPicture {public static void main (string[] args) {file F=new file ("D:\\1.jpg");
        File Destination =new file ("E:\\1.jpg");
        char context;
            try {//1. Create a file input stream to get bytes from the system file FileInputStream fis=new FileInputStream (f);
            2. Create a file output stream to write the data to the system file FileOutputStream fos=new FileOutputStream (destination);
             SYSTEM.OUT.PRINTLN ("File Size" +fis.available ());
              while (Fis.read ()!=-1) {//3. read target file context= (char) fis.read ();

             4. Write to target file Fos.write (context);
            } System.out.println ("Copy pictures successfully");
            5. Finally closes the input and output stream, releases the connection resource Fis.close ();
        Fos.close (); The catch (FileNotFoundException e) {System.out.println ("FileNotfoundexception ");
        E.printstacktrace (); 
            catch (IOException e) {System.out.println ("IOException");
        E.printstacktrace (); The method assignment picture could not be opened ........
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.