面向对象——IO流

目录

一、IO流概述和分类

1、IO流概述: 

2、IO流分类: 

二、字节流 

1、字节流写数据 :

2、字节流写入数据的3种方式 

3、字节流写数据的2个问题 

4、字节流缓冲流 

三、字符流 

1、字符流 = 字节流 + 编码表 ,编码解码问题

2、字符流读写数据

3、字符缓冲流 


一、IO流概述和分类

1、IO流概述: 

  • I表示intput,是数据从硬盘进内存的过程,称之为读 
  • O表示output,是数据从内存到硬盘的过程。称之为写

2、IO流分类: 

  • 按流向分:输入、输出流;
  • 按数据类型分:字节、字符流; 

二、字节流 

1、字节流写数据 :

  1. 创建字节输出流对象:如果文件不存在,就创建;如果文件存在就清空。
  2. 写数据:写出的整数,实际写出的是整数在码表上对应的字母。
  3. 释放资源:每次使用完流必须要释放资源。

 可以直接使用try-resource,不用手写释放资源:

public void searchAllFile(File sourceFile,File postFile){
        File[] files =sourceFile.listFiles();
        postFile.mkdirs();
        File keepPostFile = postFile;
        //先判断文件是否存在
        if (files!=null){
            for (File child : files) {
                String name = child.getName();
                File newChild = child;
                newChild = new File(postFile+"\\"+name);
                if (child.isDirectory()){
                    newChild = new File(postFile+"\\"+name);
                    postFile = new File(postFile+"\\"+name);
//                    newChild = postFile;
                    newChild.mkdirs();
                    searchAllFile(child,postFile);
                }else {
                    try (FileInputStream fileInputStream = new FileInputStream(child.getAbsolutePath());
                        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
                        FileOutputStream fileOutputStream = new FileOutputStream(newChild);
                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                    ) {
                        byte[] content = new byte[128];
                        while(bufferedInputStream.read(content)!=-1){
                            bufferedOutputStream.write(content);
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

2、字节流写入数据的3种方式 

方法名说明

void write​(int b)

一次写一个字节数据

void write​(byte[] b)

一次写一个字节数组数据

void write​(byte[] b, int off, int len)

一次写一个字节数组的部分数据

3、字节流写数据的2个问题 

字节流写数据换行:

  • 写完数据后,加换行符 :\r\n;

字节流写数据追加:

  • public FileOutputStream​(String name,boolean append) 
  • 创建文件输出流以指定的名称写入文件。如果第二个参数为true ,不会清空文件里面的内容

4、字节流缓冲流 

字节缓冲流:

  • BufferOutputStream:缓冲输出流  
  • BufferedInputStream:缓冲输入流

构造方法:

  • 字节缓冲输出流:BufferedOutputStream​(OutputStream out) 
  • 字节缓冲输入流:BufferedInputStream​(InputStream in)

例子:拷贝单级目录下的所有子文件

public void childFileCopy(File sourceFile,File postFile){
        File[] files = sourceFile.listFiles();
        if(!postFile.exists()){
            postFile.mkdir();
        }
        for (File child : files) {
            String name = child.getName();
            try (FileInputStream fileInputStream = new FileInputStream(child.getAbsolutePath());
                 BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
                 FileOutputStream fileOutputStream = new FileOutputStream(postFile+"\\"+name);
                 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            ) {
                byte[] content = new byte[128];
                while(bufferedInputStream.read(content)!=-1){
                    bufferedOutputStream.write(content);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

三、字符流 

1、字符流 = 字节流 + 编码表 ,编码解码问题

 编码:

  • byte[] getBytes​():使用平台的默认字符集将该 String编码为一系列字节,将结果存储到新的字节数组中
  • byte[] getBytes​(String charsetName):使用指定的字符集将该 String编码为一系列字节,将结果存储到新的字节数组中

解码:

  • String(byte[] bytes):通过使用平台的默认字符集解码指定的字节数组来构造新的 String
  • String(byte[] bytes, String charsetName):通过指定的字符集解码指定的字节数组来构造新的 String 

charsetName:最好指定UTF-8 

2、字符流读写数据

方法名说明

void write​(int c)

写一个字符

void write​(char[] cbuf)

写入一个字符数组

void write​(char[] cbuf, int off, int len)

写入字符数组的一部分

void write​(String str)

写一个字符串

void write​(String str, int off, int len)

写一个字符串的一部分

int read​()

一次读一个字符数据

int read​(char[] cbuf)

一次读一个字符数组数据

注意事项:

  1. 创建字符输出流对象:如果文件不存在,就创建,但是要保证父级路径存在;如果文件存在就清空 。
  2. 写数据:写出int类型的整数,实际写出的是整数在码表上对应的字母;写出字符串数据,是把字符串本身原样写出。
  3. 释放资源:每次使用完流必须要释放资源。

3、字符缓冲流 

字符缓冲流:

  • BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区大小,或者可以接受默认大小。默认值足够大,可用于大多数用途 
  • BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取,可以指定缓冲区大小,或者可以使用默认大小。 默认值足够大,可用于大多数用途

构造方法:

  • BufferedWriter​(Writer out) 
  • BufferedReader​(Reader in)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值