Java中的转换流

转换流

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

    可以使用不同编码格式写入
    GBK表 是 Windows默认的 一个字占2个字节
    UTF-8 是 Mac 默认的 一个字 占3个字节

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

字符流转向字节流

字符流转向字节流

字节流转向字符流字节流转向字符流

    public class Demo {
    public static void main(String[] args) throws IOException {
        //getUTF8();
        //getGBK();
        readGBK();
        readUTF8();
        readGBKByUTF8();
        readUTF8ByGBK();
    }

    // 利用 转换流 写文件 OutputStreamWriter 默认UTF-8写的
    public static void getUTF8() throws IOException {
        FileOutputStream fos = new FileOutputStream("/Users/s/Desktop/Test/UTF8.txt");
        // 创建转换流 字符流 转向字节流
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        // 写文件
        osw.write("扶大厦之将倾");
        // 关闭资源(只关闭外层的流 就行了)
        osw.close();
    }

    // 利用转换流使用GBK的编码 写入文件
    public static void getGBK() throws IOException {
        FileOutputStream fos = new FileOutputStream("/Users/s/Desktop/Test/GBK.txt");
        // 构建转换流 传入编码格式(编码格式的字符串 忽略大小写)
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        //写文件
        osw.write("挽狂澜之既倒");
        osw.close();
    }

    // 利用转换流读取UTF8编码格式的文件
    public static void readUTF8() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/s/Desktop/Test/UTF8.txt");
        InputStreamReader isr = new InputStreamReader(fis);

        int len = 0;
        char[] b = new char[1024];
        while ((len = isr.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }
        isr.close();
    }
    // 利用转换流读取GBK编码格式的文件
    public static void readGBK() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/s/Desktop/Test/GBK.txt");
        InputStreamReader isr = new InputStreamReader(fis, "GBK");
        int len = 0;
        char[] b = new char[1024];
        while ((len = isr.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }
        isr.close();
    }
    // 利用转换流读取GBK编码格式的文件
        public static void readGBKByUTF8() throws IOException {
            FileInputStream fis = new FileInputStream("/Users/s/Desktop/Test/GBK.txt");
            InputStreamReader isr = new InputStreamReader(fis);
            int len = 0;
            char[] b = new char[1024];
            while ((len = isr.read(b)) != -1) {
                System.out.println(new String(b, 0, len));
            }
            isr.close();
        }
        // 利用转换流读取UTF8编码格式的文件
        public static void readUTF8ByGBK() throws IOException {
            FileInputStream fis = new FileInputStream("/Users/s/Desktop/Test/UTF8.txt");
            InputStreamReader isr = new InputStreamReader(fis,"GBK");

            int len = 0;
            char[] b = new char[1024];
            while ((len = isr.read(b)) != -1) {
                System.out.println(new String(b, 0, len));
            }
            isr.close();
        }
}


                                        Day.25

http://blog.csdn.net/ssssssue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值