当前位置:网站首页>2022-08-09 Study Notes day32-IO Stream

2022-08-09 Study Notes day32-IO Stream

2022-08-10 18:32:00 aggressive leeks

IO流

什么是IO?
  I:input (输入)
  O: output (输出)
  IO:通过ioTo complete the file to read and write

什么是IO流?
  Is memory and hard disk space between a file通道,Can complete the file to read and write

流的分类

  1. 按照流向 (以内存为参照物)
    From the hard disk space ———> 内存 (输入/读)
    从内存 ———> 硬盘空间 (输出/写)

  2. 按照操作单元(读取数据方式)
    字节流 ——> 按字节读取 (Can operate all of the files)
    字符流——> 按字符读取 (只能操作文本文件[.txt/.html/.java等])

  3. 按角色划分
    节点流:Direct manipulation is a specificIO流
    处理流:在节点流的基础上,做进一步的处理

    当一个流的构造方法中需要一个流的时候,This was reported in flow is called node flow
    外部负责包装的流叫做包装流/处理流,Node flow and packing flow is relative

Colseable接口

所有的流都实现类Colseable接口,Say they are可关闭的close()

close() ----> 关闭流

  1. 所有的流(In addition to the standard output stream)都需要手动关闭 (Java7之前)
  2. To ensure that the flow must be shut down,Don't close the flow takes up a lot of resources,可以将close()放到finally语句块中
  3. Convection is closed,Not empty judgment to do first,避免NPE
  4. 流的关闭顺序:先开后关,后开先关
  5. The closing of the packing flow and node flow:只需要关闭包装流即可
  6. 对于输出流来说,使用close()After you don't need to inflush()

AutoCloseable接口(Java7)

由于Closeable继承了AutoCloseable接口,表示所有的流,都是Can be automatically shut down
使用新语法:try-with-resource
-----> 在try的”()“中声明并初始化实现AutoClosealbe接口的流.

public class Demo {
    
	@Test
    public void testAutoColse() {
       
        byte[] datas = {
    73, 32, 76, 111, 118, 101, 32, 89, 111, 117};
        // 新语法 try-with-resource
        try (OutputStream out = new FileOutputStream("1.txt")) {
    
            out.write(datas);
        } catch (FileNotFoundException e) {
    
            e.printStackTrace();
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    }
}

Java9之后,对try-with-resourceGrammar simplified,”()“里面可以是一个变量,但必须是final的或者等同final才行.

public class Demo {
    
	 @Test
    public void testAutoColse() throws FileNotFoundException {
    
        byte[] datas = {
    73, 32, 76, 111, 118, 101, 32, 89, 111, 117};
        OutputStream out = new FileOutputStream("1.txt");
        try (out) {
    
            out.write(datas);
        } catch (FileNotFoundException e) {
    
            e.printStackTrace();
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    }
}

Flushable接口

所有的输出流(InputStream/Writer)都实现了Flushable接口,Said all the output stream is可刷新的flush()

flush() ----> 刷新流
注意close()具有flush()的功能
flush()Said it will channel surplus not output data output(清空管道),如果没有flush()可能会丢失数据
具体可以看下面的例子

public class Demo {
    
	 @Test
    public void testFlush() throws IOException{
    
        char[] datas = {
    73, 32, 76, 111, 118, 101, 32, 89, 111, 117};
        BufferedWriter writer = new BufferedWriter(new FileWriter("1.txt"));
        writer.write(datas);
        
        // writer.flush();
    }
}

在这里插入图片描述

Flow of the four family leader

字节流
	java.io.InputStream ----> 字节输入流
	java.io.OutputStream ----> 字节输出流
字符流
	java.io.Reader ----> 字符输入流
	java.io.Writer ----> 字符输出流

需要掌握的16个流

文件专属

FileInputStream类

  1. 包路径:java.io.FileInputStream

  2. 继承关系:
    在这里插入图片描述

  3. 构造方法在这里插入图片描述

  4. 方法
    在这里插入图片描述

FileOutputStream类

  1. 包路径:java.io.FileOutputStream

  2. 继承关系
    在这里插入图片描述

  3. 构造方法(The output stream initialization time——重点)
    在这里插入图片描述

FileReader类

  1. 包路径:java.io.FileReader
  2. 继承关系
    在这里插入图片描述
  3. For constructing method and the method,没什么需要特别注意的地方,Specific content please see the help documentation

FileWriter类

  1. 包路径:java.io.FileWriter

  2. 继承关系
    在这里插入图片描述

  3. For constructing method and the method,没什么需要特别注意的地方,Specific content please see the help documentation

缓冲流

Take the buffer output stream to rememberflush()或close(),否则会导致数据丢失

BufferedInputSteam类

  1. 包路径:java.io.BufferedInputStream
  2. 继承关系
    在这里插入图片描述
  3. 方法

BufferedOutputStream类

  1. 包路径:java.io.BufferedOutputStream
  2. 继承关系
    在这里插入图片描述

BufferedReader类

  1. 包路径:java.io.BufferedReader

  2. 继承关系
    在这里插入图片描述

  3. 构造方法
    在这里插入图片描述

  4. 方法
    在这里插入图片描述

BufferedWriter类

  1. 继承关系
  2. 构造方法
    在这里插入图片描述
  3. 方法
    在这里插入图片描述

转换流

  将字节流转换成字符流:字节流 ----> 转换流(字符流) 可以指定字符集

InputStreamReader类

  1. 包路径:java.io.InputStreamReader

  2. 继承关系
    在这里插入图片描述

  3. 构造方法
    在这里插入图片描述

指定字符集
public class Demo01 {
    
	@Test
    public void testInputStreamReader() throws IOException {
    
        // Did not specify the character set,使用默认字符集(此处是UTF-8),在使用reader()Methods while reading the file data,以UTF-8Character set file data for(解码)
        InputStreamReader reader1 = new InputStreamReader(new FileInputStream("temp.txt"));
        // 指定字GBK符集,在使用reader()Methods while reading the file data,以GBKCharacter set file data for(解码)
        InputStreamReader reader2 = new InputStreamReader(new FileInputStream("temp.txt"),"GBK");

    }
}
Read the file at the time of the garbled problems and solutions

案例:When the default character set for theUTF-8时,Try on thisGBKCode files to read
在这里插入图片描述
具体代码如下

public class Demo01 {
    
	 @Test
    public void testInputStreamReader() {
    
        // Did not specify the character set,使用默认字符集(此处是UTF-8),在使用reader()Methods while reading the file data,以UTF-8Character set file data for(解码)
        try(InputStreamReader reader1 = new InputStreamReader(new FileInputStream("temp.txt"));) {
    
            int data;
            while ((data = reader1.read()) != -1) {
    
                System.out.println(data + " ----> " + (char) data);
            }
        } catch (FileNotFoundException e) {
    
            e.printStackTrace();
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    }
}

结果如下
在这里插入图片描述
为什么会出现乱码呢?

由于在readerInitialization time did not specify the character set,So the default character set(When the default hereUTF-8),所以使用readerTo read the byte stream,会使用用UTF-8字符集进行解码1,The file using the encoding ofGBK,Encoding and decoding the specified character set different,So the garbled words here.

  1. 设置默认字符集为GBK(不推荐)
    在这里插入图片描述
    将Global Encoding设置为GBK
  2. 在InputStreamReaderInitialize the specified when the character set for theGBK

在这里插入图片描述

采用这两种方式,The file can be read normal
在这里插入图片描述

OutputStreamWriter类

  1. 包路径:java.io.OutputStreamWriter
  2. 继承关系
    在这里插入图片描述
  3. 构造方法
    在这里插入图片描述
指定字符集
public class Demo01 {
    
	 @Test
    public void testOutputStreamWriter() throws FileNotFoundException {
    
        // Did not specify the character set,使用默认字符集(此处是UTF-8),在使用writer()Method to write data into the file,以UTF-8Set the data(编码)
        OutputStreamWriter writer1 = new OutputStreamWriter(new FileOutputStream("temp.txt"));
        // Did not specify the character set,使用默认字符集(此处是UTF-8),在使用writer()Method to write data into the file,以UTF-8Set the data(编码)
        OutputStreamWriter writer2 = new OutputStreamWriter(new FileOutputStream("temp.txt"));
    }
}

注意:
如果文件不存在,Create the specified character set file
如果文件存在,Not set additional,Create the specified character set file and override the original file
如果文件存在,And set up additional,Does not overwrite the original file will not change the original file coded character set used

Written to the file of the code problems and solutions

数据流

标准输出流

  注意:标准输出流不需要手动close(),但一定要flush(),Especially with the buffer flow

PrintStream类

  1. 包路径:java.io.PrintStream
  2. 继承关系
    在这里插入图片描述
  3. For constructing method and the method,没什么需要特别注意的地方,Specific content please see the help documentation

We often use output statements"System.out.println()",Then use the standard output stream,其中"out"便是SystemUnder the class of a static property,这个“out”是PrintStream类型,The default content output to the console.Then we can change the output statements the output of the position that?答案是可以的,我们可以通过调用"System.setOut()"这个方法来设置out属性.

获取System.out
package com.jsoft.io;

import org.junit.Test;
import java.io.*;

public class Demo01 {
    
	 public void testPrintStream() {
    
        // 获取System.out
        PrintStream myOut = System.out;
        // 向控制台输出内容
        myOut.println("Pan her,我是你爹");
    }
}
更改System.out (Change the output statements output location)
package com.jsoft.io;

import org.junit.Test;
import java.io.*;

public class Demo01 {
    
	@Test
   public void testPrintStream() throws FileNotFoundException {
    

       PrintStream myOut = new PrintStream("temp.txt");
       // 更改System.out的输出位置(控制台 ---> temp.txt)
       System.setOut(myOut);
       System.out.println("向temp.txt输出的内容");

       // 将System.outTo modify the original output location(temp.txt ---> 控制台)
       System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
       System.out.println("To the console output content");
   }
}

在这里插入图片描述

PrintWriter类

  1. 包路径:java.io.PrintWriter

  2. 继承结构
    在这里插入图片描述

  3. For constructing method and the method,和PrintStream基本没啥区别,Specific content please see the help documentation

对象专属流


  1. 解码:The bytes in the specified character set into character
    So what should we to readGBK呢?

原网站

版权声明
本文为[aggressive leeks]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208101805186931.html