Java basic learning summary - IO flow shutdown

Posted by cdjaco on Fri, 20 Mar 2020 15:55:53 +0100

1, Why the io stream must be closed in java

When we new a java stream object, we not only create an instance object of the corresponding class in the computer memory. In addition, it also takes up the corresponding system resources. When there is no reference to an instance object in memory, the java garbage collector will recycle it automatically according to the corresponding policy, but it cannot release the system resources. Therefore, we need to actively call the close() method to release the java stream object.

2, How to release resources:

1. Method 1:
        File file = new File("F:/JavaPractice/IO2/src/test");
        FileInputStream is = null;
        try {
            is = new FileInputStream(file);
            byte[] flush = new byte[1024];
            int len=0;
            while((len=is.read(flush)) != -1) {
                //The actual read length must be put here, otherwise the non read data in the flush array will also be output, such as the default value of 0
                System.out.print(new String(flush, 0, len));        
            }
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(is != null) {
                    is.close();
                }
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
2. Method 2:

If there are many streams that need to be closed, you can use the Closeable interface to encapsulate a method by yourself

public static void close(Closeable... ios) {    //In fact, the principle of polymorphism is applied here
        for(Closeable io:ios) {
            try {
                if(io != null) {
                    io.close();
                }
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

Add: public static void close (closed... IOS). The three points here are not ellipsis, but variable parameters.

Variable parameter reference

3. Method 3:

After java7, you can use try with resources statement to release Java flow objects, thus avoiding the tedious try catch finally statement.

public static void copy(String srcPath, String destPath) {
        //Create source
        File src = new File(srcPath);
        File dest = new File(destPath);
        
        //Selection flow
        try(FileInputStream is = new FileInputStream(src);
            FileOutputStream os = new FileOutputStream(dest)) { 
            //operation
            byte[] flush = new byte[1024*2];
            int len = 0;
            while((len=is.read(flush)) != -1) {
                os.write(flush, 0, len);
            }
            os.flush();
        }catch(FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }
Java novice, if there are errors, welcome to correct!

Topics: Java iOS