java I style

1. Flow relationship

1. Classification of IO streams:

    Java's IO stream is divided into: byte stream (InputStream / OutputStream) and character stream (Reader / Writer) according to the type of data processed;

    According to the data flow direction, it is divided into: input stream (Input) and output stream (Output);

               

2. Byte stream and character stream

    1) Byte stream: is the most basic IO stream;

                   The read unit is byte (8bit), usually use Byte[] buffer = new Byte[size] to define the number of bytes read at a time;

                   Can handle all types of data;

    2) Character stream: Due to different data encoding, there is a stream object for efficient operation of characters, which is essentially based on checking the specified code table when reading the byte stream;

                   The reading unit is character, usually use Char[] buffer = new Char[size] to define the number of characters read at a time;

                   Can only handle character type data;

    As long as it is dealing with plain text data, the character stream is given priority, otherwise the byte stream is used;

3. Input stream output stream correspondence of byte stream

                                   

1) Input stream ( InputStreamIO )

  • InputStream is the parent class of all input byte streams, it is an abstract class.
  • ByteArrayInputStream, StringBufferInputStream, and FileInputStream are three basic media streams that read data from Byte arrays, StringBuffers, and local files, respectively. PipedInputStream reads data from pipes shared with other threads;
  • ObjectInputStream (used for serialization) and all subclasses of FilterInputStream are decorated streams (the protagonist of the decorator pattern)

2) Output Stream ( OutputStream )

  • OutputStream is the parent class of all output byte streams, it is an abstract class.
  • ByteArrayOutputStream and FileOutputStream are two basic media streams that write data to Byte arrays and local files respectively. PipedOutputStream is to write data to the pipe shared with other threads;
  • ObjectOutputStream (deserialization) and all subclasses of FilterOutputStream are decorative streams.

The blue part in the figure is the main corresponding part, and the red part is the non-corresponding part. The purple dotted lines indicate that these streams are generally used in combination . As can be seen from the above figure, the byte stream in Java IO is extremely symmetrical. "Existence and Reason" Let's take a look at a few classes that are not asymmetrical in these byte streams!

  1. When LineNumberInputStream mainly completes reading data from the stream, it will get the corresponding line number. As for when and where to branch, it is actively determined by the reclassification, and there is no such a line number in the original. There is no corresponding part in the output part, we can build a LineNumberOutputStream by ourselves, which will have a reference line number when it is initially written, and will add a line number to the next line every time a newline is encountered in the future, which seems to be possible. It seems to be less popular.
  2. The function of PushbackInputStream is to view the last byte and put it into the buffer if it is not satisfied. Mainly used in the syntax and lexical analysis part of the compiler. The BufferedOutputStream of the output part implements almost the same function.
  3. StringBufferInputStream has been Deprecated, itself should not appear in the InputStream section, mainly because String should belong to the scope of the character stream. It's deprecated, and of course the output section doesn't need it anymore! It is also allowed to exist just to maintain version backward compatibility.
  4. SequenceInputStream can be considered as a tool class that reads two or more input streams as one input stream in sequence. It can be completely removed from the IO package, and it does not affect the structure of the IO package at all, but makes it more "pure" - pure Decorator mode.
  5. PrintStream can also be thought of as a helper tool. It can mainly write data to other output streams or FileInputStream, and its internal implementation is still buffered. Essentially, it is just a tool for the comprehensive use of other streams. The same can kick out the IO package! System.out and System.out are instances of PrintStream!

4. Character stream correspondence

                                     

1) Character input stream (Reader)

  • Reader is the parent class of all input character streams, it is an abstract class.
  • CharReader and StringReader are two basic media streams, which read data from Char array and String respectively. PipedReader reads data from a pipe shared with other threads.
  • BufferedReader is obviously a decorator, and it and its subclasses are responsible for decorating other Reader objects.
  • FilterReader is the parent class of all custom specific decoration streams, and its subclass PushbackReader decorates the Reader object and adds a line number.
  • InputStreamReader is a bridge connecting byte stream and character stream, it converts byte stream to character stream. FileReader can be said to be a commonly used tool class to achieve this function, and the method of converting FileInputStream into Reader is obviously used in its source code. We can get certain tricks from this class. The purpose and usage of each class in Reader are basically the same as those in InputStream. There will be a correspondence between Reader and InputStream later.

2) Character output stream (Writer)

  • Writer is the parent class of all output character streams, it is an abstract class.
  • CharArrayWriter and StringWriter are two basic media streams, which write data to Char array and String respectively. PipedWriter is to write data to the pipe shared with other threads;
  • BufferedWriter is a decorator that provides buffering functionality for Writers.
  • PrintWriter and PrintStream are very similar in function and usage.
  • OutputStreamWriter is a bridge from OutputStream to Writer, and its subclass FileWriter is actually a concrete class that implements this function (for details, you can study a SourceCode). The function and usage are very similar to OutputStream, and there will be their corresponding diagrams later.

5. Character stream and byte stream conversion

Features of Transform Streams:

  1. It is a bridge between character stream and byte stream
  2. The read byte data can be converted into characters through the specified encoding
  3. The read character data can be converted into bytes through the specified encoding

When to use transform streams?

  1. When there is a conversion action between bytes and characters;
  2. When the data of the stream operation needs to be encoded or decoded.

Specific objects reflect:

  1. InputStreamReader: A Byte-to-Character Bridge
  2. OutputStreamWriter: A character-to-byte bridge

       These two stream objects are members of the character system. They have conversion functions and are character streams themselves, so you need to pass in byte stream objects during construction.

6. File class

        The File class is an object that encapsulates files and folders in the file system, and can operate files and folders through the idea of ​​objects. The File class saves various metadata information of a file or directory, including file name, file length, last modification time, whether it is readable, obtains the path name of the current file, determines whether the specified file exists, obtains the list of files in the current directory, and creates , delete files and directories, etc.

7.RandomAccessFile type

       This object is not a member of the stream system, it encapsulates the byte stream, and also encapsulates a buffer (character array), and operates the data in the character array through the internal pointer. The object features:

  1. The object can only operate on files, so the constructor receives two types of parameters: a. string file path; b. File object.
  2. The object can perform both read and write operations on the file, and the operation mode (r, rw) can be specified when the object is instantiated.

Note: When the object is instantiated, if the file to be operated does not exist, it will be automatically created; if the file exists and the write data does not specify a location, it will be written from the beginning, that is, the original content will be overwritten.  Can be used for multi-threaded download or multiple threads to write data to file at the same time

The above reference article: https://www.cnblogs.com/cangsir/p/5397619.html

 

2. The use of IO streams

1. Copy file using byte stream

    @Test //使用字节流复制文件
    public void test1(){
        //初始化文件输入流和文件输出流
        FileOutputStream fileOutputStream = null;
        FileInputStream fileInputStream = null;
        long start = System.currentTimeMillis();
        try {
            //文件读取和文件输出位置
            fileOutputStream = new FileOutputStream(new File("D:\\java\\javacopy.rar"));
            fileInputStream = new FileInputStream(new File("D:\\java\\java.rar"));
            //每次读取的字节数
            byte[] bytes = new byte[128];
            //当读取的字节数不为空时,写出字节到输出位置
            while ((fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭流
            try {
                fileInputStream.close();
                fileOutputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            System.out.println("cost time : "+(System.currentTimeMillis() - start));
        }
    }

cost time : 68852

2. Copy the file using a buffered byte stream

    @Test
    public void test2() {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        long start = System.currentTimeMillis();
        try{
            bufferedInputStream = new BufferedInputStream(new FileInputStream(new File("D:\\java\\java.rar")));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File("D:\\java\\javacopy.rar")));
            byte[] bytes = new byte[128];
            while (bufferedInputStream.read(bytes) !=-1){
                bufferedOutputStream.write(bytes);
            }

        }catch (IOException e ){
            e.printStackTrace();
        }finally {
            try {
                bufferedInputStream.close();
                bufferedOutputStream.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            System.out.println("cost time : "+(System.currentTimeMillis() - start));
        }


    }

cost time : 13986

Obviously, the time spent is saved by nearly 5/6;

3. Copy Text Documents Using Character Streams

    @Test
    public void test3() {
        InputStreamReader  inputStreamReader = null;
        OutputStreamWriter outputStreamWriter = null;
        long start = System.currentTimeMillis();
        try {
            //字节流转字符流,这里最好指定Charset字符集编码,如中文指定GBK,否则将使用本地默认字符集,这里你不清楚格式的话会导致乱码
            inputStreamReader = new InputStreamReader(new FileInputStream(new File("source.txt")),"GBK");
            outputStreamWriter = new OutputStreamWriter(new FileOutputStream(new File("target.txt")),"GBK");

            char[] buffer = new char[16];
            while (inputStreamReader.read(buffer) !=-1){
                outputStreamWriter.write(buffer);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                inputStreamReader.close();
                outputStreamWriter.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            System.out.println("cost time : "+(System.currentTimeMillis() - start));
        }

    }

cost time : 8769

4. Buffer character stream

@Test
    public void test4() {
        BufferedReader reader = null;
        BufferedWriter writer = null;

        long start = System.currentTimeMillis();
        try {
            //字节流转字符流,这里最好指定Charset字符集编码,如中文指定GBK,否则将使用本地默认字符集,这里你不清楚格式的话会导致乱码
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("source.txt")),"GBK"));
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("target.txt")),"GBK"));

            char[] buffer = new char[512];
            while (reader.read(buffer) !=-1){
                writer.write(buffer);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                reader.close();
                writer.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            System.out.println("cost time : "+(System.currentTimeMillis() - start));
        }

    }

cost time : 8580

Why hasn't the consumption time been significantly reduced? (Leave a message in the comment area... )

5.

   @Test
    public void test5() {
        BufferedReader reader = null;
        BufferedWriter writer = null;

        long start = System.currentTimeMillis();
        try {
            //字节流转字符流,这里最好指定Charset字符集编码,如中文指定GBK,否则将使用本地默认字符集,这里你不清楚格式的话会导致乱码
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("source.txt")),"GBK"));
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("target.txt")),"GBK"));
            String text ;
            while ((text = reader.readLine()) !=null){
                writer.write(text);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
               // writer.flush();
                reader.close();
                writer.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            System.out.println("cost time : "+(System.currentTimeMillis() - start));
        }

    }

cost time : 10492

Time has increased???

6.

@Test
    public void test6(){

        FileReader reader = null;
        FileWriter writer = null;
        long start = System.currentTimeMillis();
        try {
            reader = new FileReader("source1.txt");
            writer = new FileWriter("target1.txt",true);
            char[] buffer = new char[512];
            while (reader.read(buffer) !=-1){
                writer.write(buffer);
            }
        }catch (IOException e){
        }finally {
            try {
                reader.close();
                writer.close();
            }catch (IOException e){
                e.printStackTrace();
            }
            System.out.println("cost time : "+(System.currentTimeMillis() - start));
        }
    }

I don't know why the target(6K) is 1k larger than the source(5K), the comparison files are exactly the same, strange???

7. Read a section (demo)

@Test
    public void test7() throws IOException{
        RandomAccessFile randomAccessFile = new RandomAccessFile("source1.txt","r");
        FileWriter writer = new FileWriter("target3.txt");
        long length = randomAccessFile.length();
        System.out.println("文件大小:"+length);
        randomAccessFile.seek(length-103);
        byte[] bytes = new byte[9];
        while (randomAccessFile.read(bytes) !=-1){
            System.out.println(new String(bytes,"UTF-8"));
            writer.write(new String(bytes,"UTF-8"));
        }
        writer.flush();
    }

File size: 5100
frequency pattern
generation
method, which can
improve the anti -jamming capability of tactical data link system in
electronic
countermeasure environment .





��。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325440283&siteId=291194637