Usage of stream objects commonly used in Java (byte stream and buffer stream)

1 file byte stream
FileIputStream reads files by bytes, which is suitable for all types of files (images, videos, text files, etc.). java also provides FileReader to specifically read text files.

import java.io.FileInputStream;

*public class FileStreamDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            //Create file byte input stream object
            fis = new FileInputStream("e:/R.gif");
            int temp = 0;
            while((temp = fis.read()) != -1){
                System.out.println(temp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (fis != null){
                    fis.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}*
output:	25
		0
		91
		132
		112
		93
		18
		...
		...
	Note: the size of the returned value of the number of bytes is 0-256 between

FileOutputStream writes data to files in bytes, which is suitable for all types of files. Java also provides FileWriter for writing text files.

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileStreamDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //Create file byte input stream object
            fis = new FileInputStream("e:/R.jpg");
            //Create file byte output stream object
            fos = new FileOutputStream("e:/aa.jpg");
            int temp = 0;
            while((temp = fis.read()) != -1){
                fos.write(temp);
            }
            //Writes data from memory to disk
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (fis != null){
                    fis.close();
                }
                if(fis != null){
                    fos.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

Improve reading and writing efficiency through buffer

Mode 1
By creating a byte array with a specified length as a buffer, the read and write efficiency of Io stream is improved. This method is applicable to the buffer definition when reading large pictures. Note: the length of the buffer must be an integer power of 2. Generally, 1024 length is appropriate.

     import java.io.FileInputStream;
     import java.io.FileOutputStream;

public class FileStreamBufferedDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //Create file byte input stream object
            fis = new FileInputStream("e:/R.jpg");
            //Create file byte output stream object
            fos = new FileOutputStream("e:/aa.jpg");
            //Create a buffer to improve reading and writing efficiency
            byte[] buff = new byte[1024];

            int temp = 0;
            while((temp = fis.read(buff)) != -1){
                fos.write(buff,0,temp);
            }
            //Writes data from memory to disk
            fos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (fis != null){
                    fis.close();
                }
                if(fis != null){
                    fos.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    }

Mode II
By creating a byte array as a buffer, the length of the array is defined by the estimated length of the current file returned by the available() of the input stream object. When reading and writing a file, the file reading and writing operation is completed in one reading and writing operation. Note: if the file is too large, it will occupy more memory. Therefore, this method is not recommended for large files.

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileStreamBuffer2Demo {

    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //Create file byte input stream object
            fis = new FileInputStream("e:/R.jpg");
            //Create file byte output stream object
            fos = new FileOutputStream("e:/aa.jpg");
            //Create a buffer to improve reading and writing efficiency
            byte[] buff = new byte[fis.available()];
            fis.read(buff);
            //Writes data from memory to disk
            fos.write(buff);
            fos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (fis != null){
                    fis.close();
                }
                if(fis != null){
                    fos.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

2 buffered byte stream

Improve reading and writing efficiency through byte buffer stream

The Java buffer stream itself does not have the function of reading and writing Io streams, but adds the buffer function to other streams (node streams or other processing streams) to improve efficiency, just like wrapping other streams. Therefore, the buffer stream is a processing stream (wrapping stream).
When files or other data sources are read and written frequently, the efficiency is relatively low. This is that if the buffer stream is used, the information can be read and written more efficiently. Because the buffer stream caches the data first, and then reads it to the program or writes it to the destination at one time when the cache is full or refreshed manually.
Therefore, buffered stream is still very important. We remember to add buffered stream to improve performance during Io operation.
BufferedInputStream and BufferedOutputStream are buffered byte streams. The efficiency of the operation stream is improved through the internal cache array.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileStreamBuffered3Demo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try{
            fis = new FileInputStream("e:/R.jpg");
            bis = new BufferedInputStream(fis);
            fos = new FileOutputStream("e:/aa.jpg");
            bos = new BufferedOutputStream(fos);

            int temp = 0;
            while ((temp = bis.read())!= -1){
                bos.write(temp);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
           try {
             //Note: the sequence of closing the flow: "the last opened is closed first"
               if(bis != null){
                   bis.close();
               }if (fis != null){
                   fis.close();
               }if (bos != null){
                   bos.close();
               }if(fos != null){
                   fos.close();
               }
           }catch (Exception e){
               e.printStackTrace();
           }
        }
    }
}

Keywords: Java

Added by php12342005 on Fri, 24 Dec 2021 11:29:36 +0200