JavaIO

1. Byte stream and character stream

There are two data transmission formats in io, one is character stream and the other is byte stream, but character stream will involve coding.

2. There are four abstract top-level interfaces in the IO stream

3. Decoration design mode: that is, based on the existing objects of the principle, it provides more powerful functions on the basis of this object, that is, it can expand the functions of the original class without repairing the source code. The class used for enhancement is called decoration class, This class generally uses the constructor to accept the objects of the class that need to be decorated. For example, bufferedread() calls the read method of FileReader. When it comes to function expansion, it naturally thinks of inheritance. Here is why decoration mode is sometimes needed. Suppose there are the following class hierarchies:

---ReadText
---ReadMedia
---ReadData
那么我们要用缓冲区读的话我们就会有以下的类层次结构:
    ---ReadText
        |--BufferedReadText
    ---ReadMedia
        |--BufferedReadMedia
    ---ReadData
        |--BufferedReadData
类层级就非常臃肿
所以我们想到就用装饰者模式:
假如有以下装饰类:
   class BufferedReader{
       BufferedReader(ReadText readText){

       }
       BufferedReader(ReadMedia readMedia){

       }
       BufferedReader(ReadData readData){

       }
   }

Although this is OK, if we want to expand the subclass of read, the decoration class must be modified, and the expansibility is very poor, so we directly use polymorphism

   class BufferedReader extends Read{
     private Read read;
       BufferedReader(Read read){

       }

     public int read(char cbuf[],int off,int len){
           return read.read( cbuf[],off,len);//这里我们必须要复写Read里面的抽象方法,但是我们不知道这个read怎么实现,所以我们直接调用传进来的子类的对象的read方法

     }

     public boolean close(){
         return read.close();  //同上
     }
   }

In this way, the class hierarchy is:

  ---Read
    |---ReadText
    |---ReadMedia
    |---ReadData
    |---BufferedRead

Note that the decoration class is a subclass of read, and it is at the same level as the decorated class. Because it is a stronger decorated class, they are at the same level

4. Byte stream processing

    InputStream
        |--FileInputStream
    OutputStream
        |--FileOutputStream
  

The processing methods of these two classes are exactly the same as the character stream above. The only difference is that there is an avaliable method to determine the size of bytes in the file when reading it, and it is in bytes

public class FileInputStreamDemo {
    public static void main(String[] args) throws IOException{
        FileInputStream fileInputStream=new FileInputStream("a.txt");
        read_3(fileInputStream);
    }

    public static void read_1(FileInputStream fileInputStream)throws IOException{
        byte by;
        while ((by= (byte) fileInputStream.read())!=-1){
            System.out.print((char) by);
        }
    }

    public static void read_2(FileInputStream fileInputStream)throws IOException{
        byte[] buffer=new byte[1024];
        char[] buf=new char[1024];
        int num=0;
        StringBuilder builder=new StringBuilder();
        while ((num=fileInputStream.read(buffer))!=-1){
            // TODO: 2017/7/16 转数组为字符串的问题   尤其是基本类型转字符串的问题   是不是还有什么好的对象去做这件事
            for (byte b:buffer){
                builder.append((char) b);
            }
        }
        System.out.println(builder);
    }

    public static void read_3(FileInputStream fileInputStream) throws IOException{
        byte[] by=new byte[fileInputStream.available()];   //这个函数就可以确定字节个数   不过只能用于较小的文件  大文件开辟太大的缓冲区会出问题
        fileInputStream.read(by);
        StringBuilder stringBuilder=new StringBuilder();
        for (byte bys:by){
            // TODO: 2017/7/16 转数组为字符串的问题   尤其是基本类型转字符串的问题   是不是还有什么好的对象去做这件事
            stringBuilder.append((char)bys);
        }
        System.out.println(stringBuilder);

    }
}

5. as like as two peas of BufferedInputStream and bufferedoutputstream, two of them are the same.

6. Use of flow objects. There are too many types of flow objects. Here, in order to distinguish which flow object to use, let's analyze it in detail:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>