飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

转换流与打印流

时间:2021-12-08  作者:wydilearn  

编码引出的问题

import 域名eredReader;
import 域名Reader;
import 域名ception;

/*
FileReader可以读取IDE默认编码格式(UTF-8)的文件
FileReader读取系统默认编码(中文GBK)会产生乱码���
 */
public class new01 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\environment\\java_project\\javase\\我是GBK格式文件.txt"));
        int len=0;
        while ((len=域名())!=-1){
            域名tln((char) len);
        }
        域名e();
    }
}

转换流

转换流原理

OutputStreamWriter

import 域名OutputStream;
import 域名ception;
import 域名utStreamWriter;

/*
域名utStreamWriter extends Writer
OutputStreamWriter:是字符流通向字节流的桥梁:可使用指定的charset将要写入流中的字符编码成字节。(编码:把能看懂的编程看不懂的)

继承自父类的共性成员方法:
    - void write(int c) 写入单个字符
    - void write(char[] cbuf) 写入字符数组
    - abstract void write(char[] cbuf, int off, int len) 写入字符数组的某一部分,off数组的开始索引,len写的字符个数
    - void write(String str) 写入字符串
    - void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数
    - void flush() 刷新该流的缓冲
    - void close() 关闭此流,但要先刷新它
构造方法:
    OutputStreamWriter(OutputStream out) 创建使用默认字符编码的 OutputStreamWriter
    OutputStreamWriter(OutputStream out, String charsetName) 创建使用指定字符集的 OutputStreamWriter
    参数:
        OutputStream out:字节输出流,可以用来写转换之后的字节到文件中
        String charsetName:指定的编码表名称,不区分大小写,可以是utf-8/UTF-8,gbk/GBK,...不指定默认使用UTF-8
使用步骤:
    1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
    2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
    3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
    4.释放资源
 */
public class Demo01OutputStreamWriter {
    public static void main(String[] args) throws IOException {
        write_utf_8();
    }
/*
使用转换流OutputStreamWriter写UTF-8格式的文件
 */
    private static void write_utf_8() throws IOException {
        //1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
        //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\environment\\java_project\\javase\\域名"), "utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\environment\\java_project\\javase\\域名"), "GBK");
        //2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
        域名e("你好");
        //3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
        域名h();
        //4.释放资源
        域名e();
    }
}

InputStreamReader

import 域名InputStream;
import 域名ception;
import 域名tStreamReader;

/*
域名tStreamReader extends Reader
InputStreamReader:是字节流通向字符流的桥梁:它使用指定的charset读取字节并将其解码为字符。(解码:把看不懂的变成能看懂的)

继承自父类的共性成员方法:
    int read()读取单个字符并放回
    int read(char[] cubf)一次读取多个字符,将字符读入数组
    void close()关闭该流并释放与之关联的所有资源
构造方法:
    InputStreamReader(InputStream in) 创建一个使用默认字符集的InputStreamReader
    InputStreamReader(InputStream in, String charsetName) 创建使用指定字符集的InputStreamReader
    参数:
        InputStream in:字节输入流,用来读取文件中保存的字节
        String charsetName:指定的编码表名称,不区分大小写,可以是utf-8/UTF-8,gbk/GBK,...不指定默认使用UTF-8
使用步骤:
    1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
    2.使用InputStreamReader对象中的方法read读取文件
    3.释放资源
注意事项:
    构造方法中指定的编码表名称要和文件的编码相同,否则会发生乱码
 */
public class Demo02InputStreamReader {
    public static void main(String[] args) throws IOException {
        read_utf_8();

    }

    /*
    使用InputStreamReader读取UTF-8格式的文件
     */
    private static void read_utf_8() throws IOException {
        //1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
        InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\environment\\java_project\\javase\\域名"), "UTF-8");
        //2.使用InputStreamReader对象中的方法read读取文件
        int len = 0;
        while ((len = 域名())!=-1){
            域名tln((char)len);
        }
        //3.释放资源
        域名e();
    }
}

转换文件编码

/*
练习:转换文件编码
    将GBK编码的文本文件,转换为UTF-8编码的文本文件
    
分析:
    1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
    2.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
    3.使用InputStreamReader对象中的方法read读取文件
    4.使用OutputStreamWriter对象中的方法write,把读取的数据写入到文件中
    5.释放资源
 */
import 域名.*;

public class Demo03Test {
    public static void main(String[] args) throws IOException {
        read_GBK();
    }

    private static void read_GBK() throws IOException {
        //1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
        InputStreamReader isr = new InputStreamReader(new FileInputStream("D:\\environment\\java_project\\javase\\域名"), "utf-8");
        //2.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\environment\\java_project\\javase\\域名"), "GBK");
        // 3.使用InputStreamReader对象中的方法read读取文件
        int len = 0;
        while ((len = 域名())!=-1){
            //4.使用OutputStreamWriter对象中的方法write,把读取的数据写入到文件中
            域名e(len);
        }
        //5.释放资源
        域名e();
        域名e();
    }
}

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。