Java的IO流

1、汇总

字符流

  • Reader
    • BufferReader
    • InputStreamReader->FileReader
    • StringReader
    • PipeReader
    • ByteArrayReader
    • FilterReader->PushbackReader
  • Writer
    • BufferedWriter
    • OutputStreamWriter->FileWriiter
    • PrinterWriter
    • StringWriter
    • PiperWriter
    • CharArrayWriter
    • FilterWriter

字节流

  • InputStream
    • FileInputStream
    • FilterInputStream
      • BufferedInputStream
      • DataInputStream
      • PushbakInputStream
    • ObjectInputStream
    • PipedInputStream
    • SequenceInputStream
    • StringBuffereInputStream
    • ByteArrayInputStraam
  • OutputStream
    • FileOutputStream
    • FilterOutputStream
      • BufferedOutputStream
      • DataOutputStream
      • PrintStream
    • ObjectOutputStream
    • PipedOutputStream
    • ByteArrayOutputStream

2、流

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

IO流的分类:

  • 根据处理数据类型的不同:字符流和字节流
  • 流向不同:输入流和输出流

字符流和字节流的区别是:

  • 读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。
  • 处理对象不同:字节流能处理所有类型的数据(图片,avi等),字符流只能处理字符类型的数据。
  • 字节流:一次读入出8位二进制
  • 字符流:16位二进制

——>纯文本有限字符流,除此之外使用字节流

输入字节流InputStream:

  • InputStream:所有的输入字节流的父类,是一个抽象类
  • ByteArrayInputStreamStringBufferInputStreamFileInputStream是种基本的介质流,他们分别从Byte数组、StringBuffer、和本地文件中读取数据
  • PipedInputStream是从其他线程公用的管道中读取数据
  • ObjectInputStream和所有的FilterInputStream的子类都是装饰流

输出字节流OutputStream

  • OutputStream 是所有的输出字节流的父类,它是一个抽象类。
  • ByteArrayOutputStreamFileOutputStream 是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。
  • PipedOutputStream 是向与其它线程共用的管道中写入数据。
  • ObjectOutputStream 和所有FilterOutputStream 的子类都是装饰流。

节点流:直接与数据源相连,读入或读出

直接使用节点流,读写不方便,为了更快读写文件,才有了处理流

  • 常用的字节流:

    • 父类:InputStream OutputStreamReaderWriter
    • 文件:FileInputStreamFileOutputStreanFileReaderFileWriter 文件进行处理的节点流
    • 数组:数 组 :ByteArrayInputStreamByteArrayOutputStreamCharArrayReaderCharArrayWriter 对数组进行处理的节点流(对应的不再是文件,而是内存中的一个数组)
    • 字符串 :StringReaderStringWriter 对字符串进行处理的节点流
    • 管 道 :PipedInputStreamPipedOutputStreamPipedReaderPipedWriter 对管道进行处理的节点流
  • 处理流:处理流和节点流一块使用,在节点流的基础上,在套接一层,套接的就是处理流。如BufferedReader,处理流的构造方法总是要带一个其他的流对象做参数。一个流对象经过其他流的多次包装,成为流的链接

  • 常用的处理流:

    • 缓冲流:BufferedInputStreanBufferedOutputStreamBufferedReaderBufferedWriter 增加缓冲功能,避免频繁读写硬盘。

    • 转化流:InputStreamReaderOutputStreamReader实现字节流和字符流之间的转换。

      • 构造函数

        1
        2
        3
        4
        5
        InputStreamReader(InputStream);        //通过构造函数初始化,使用的是本系统默认的编码表GBK。
        InputStreamReader(InputStream,String charSet); //通过该构造函数初始化,可以指定编码表。
        OutputStreamWriter(OutputStream); //通过该构造函数初始化,使用的是本系统默认的编码表GBK。
        OutputStreamwriter(OutputStream,String charSet); //通过该构造函数初始化,可以指定编码表。

    • 数据流:DataInputStreamDataOuputStream等提供将基础数据类型写入文件中,或者读取出来。

3、实战操作

  • FileInputStream类的使用:读取文件内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    package com.app;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;

    public class A1{
    public static void main(String[],args){
    A1 a1 = new A1();

    //电脑d盘中的abc.txt文档
    String filePath = "D:/abc.txt";
    String result = a1.readFile(filePath);
    syso(result);
    }
    //读取指定文件的内容
    public String readFile(String filePath){
    FileInputStream fis = null;
    String result = "";
    try{
    //根据相同的路径实例化一个输入流对象
    fis = new FileInputStream(filePath);
    //返回这个输入流中可以被读的剩下的byte字节的估计值
    int size = fis.available();
    //根据输入流中的字节数创建byte数组
    byte[] array = new byte[size];
    //数据读到数组中
    fis.read(array);
    //根据获得的byte新建一个字符串,然后输出
    result = new String(arrar);
    }catch (FileNotFoundException e){
    e.printStackTrace();
    }catch(IOException e){
    e.printStackTrace();
    }finally{
    if(fis!=ull){
    try{
    fis.close();
    }catch(IOException e){
    e.printStackTrace();
    }
    }
    }
    return result;
    }
    }
  • FileOutputStream类的使用:讲内容写入文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    package com.app;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    public class A2 {

    public static void main(String[] args) {
    A2 a2 = new A2();

    //电脑d盘中的abc.txt 文档
    String filePath = "D:/abc.txt" ;

    //要写入的内容
    String content = "今天是2017/1/9,天气很好" ;
    a2.writeFile( filePath , content ) ;

    }


    public void writeFile( String filePath , String content ){
    FileOutputStream fos = null ;
    try {
    //1、根据文件路径创建输出流
    fos = new FileOutputStream( filePath );

    //2、把string转换为byte数组;
    byte[] array = content.getBytes() ;
    //3、把byte数组输出;
    fos.write( array );

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }catch (IOException e) {
    e.printStackTrace();
    }finally{
    if ( fos != null) {
    try {
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }


    }

  • 文件复制从D到E

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    package com.app;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    public class A3 {

    public static void main(String[] args) {
    A3 a2 = new A3();

    //电脑d盘中的cat.png 图片的路径
    String filePath1 = "D:/cat.png" ;

    //电脑e盘中的cat.png 图片的路径
    String filePath2 = "E:/cat.png" ;

    //复制文件
    a2.copyFile( filePath1 , filePath2 );

    }

    /**
    * 文件复制
    * @param filePath_old : 需要复制文件的路径
    * @param filePath_new : 复制文件存放的路径
    */
    public void copyFile( String filePath_old , String filePath_new){
    FileInputStream fis=null ;
    FileOutputStream fout = null ;
    try {
    // 根据path路径实例化一个输入流的对象
    fis = new FileInputStream( filePath_old );

    //2. 返回这个输入流中可以被读的剩下的bytes字节的估计值;
    int size = fis.available() ;
    //3. 根据输入流中的字节数创建byte数组;
    byte[] array = new byte[size];
    //4.把数据读取到数组中;
    fis.read( array ) ;

    //5、根据文件路径创建输出流
    fout = new FileOutputStream( filePath_new ) ;

    //5、把byte数组输出;
    fout.write( array );

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }catch (IOException e) {
    e.printStackTrace();
    }finally{
    if ( fis != null) {
    try {
    fis.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    if ( fout != null ) {
    try {
    fout.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
      

    + java读取远程文件实例

    ```java
    //读取远程文件
    @ApiOperation("通过URL读取远程文件")
    @GetMapping("getFileByURL")
    public ResponseWrapper<String> getFileByUrl(@RequestParam("location") String location) throws IOException {
    int b;
    String str = "";
    URL url = new URL(location);
    InputStream is = url.openStream();
    BufferedInputStream bis = new BufferedInputStream(is);

    while ((b = is.read()) != -1) {
    str = str + (char) b;
    }
    is.close();
    bis.close();
    str = new String(str.getBytes("ISO-8859-1"), "GB2312");
    return new ResponseWrapper<>(str);
    }
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~

支付宝
微信