转换流
我们要读取的文件aa.txt采用的是GBK的编码方式。
FileReader这个流是采用Idea默认的编码方式进行读取(UTF-8)。
两种编码不一致,那么就引发了乱码问题。
如果想要读取GBK编码的文件,我们可以指定一个编码方式去读取。
使用转换流,可以指定编码进行读取。
public class Demo01FileReader {
public static void main(String[] args) throws IOException {
//创建FileReader对象
Reader r = new FileReader("d:\\aa.txt");
//开始读取,一次读取一个字符
int i;
while ((i = r.read()) != -1) {
//输出
System.out.print((char)i);
}
//关流
r.close();
}
}
InputStreamReader
InputStreamReader是转换流, 可以指定编码读取数据, 可以将文件中的数据读取到Java程序中。
InputStreamReader的构造方法:
-
InputStreamReader(InputStream in):参数要传递字节输入流。 使用该构造方法创建的转换流对象会使用开发平台默认编码(utf-8)去读取。
-
InputStreamReader(InputStream in, String charsetName):第一个参数是字节输入流,第二个参数是指定的编码方式。 该构造方法创建的对象会采用指定的编码读取。
InputStreamReader读取的方法:
- InputStreamReader这个转换流是属于字符流的一种,是字符流,里面读取的方法和字符流读取的方法一模一样。
InputStreamReader使用步骤:
- 创建InputStreamReader转换流对象。
- 读取数据。
- 释放资源。
tips:
如果创建转换流对象时,指定的编码不存在,那么会报错
public class Demo02InputStreamReader {
public static void main(String[] args) throws IOException {
readUTF8();
}
/*
读取UTF-8编码的文件
*/
public static void readUTF8() throws IOException {
//创建InputStreamReader转换流对象,指定UTF-8的方式读取
InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\file02-utf8.txt"), "UTF-8");
//2. 读取数据。
int i;
while ((i = isr.read()) != -1) {
System.out.print((char)i);
}
//释放资源
isr.close();
}
/*
要求读取GBK编码的文件
*/
public static void readGBK() throws IOException {
//1. 创建InputStreamReader转换流对象,并指定GBK的方式读取
InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\file01-gbk.txt"), "gbk");
//2. 读取数据。
int i;
while ((i = isr.read()) != -1) {
System.out.print((char)i);
}
//释放资源
isr.close();
}
}
OutputStreamWriter
- OutputStreamWriter是转换流,用来写数据,可以将Java程序中的数据按照指定编码写到文件中
OutputStreamWriter的构造方法:
-
OutputStreamWriter(OutputStream out): 参数要传递一个字节输出流,使用该构造方法创建的对象会按照开发平台默认编码写数据。
-
OutputStreamWriter(OutputStream out, String charsetName): 第一个参数是字节输出流,第二个参数是编码方式。会采用指定编码去写数据。
OutputStreamWriter写数据的方法:
- OutputStreamWriter是字符流的一种,里面写数据的方法和字符流写数据的方法一模一样
OutputStreamWriter使用步骤:
- 创建OutputStreamWriter转换流对象
- 调用write方法写数据。
- 刷新
- 释放资源
public class Demo03OutputStreamWriter {
public static void main(String[] args) throws IOException {
//writeGBK();
writeUTF8();
}
/*
定义方法,向文件中写UTF-8的数据
*/
public static void writeUTF8() throws IOException {
//创建OutputStreamWriter转换流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\file04-utf8.txt"), "utf-8");
//2. 调用write方法写数据
osw.write("你好");
//3. 刷新
osw.flush();
//4. 关流
osw.close();
}
/*
定义方法,向文件中写GBK的数据
*/
public static void writeGBK() throws IOException {
//1. 创建OutputStreamWriter转换流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\file03-gbk.txt"), "gbk");
//2. 调用write方法写数据
osw.write("你好");
//3. 刷新
osw.flush();
//4. 关流
osw.close();
}
}
例题
将GBK编码的文本文件,转换为UTF-8编码的文本文件。
按照GBK的方式读取源文件,按照UTF-8的方式将读取到的内容写到一个新的文件。
步骤:
- 创建InputStreamReader转换流,指定GBK的方式读取。
- 创建OutputStreamWriter转换流, 指定UTF-8的方式写
- 读取数据, 每读取到数据,就将读取到的数据写到目的地文件。
- 释放资源
public class Demo04Test {
public static void main(String[] args) throws IOException {
//1. 创建InputStreamReader转换流,指定GBK的方式读取。
InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\file05-gbk.txt"), "gbk");
//2. 创建OutputStreamWriter转换流, 指定UTF-8的方式写
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\file06-utf8.txt"), "utf-8");
//3. 读取数据, 每读取到数据,就将读取到的数据写到目的地文件。
int i;
while ((i = isr.read()) != -1) {
//将读取到的数据写到目的地文件
osw.write(i);
}
//4. 释放资源
osw.close();
isr.close();
}
}
来源:https://blog.csdn.net/weixin_45365797/article/details/99232339