Java 字符流

字符流

    单字节读取
    只能用来操作文本

Writer (所有输出流的父类)

FileWriter

    构造方法(绑定写入的路径):
        文件
        字符串

  mac系统下 一个字 3个字节 UTF-8
  windows 一个字 俩字节 GBK 简体中文
    FileWriter fw = new FileWriter("文件路径");
        fw.write(100);
        // 字符输出流 写入文件时
        // 需要调用刷新方法
        // 建议: 每次写入 都刷新一次
        fw.flush();
        // 字符数组写入
        char[] c = { 'z', 'h', 'a', 'n', 'g' };
        fw.write(c);
        fw.flush();

        fw.write(c, 1, 3);
        fw.flush();

        // 使用字符串直接写入
        fw.write("写一句古诗\n");
        fw.flush();
        fw.write("床前明月光\n疑是地上霜\n举头望明月\n低头思故乡\n");
        fw.flush();

        fw.write("白日依山尽", 1, 2);

        // 关闭资源前 会刷新
        fw.close();

Reader (所有字符输入流的父类)

FileReader

    FileReader fr = new FileReader("/Users/C/Desktop/Test/haha.txt");
        // int read = fr.read();
        // System.out.println(read);

        // 循环读取
        /*
        int len = 0;
        while ((len = fr.read()) != -1) {
            System.out.println((char)len);
        }
        */

        int num = 0;
        char[] c = new char[1024];
        while ((num = fr.read(c)) != -1) {
            System.out.println(new String(c, 0, num));
        }

        fr.close();

    "利用字符流复制文件(带异常处理)"

    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("文件路径");
            fos = new FileOutputStream("");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void copyDir(File oFile, File nFile ){
        File newFile = new File(nFile, oFile.getName());
        newFile.mkdir();

        File[] listFiles = oFile.listFiles();
        for (File subFile : listFiles) {
            if (subFile.isFile()) {
                FileWriter fw = null;
                FileReader fr = null;
                try {
                    fr = new FileReader(subFile);
                    File tempFile = new File(nFile, subFile.getName());
                    fw = new FileWriter(tempFile);
                    int len = 0;
                    char[] b = new char[1024];
                    while ((len = fr.read(b)) != -1) {
                        fw.write(b, 0, len);
                    }

                } catch (FileNotFoundException e) {
                    throw new RuntimeException();
                } catch (IOException e) {
                    throw new RuntimeException("关闭资源失败");

                } finally {
                    try {
                        if (fw != null) {
                            fw.close();
                        }               
                    } catch (IOException e) {
                        throw new RuntimeException("关闭资源失败");
                    }
                }


            }else {
                copyDir(subFile, newFile);
            }
        }
    }


转换流

这里写图片描述



  OutputStreamWriter(字符流转向字节流)
  作用: 可以根据不同编码格式写入
  需要使用到 FileOutputStream 类

  InputStreamWriter(字节流转向字符流)
  作用: 可以读取不同编码格式的文件
  需要使用到 FileInputStream 类



public static void main(String[] args) throws IOException {
         getUTF8();
         getGBK();
         readerGBK();
         readerUTF8();
    }
    // 读取GBK
    public static void readerGBK() throws IOException {
        FileInputStream fis = new FileInputStream("文件路径/GBK.txt");
        InputStreamReader isr = new InputStreamReader(fis, "GBK");
        char[] c = new char[1024];
        int len = 0;
        while ((len = isr.read(c)) != -1) {
            System.out.println(new String(c, 0, len));
        }
        isr.close();
    }
    // UTF-8
    public static void readerUTF8() throws IOException {
        FileInputStream fis = new FileInputStream("文件路径/utf8.txt");
        InputStreamReader isr = new InputStreamReader(fis);
        char[] c = new char[1024];
        int len = 0;
        while ((len = isr.read(c)) != -1) {
            System.out.println(new String(c, 0, len));
        }
        isr.close();
    }


    // 利用转换流写文件 OutputStreamWriter
    public static void getUTF8() throws IOException {
        FileOutputStream fos = new FileOutputStream("文件路径/utf8.txt");
        // 创建转换流 字符流转向字节流
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        // 写文件
        osw.write("张三");
        // 关闭资源(只关外层的流)
        osw.close();
    }
    // 利用转换流使用GBK的编码写入文件
    public static void getGBK() throws IOException {
        FileOutputStream fos = new FileOutputStream("文件路径/GBK.txt");
        // 构建转换流 传入编码格式(编码格式的字符串忽略大小写)
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        osw.write("张三");
        osw.close();

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值