IO flow overview and classification [understanding]

  • IO stream introduction

    • IO: input / output

    • Stream: an abstract concept, which is the general term for data transmission In other words, the transmission of data between devices is called stream, and the essence of stream is data transmission

    • IO stream is used to deal with data transmission between devices Common applications: file replication; File upload; File download

  • Classification of IO streams

    • According to the flow direction of data

      • Input stream: reading data

      • Output stream: write data

    • By data type

      • Byte stream

        • Byte input stream

        • Byte output stream

      • Character stream

        • Character input stream

        • Character output stream

  • Usage scenarios of IO streams

    • If the operation is a plain text file, the character stream is preferred

    • If binary files such as picture, video and audio are operated, byte stream is preferred

    • If the file type is uncertain, byte stream is preferred Byte stream is a universal stream

    • Byte stream write data [application]

    • Byte stream abstract base class

      • InputStream: this abstract class is a superclass that represents all classes of byte input stream

      • OutputStream: this abstract class is a superclass representing all classes of byte output stream

      • Subclass name features: subclass names are suffixed with their parent class name

    • Byte output stream

      • FileOutputStream(String name): creates a file output stream and writes it to the file with the specified name

    • To write data using a byte output stream

      • Create a byte output stream object (call the system function to create a file, create a byte output stream object, and let the byte output stream object point to the file)

      • Call the write data method of byte output stream object

      • Free resources (close this file output stream and free any system resources associated with this stream)

    • Sample code

    • public class FileOutputStreamDemo01 {
          public static void main(String[] args) throws IOException {
              //Create byte output stream object
            	/*
            		Note:
            				1.If the file does not exist, it will be created for us
            				2.If the file exists, it will be emptied
            	*/
            	//FileOutputStream(String name): creates a file output stream and writes it to the file with the specified name
              FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt");
      
              //void write(int b): writes the specified bytes to this file output stream
              fos.write(97);
      //        fos.write(57);
      //        fos.write(55);
      
              //Finally, we should release resources
              //void close(): closes the file output stream and frees any system resources associated with the stream.
              fos.close();
          }
      }

      Three ways to write data in byte stream [application]

    • public class FileOutputStreamDemo02 {
          public static void main(String[] args) throws IOException {
              //FileOutputStream(String name): creates a file output stream and writes it to the file with the specified name
              FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt");
              //FileOutputStream(File file): creates a File output stream to write to the File represented by the specified File object
      //        FileOutputStream fos = new FileOutputStream(new File("myByteStream\\fos.txt"));
      
              //void write(int b): writes the specified bytes to this file output stream
      //        fos.write(97);
      //        fos.write(98);
      //        fos.write(99);
      //        fos.write(100);
      //        fos.write(101);
      
      //        void write(byte[] b): writes b.length bytes from the specified byte array to the file output stream
      //        byte[] bys = {97, 98, 99, 100, 101};
              //byte[] getBytes(): returns the byte array corresponding to the string
              byte[] bys = "abcde".getBytes();
      //        fos.write(bys);
      
              //void write(byte[] b, int off, int len): write len bytes to the file output stream starting from the specified byte array and starting from offset off
      //        fos.write(bys,0,bys.length);
              fos.write(bys,1,3);
      
              //Release resources
              fos.close();
          }
      }

      Two small problems of byte stream writing data [application]

    • How to wrap byte stream write data

      • windows:\r\n

      • linux:\n

      • mac:\r

    • How to realize additional writing of byte stream write data

      • public FileOutputStream(String name,boolean append)

      • Creates a file output stream and writes to the file with the specified name. If the second parameter is true, the bytes are written to the end of the file instead of the beginning

    • Sample code

    • public class FileOutputStreamDemo03 {
          public static void main(String[] args) throws IOException {
              //Create byte output stream object
      //        FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt");
              FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt",true);
      
              //Write data
              for (int i = 0; i < 10; i++) {
                  fos.write("hello".getBytes());
                  fos.write("\r\n".getBytes());
              }
      
              //Release resources
              fos.close();
          }
      }

      Byte stream write data plus exception handling [application]

    • Exception handling format

    • try-catch-finally

  • try{
    	Possible exception codes;
    }catch(Exception class name variable name){
    	Exception handling code;
    }finally{
    	Perform all cleanup operations;
    }

    • finally features

      • Statements that are finally controlled must be executed unless the JVM exits

  • Sample code

  • public class FileOutputStreamDemo04 {
        public static void main(String[] args) {
            //Join finally to release resources
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream("myByteStream\\fos.txt");
                fos.write("hello".getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    Byte buffer stream construction method [application]

  • Introduction to byte buffer stream

    • lBufferOutputStream: this class implements buffered output stream By setting up such an output stream, an application can write bytes to the underlying output stream without causing a call to the underlying system for each byte written

    • L BufferedInputStream: creating BufferedInputStream will create an internal buffer array When bytes are read or skipped from the stream, the internal buffer will be refilled from the included input stream as needed, many bytes at a time

  • Construction method:

  • public class BufferStreamDemo {
        public static void main(String[] args) throws IOException {
            //Byte buffered output stream: BufferedOutputStream(OutputStream out)
     
            BufferedOutputStream bos = new BufferedOutputStream(new 				                                       FileOutputStream("myByteStream\\bos.txt"));
            //Write data
            bos.write("hello\r\n".getBytes());
            bos.write("world\r\n".getBytes());
            //Release resources
            bos.close();
        
    
            //Byte buffered input stream: BufferedInputStream(InputStream in)
            BufferedInputStream bis = new BufferedInputStream(new                                                          FileInputStream("myByteStream\\bos.txt"));
    
            //Read one byte of data at a time
    //        int by;
    //        while ((by=bis.read())!=-1) {
    //            System.out.print((char)by);
    //        }
    
            //Read one byte array data at a time
            byte[] bys = new byte[1024];
            int len;
            while ((len=bis.read(bys))!=-1) {
                System.out.print(new String(bys,0,len));
            }
    
            //Release resources
            bis.close();
        }
    }

Keywords: AI Data Mining

Added by godwheel on Sun, 26 Dec 2021 21:34:13 +0200