IO流学习笔记(2)

三、处理流之一:缓冲流的使用

  1. 缓冲流:BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter

  2. 作用:提高流的读取,写入的速度

    提高读写速度的原因:内部提供了一个缓冲区

  3. 处理流,就是“套接”在已有的流的基础上

实现非文本文件的复制

对于资源关闭,要求:先关闭外层的流,再关闭内层的流

说明:关闭外层流的同时,内层流也会自动地进行关闭。关于内层流的关闭,我们可以忽略。

@Test
    public void test5() {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            // 1.造文件
            File srcFile = new File("departments.png");
            File destFile = new File("copy1.png");
            // 2.造流
            // 2.1 造节点流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);
            // 2.2造缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            // 3.复制的细节:读取、写入
            byte[] buffer = new byte[10];
            int len;
            while((len = bis.read(buffer)) != -1) {
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.资源关闭
            try {
                if(bos != null)
                    bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(bis != null)
                    bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
//        fos.close();
//        fis.close();
        }
    }

使用BufferedReader和BufferWriter实现文本文件的复制

@Test
    public void test55() {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            // 创建文件和相应的流
            br = new BufferedReader(new FileReader(new File("dbcp.txt")));
            bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));
            // 读写操作
            // 方式一:使用char[]数组
//            char[] cbuf = new char[1024];
//            int len;
//            while((len = br.read(cbuf)) != -1) {
//                bw.write(cbuf,0,len);
//            }
            // 方式二:使用String
            String data;
            while((data = br.readLine()) != null) {
                bw.write(data); //data中不包含换行符
                bw.newLine(); //提供换行的操作
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if(bw != null)
                    bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

总结

抽象基类节点流(或文件流)缓冲流(处理流的一种)
InputStreamFileInputStream(read(byte[] buffer))BufferedInputStream(read(byte[] buffer))
OutputStreamFileOutputStream(write(byte[] buffer , 0 , len))BufferedOutputStream(write(byte[] buffer,0,len)) / flush()
ReaderFileReader(char[] cbuf)BufferedReader(char[] cbuf)
WriterFileWriter(write(char[] cbuf , 0 , len))BufferedWriter(char[] cbuf,0,len) / flush()

练习

图片的加密

@Test
public void test5() {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream("departments.png");
        fos = new FileOutputStream("secret.png");
        byte[] buffer = new byte[20];
        int len;
        while((len = fis.read(buffer)) != -1) {
            // 字节数组进行修改
            // 错误的
//            for(byte b : buffer) {
//                b = (byte) (b ^ 5);
//            }
            // 正确的
            for (int i = 0; i < len; i++) {
                buffer[i] = (byte) (buffer[i] ^ 5);
            }
            fos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

图片的解密

@Test
public void test54() {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream("secret.png");
        fos = new FileOutputStream("huanyuan.png");
        byte[] buffer = new byte[20];
        int len;
        while((len = fis.read(buffer)) != -1) {
            for (int i = 0; i < len; i++) {
                buffer[i] = (byte) (buffer[i] ^ 5);
            }
            fos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

四、处理流之二:转换流的使用

  1. 转换流:属于字符流

    InputStreamReader:将一个字节的输入流转换为字符的输入流

    OutputStreamWriter:将一个字符的输出流转换为字节的输出流

  2. 作用:提供字节流与字符流之间的转换

  3. 解码:字节、字节数组 --> 字符数组、字符串

    编码:字符数组、字符串 --> 字节、字节数组

  4. 字符集

InputStreamReader的使用:实现字节的输入流到字符的输入流的转化

此时处理异常的话,仍然应该使用try-catch-finally

@Test
    public void test5() throws IOException {
        FileInputStream fis = new FileInputStream("dbcp.txt");
//        InputStreamReader isr = new InputStreamReader(fis); // 使用系统默认的字符集
        // 参数2指明了字符集,具体使用哪个字符集,取决于文件保存时使用的字符集
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8");

        char[] cbuf = new char[20];
        int len;
        while((len = isr.read(cbuf)) != -1) {
            String str = new String(cbuf,0,len);
            System.out.print(str);
        }
        isr.close();
    }

综合使用InputStreamReader和OutputStreamWriter

此时处理异常的话,仍然应该使用try-catch-finally

@Test
public void test25() throws Exception {
    // 1. 造文件、造流
    File file1 = new File("dbcp.txt");
    File file2 = new File("dbcp_gbk.txt");
    FileInputStream fis = new FileInputStream(file1);
    FileOutputStream fos = new FileOutputStream(file2);
    InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
    OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
    // 2. 读写过程
    char[] cbuf = new char[20];
    int len;
    while((len = isr.read(cbuf)) != -1) {
        osw.write(cbuf,0,len);
    }
    // 3. 关闭资源
    isr.close();
    osw.close();
}

其他流的使用

  1. 标准的输入、输出流
  2. 打印流
  3. 数据流

标准的输入、输出流

  1. System.in:标准的输入流,默认从键盘输入

    System.out:标准的输出流,默认从控制台输出

  2. System类的setIn(InputStream is) / setOut(PrintSream ps) 方式重新指定输入和输出流

  3. 练习:从键盘入字符害,要求将读取到的整行字符害转成大写输出。然后继续进行输入操作, 直至当输入“e”或者“exit”时,退出程序

方法一:使用Scanner实现,调用next()返回一个字符串

方法二:使用System.in实现。System.in --> 转换流 --> BufferedReader的readLine()

package com.atguigu.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class OtherStreamTest {
    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            InputStreamReader isr = new InputStreamReader(System.in);
            br = new BufferedReader(isr);
            while (true) {
                System.out.print("输入字符串:");
                String data = br.readLine();
                if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
                    System.out.println("程序结束");
                    break;
                }
                System.out.println(data.toUpperCase());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

打印流:PrintStream 和 PrintWriter

  1. 提供了一系列重载的 print() 和 println()
  2. 练习
@Test
public void test36() {
    PrintStream ps = null;
    try {
        FileOutputStream fos = new FileOutputStream(new File("/Users/cenyu/Documents/io/text.txt"));
        // 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
        ps = new PrintStream(fos, true);
        if (ps != null) {// 把标准输出流(控制台输出)改成文件
            System.setOut(ps);
        }
        for (int i = 0; i <= 255; i++) { // 输出ASCII字符
            System.out.print((char) i);
            if (i % 50 == 0) { // 每50个数据一行
                System.out.println(); // 换行
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (ps != null) {
            ps.close();
        }
    }
}

数据流

  1. DataInputStream 和 DataOutputStream
  2. 作用:用于读取或写出基本数据类型的变量或字符串
练习:将内存中的字符串、基本数据类型的变量写出到文件中

注意: 处理异常的话,仍然需要使用 try-catch-finally

@Test
public void test61() throws IOException {
    DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
    dos.writeUTF("岑昱");
    dos.writeInt(19);
    dos.writeBoolean(true);
    dos.close();
}
将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。

注意点: 读取不同类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致!

@Test
public void test72() throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
    String name = dis.readUTF();
    int age = dis.readInt();
    boolean isMale = dis.readBoolean();
    System.out.println("name = " + name + ", age = " + age + ", isMale = " + isMale);
    dis.close();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值