Input and output streams (IO streams) in Java

Keywords: Java less Junit

How to use read in java:

  1. Abstract int read() reads the next byte of data from the input stream (less efficient)
  2. int read(byte[] b) reads a certain number of input streams and stores them in buffer array B.
  3. Int read(byte[] b, int off, int len) reads the most len data bytes in the input stream into the byte array. (The return value of this method is our i value in the loop. For detailed analysis, see the code below.)
    Source code analysis:
    Here we will only make a detailed analysis of the third method.
   public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }//Previous tests are performed on arrays and incoming values to determine whether they are legitimate or not.

        int c = read();
        if (c == -1) {
            return -1;
        }//First, let's make sure that our document is not a blank one.
        b[off] = (byte)c;If it's not a blank file, we can read the first byte in first.

        int i = 1;//i started from the beginning because our starting position was added in the previous step.
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;//As long as we read the end of the file, we jump out of the loop.
                    //This is also the advantage of our third method over the second one.
                    //That's not to make our cache array misread because there's too much space left.
                }
                b[off + i] = (byte)c;
            }
        } catch (IOException ee) {
        }
        return i;//Finally, we can use this return value reasonably to keep our array from misreading.
    }

Raise a chestnut.

package com.zzxtit.first;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;

public class StreamTest {
	@Test
	public void inputStreamDemo() throws IOException {
//		String filePath= "D:\ target\ hello\ IO flow";
//		File file=new File(filePath);
//		System.out.println("Create folders"+file.mkdirs());
		InputStream is=new FileInputStream("D:\\target\\hello\\IO flow\\wei.txt");
		int arrLen=0;
		OutputStream os=new FileOutputStream("D:\\target\\hello\\IO flow\\wei_back.txt");
		byte[] buffers=new byte[4096];//We usually set the size of this array to 4096.
		while((arrLen=is.read(buffers,0,buffers.length))!=-1) {
			System.out.println("arrlen"+arrLen);
			os.write(buffers,0,arrLen);//In this step, we reasonably use the return value to make our entry accurate.
		}
		is.close();
		os.close();
	}
}

Note: The above code needs to be modified before it can work properly, and it contains the content of the author's personality.
Plus: Why do we say that the code above makes reasonable use of the return value of the read function, because we
The third parameter of our write function is the byte length we read from the cache array.

Posted by karimali831 on Sat, 12 Oct 2019 14:11:48 -0700