Java日志第42天 2020.8.16
转换流
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
使用步骤:
1.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称
2.使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中
3.使用OutputStreamWriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中
4.释放资源
import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; public class Demo01OutputStreamWriter { public static void main(String[] args) throws IOException{ write_utf_8(); write_gbk(); } private static void write_gbk() throws IOException{ OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\Java\\Practice\\src\\Practice\\demo15\\GBK.txt"),"GBK"); osw.write("你好GBK"); osw.flush(); osw.close(); } private static void write_utf_8() throws IOException { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\Java\\Practice\\src\\Practice\\demo15\\UTF-8.txt"), "utf-8"); osw.write("你好UTF-8"); osw.flush(); osw.close(); } }
InputStreamReader
java.io.InputStreamReader extends Reader
InputStreamReader是字节通向字符流的桥梁,它使用指定的charset读取字节并将其解码为字符。
继承自父类的共性成员方法:
- int read():读取单个字符并返回
- int read(char[] cnuf):一次读取多个字符,并将字符读入数组
- void close():关闭该流并释放与之关联的所有资源
构造方法:
InputStreamReader(InputStream in):创建一个默认字符集的InputStreamReader
InputStreamReader(InputStream in, String charsetName):创建使用指定的字符集的InputStreamReader
*参数:
InputStream in:字节输入流,用来读取文件中保存的字节
String charsetName:指定的编码表名称,不区分大小写
使用步骤:
1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称
2.使用InputStreamReader中的方法read读取文件
3.释放资源
注意事项:
构造方法中指定的编码表名称要和文件的编码相同,否则会发生乱码
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class Demo02InputStreamReader { public static void main(String[] args) throws IOException { read_utf_8(); System.out.println(); read_gbk(); } private static void read_gbk() throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\Java\\Practice\\src\\Practice\\demo15\\GBK.txt"),"GBK"); int len=0; while((len = isr.read()) != -1){ System.out.print((char) len); }
isr.close(); } private static void read_utf_8() throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\Java\\Practice\\src\\Practice\\demo15\\UTF-8.txt")); char[] cbuf = new char[1024]; int len = 0; while((len = isr.read(cbuf)) != -1){ System.out.println(new String(cbuf, 0, len)); }
isr.close();
}
}
结果如下:
练习:文件编码的转换
将GBK格式的文件GBK.txt转换为UTF-8格式的文件UTF-8.txt
import java.io.*; public class Demo03Test { public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\Java\\Practice\\src\\Practice\\demo15\\GBK.txt"),"GBK"); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\Java\\Practice\\src\\Practice\\demo15\\UTF-8.txt"),"UTF-8"); char[] cbuf = new char[1024]; int len = 0; while((len = isr.read(cbuf))!=-1){ osw.write(cbuf, 0, len ); } osw.close(); isr.close(); } }