JAVA-转换流

InputStreamReader

查看API文档,发现是字节流通向字符流的桥梁。查看构造,可以传递字节流,可以指定编
工作。该流是一个Reader的子类,是字符流的体系。所以将转换流称之为字节流和字符流
之间的桥梁。
InputStreamReader 是字节流通向字符流的桥梁
测试InputStreamReader:
    第一步: 需要专门新建以GBK编码的文本文件。为了便于标识,我们命名为gbk.txt
        和以UFT-8编码的文本文件,命名为utf.txt
    第二步: 分别写入汉字”中国”
    第三步:编写测试方法,用InputStreamReader 分别使用系统默认编码,GBK,
    UTF-8编码读取文件.
public class Demo1 {

    public static void main(String[] args) throws Exception {
        //系统默认编码文件
        File file = new File("./src/a.txt");
        //使用gbk编码的文件
        File gbkFile = new File("./src/gbk.txt");
        //使用utf-8编码的文件
        File utfFile = new File("./src/utf.txt");

        // 默认编码
        testReadFile(gbkFile);
        System.out.println("====================");
        // 传入gbk编码文件,使用gbk解码
        testReadFile(utfFile, "gbk");
        System.out.println("====================");
        // 传入utf-8文件,使用utf-8解码
        testReadFile(file, "utf-8");

    }

    private static void testReadFile(File file, String string) throws Exception {

        FileInputStream fis = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(fis,string);

        int content = 0;
        while((content = reader.read())!=-1){
            System.out.print((char)content);
        }
        reader.close();
        System.out.println("");
    }

    private static void testReadFile(File file) throws Exception {

        FileInputStream fis = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(fis);

        int content = 0;
        while((content = reader.read())!= -1){
            System.out.print((char)content);
        }

        reader.close();
        System.out.println("");
    }
}

OutputStreamWriter

有了InputStreamReader 可以转换InputStream  
那么其实还有OutputStreamWriter 可以转换OutputStream
OutputStreamWriter 是字符流通向字节流的桥梁
测试OutputStreamWriter 
    一: 分别使用OutputStreamWriter使用系统默认编码,GBK,UTF-8相对应的默认
    编码文件,GBK编码文件,UTF-8编码文件中写出汉字”中国”.
    二: 在使用上述案例中的readFile方法传入相对应码表读取.
public class TestIo {
    public class Demo4 {
    public static void main(String[] args) throws IOException {
        File file = new File("c:\\a.txt");
        File fileGBK = new File("c:\\gbk.txt");
        File fileUTF = new File("c:\\utf.txt");

        // 写入
        // 使用系统默认码表写入
        testWriteFile(file);
        // 使用gbk编码向gbk文件写入信息
        testWriteFile(fileGBK, "gbk");
        // 使用utf-8向utf-8文件中写入信息
        testWriteFile(fileUTF, "utf-8");

        // 读取
        // 默认编码
        testReadFile(file);
        // 传入gbk编码文件,使用gbk解码
        testReadFile(fileGBK, "gbk");
        // 传入utf-8文件,使用utf-8解码
        testReadFile(fileUTF, "utf-8");

    }

    // 使用系统码表将信息写入到文件中
    private static void testWriteFile(File file) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter ops = new OutputStreamWriter(fos);
        ops.write("中国");
        ops.close();
    }

    // 使用指定码表,将信息写入到文件中
    private static void testWriteFile(File file, String encod)
            throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter ops = new OutputStreamWriter(fos, encod);
        ops.write("中国");
        ops.close();
    }

    // 该方法中nputStreamReader使用系统默认编码读取文件.
    private static void testReadFile(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader ins = new InputStreamReader(fis);
        int len = 0;
        while ((len = ins.read()) != -1) {
            System.out.print((char) len);
        }
        ins.close();

    }

    // 该方法适合用指定编码读取文件
    private static void testReadFile(File file, String encod)
            throws IOException {
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader ins = new InputStreamReader(fis, encod);
        int len = 0;
        while ((len = ins.read()) != -1) {
            System.out.print((char) len);
        }

        ins.close();
    }

}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值