【java】IO处理流

学习笔记-java IO处理流以及其他流

增强功能、提供性能

节点流之上

缓冲流(节点流)

缓冲流

字节缓冲流

BufferedInputStream

BufferedOutputStream

package com.iotest.buffered;

import java.io.*;

/**
 * 文件拷贝+缓冲流
 * 提高性能
 */
public class Demo01BufferedByte {
    public static void main(String[] args) {
        File src = new File("D:/Code/Java/IO/src/main/resources/test.png");
        File dest = new File("D:/Code/Java/IO/src/main/resources/1020.png");
        try {
            copyFile(src,dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(File src, File dest) throws IOException {
        if (!src.isFile()) {
            System.out.println("only file");
            throw new IOException("only file");
        }
        // 建立联系 源(存在且为文件) + 目的地(文件可以不存在)
        // 选择流
        InputStream is = new BufferedInputStream(new FileInputStream(src));
        OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));
        if (!src.isFile()) {
            System.out.println("only file");
        }
        // 文件拷贝 循环+读取+写出
        byte[] flush = new byte[1024];
        int len = 0;
        while (-1 != (len = is.read(flush))) {
            os.write(flush, 0, len);
        }
        os.flush();

        os.close();
        is.close();
    }
}

字符缓冲流

新增方法

BufferedReader readLine()

BufferedWriter newLine()

package com.iotest.buffered;

import java.io.*;

public class Demo02BufferedChar {
    public static void main(String[] args) {
        File src = new File("D:/Code/Java/IO/src/main/java//com/iotest/charIO/demo01.java");
        File dest = new File("D:/Code/Java/IO/src/main/resources/2copy.txt");

        BufferedReader reader = null;
        BufferedWriter wr = null;

        try {
            reader = new BufferedReader(new FileReader(src));
            wr = new BufferedWriter(new FileWriter(dest));

            String line = null;
            while (null != (line = reader.readLine())) {
                wr.write(line);
                wr.newLine();
            }

            wr.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (wr != null) {
                try {
                    wr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
}

转换流

字节流 转换为 字符流

处理乱码(编码集、解码集体)

1、输出流

扫描二维码关注公众号,回复: 4354320 查看本文章

OutputStreamWriter 编码

2、输入流

InputStreamReader 解码

编码与解码概念

编码:字符 --编码字符集–> 二进制

解码:二进制 --解码字符集–> 字符

乱码

1、编码与解码的字符集不统一

2、字节缺少,长度丢失

public static void main(String[] args) throws UnsupportedEncodingException {
        // 解码 byte --> char
        String str = "中国中国";
        // 编码 char --> byte
        byte[] data = str.getBytes();
        // 编码与解码字符集统一
        System.out.println(new String(data));
        data = str.getBytes("GBK");
        System.out.println(new String(data));

        str = new String("中国".getBytes("GBK"),"GBK");
        System.out.println(str);
    }
public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(new File("D:/Code/Java/IO/src/main/resources/2copy.txt")),"UTF-8")
        );
        String info = null;
        while (null!= (info=br.readLine())) {
            System.out.println(info);
        }

        br.close();
    }

猜你喜欢

转载自blog.csdn.net/qq_43140314/article/details/83755145