转换流

1_字符编码和字符集

 

 

 记住:

(1)ASCII表,GBK,UTF-8

(2)编码表:生活中的文字和计算机中二进制的对应规则

==========================================================================================================

2_编码引出的问题_FileReader读取G

 

 

 ====================================================================================================

3_转换流的原理

 

 ======================================================================================================================

4_OutputStreamWriter介绍&代码实现

Demo02OutputStreamWriter.java

package com.itheima.demo03.ReverseStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/*
java.io.OutputStreamWriter 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 Demo02OutputStreamWriter {
public static void main(String[] args) throws IOException {
//write_utf_8();
write_gbk();
}

/*
使用转换流OutputStreamWriter写GBK格式的文件
*/
private static void write_gbk() throws IOException {
//1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("10_IO\gbk.txt"),"GBK");
//2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
osw.write("你好");
//3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
osw.flush();
//4.释放资源
osw.close();
}

/*
使用转换流OutputStreamWriter写UTF-8格式的文件
*/
private static void write_utf_8() throws IOException {
//1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
//OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("10_IO\utf_8.txt"),"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("10_IO\utf_8.txt"));//不指定默认使用UTF-8
//2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
osw.write("你好");
//3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
osw.flush();
//4.释放资源
osw.close();
}
}

==============================================================================================================

5_InputStreamReader介绍&代码实现

 Demo03InputStreamReader.java

package com.itheima.demo03.ReverseStream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

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

继承自父类的共性成员方法:
int read() 读取单个字符并返回。
int read(char[] cbuf)一次读取多个字符,将字符读入数组。
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 Demo03InputStreamReader {
public static void main(String[] args) throws IOException {
//read_utf_8();
read_gbk();
}


/*
使用InputStreamReader读取GBK格式的文件
*/
private static void read_gbk() throws IOException {
//1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
//InputStreamReader isr = new InputStreamReader(new FileInputStream("10_IO\gbk.txt"),"UTF-8");//???
InputStreamReader isr = new InputStreamReader(new FileInputStream("10_IO\gbk.txt"),"GBK");//你好

//2.使用InputStreamReader对象中的方法read读取文件
int len = 0;
while((len = isr.read())!=-1){
System.out.println((char)len);
}
//3.释放资源
isr.close();
}

/*
使用InputStreamReader读取UTF-8格式的文件
*/
private static void read_utf_8() throws IOException {
//1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
//InputStreamReader isr = new InputStreamReader(new FileInputStream("10_IO\utf_8.txt"),"UTF-8");
InputStreamReader isr = new InputStreamReader(new FileInputStream("10_IO\utf_8.txt"));//不指定默认使用UTF-8
//2.使用InputStreamReader对象中的方法read读取文件
int len = 0;
while((len = isr.read())!=-1){
System.out.println((char)len);
}
//3.释放资源
isr.close();
}
}

=========================================================================================

6_练习_转换文件编码

Demo04Test.java

package com.itheima.demo03.ReverseStream;

import java.io.*;

/*
练习:转换文件编码
将GBK编码的文本文件,转换为UTF-8编码的文本文件。

分析:
1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
2.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
3.使用InputStreamReader对象中的方法read读取文件
4.使用OutputStreamWriter对象中的方法write,把读取的数据写入到文件中
5.释放资源
*/
public class Demo04Test {
public static void main(String[] args) throws IOException {
//1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
InputStreamReader isr = new InputStreamReader(new FileInputStream("10_IO\我是GBK格式的文本.txt"),"GBK");
//2.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("10_IO\我是utf_8格式的文件.txt"),"UTF-8");
//3.使用InputStreamReader对象中的方法read读取文件
int len = 0;
while((len = isr.read())!=-1){
//4.使用OutputStreamWriter对象中的方法write,把读取的数据写入到文件中
osw.write(len);
}
//5.释放资源
osw.close();
isr.close();
}
}

原文地址:https://www.cnblogs.com/curedfisher/p/12487034.html