1. 什么时候使用转换流
- 什么是转换流?
- 数据源是字节流,或者底层框架返回的字节流数据,但是操作的数据是,纯文本数据,可以使用转换流。
- 操作纯文本需要具体的编码指定时,字节流字符流处理不了编码问题,只有转换流是为了解决字符流编码问题。
2. 方法介绍
- 主要进行编码的转换,用来解决字符流读写乱码的问题。
String charsetName
是指定字符集(即编码)。
1.OutputStreamWriter: 写出:
1.1 OutputStreamWriter(OutputStream out,String charsetName);
1.2 OutputStreamWriter(OutputStream out);
2.InputStreamReader: 读取:
2.1 InputStreamReader(InputStream in)
2.2 InputStreamReader(InputStream in,String charsetName);
2.1 将控制台输入写入文件中
- 键盘输入信息,保存到磁盘中。
- 键盘输入的是
System.in
字节流。 - 遇到指定的字符停止输入。
public static void main(String[] args) throws IOException {
try( BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw =
new BufferedWriter(new FileWriter("D://a.txt"));){
String line;
while((line=br.readLine())!=null){
if ("ok".equals(line)){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
}
}
2.2 将文件中内容显示到控制台
- 将文件中的内容显示到控制台上。
public static void main(String[] args) throws IOException {
try( BufferedReader br = new BufferedReader(new FileReader("D://a.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))){
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
}
}
3. 常见编码表
- 编码表最主要的作用,就是随着计算机不断的发展,解决各个国家因字符集不同而出现的乱码问题。
编码 | 说明 | 编码范围 | 字节量 |
---|
ASC-II | 英文,标点,基本指令 | 0到127 | 单字节 |
ISO-8859-1 | ASC-II扩展,西欧字符 | 128~255 | 单字节 |
ANSI | ANSI是一种字符编码规范,设计为了使计算机支持更多语言,英文用单字节编码,其他字符用双字节编码CJK表示亚洲字符编码的统称(中日韩) | | |
GBK | 中国国标码 | 最大65535 | 英文使用单字节,中文使用双字节 |
UNICODE | 统一码Java的char类型字符 就采用Unicode编码 | 100万+编码位,分为常用字符表,生僻字符表等,我们只是用常用表 | 常用字符表所有字符都采用双字节 |
UTF-8 | Unicode传输格式Unicode Transformations Format为了解决Unicode英文字符字节量翻倍的问题,提出的一种变长的编码格式 | UTF是unicode传输格式 | 英文单字节,某些字符双字节,中文三字节,一些特殊符号四字节 |
3.1 编码字节测试
- 测试编码占字节。
public static void main(String[] args) throws UnsupportedEncodingException {
String s= "你好8";
System.out.println(s.getBytes().length);
System.out.println(s.getBytes("UTF-8").length);
System.out.println(s.getBytes("GBK").length);
System.out.println(s.getBytes("unicode").length);
System.out.println(s.getBytes("iso-8859-1").length);
}
4. 指定编码读取
4.1 转换流–读
- 当需要指定编码读取的时候就需要转换流 。
- 如果你设置与打开一致则不需要。
- 乱码通常是汉字乱码, 字母和数字不会乱码,因为早期编码集都将字母与数字收录在其中。
- 一定要注意:同编码,同解码。
- 即:数据源如果是
UTF-8
,就需要使用UTF-8
去读。
public static void main(String[] args) {
File file = new File("D://a.txt");
try {
method1(file);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void method1(File file) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
String line;
while((line=br.readLine())!=null){
System.out.println(line);
}
}
4.2 转换流–写
- 往文件中写入数据。
- 字节流处理不了编码问题,转换流是为了解决字符流编码问题。
public static void main(String[] args) {
File file = new File("D://a.txt");
try {
method2(file);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void method2(File file) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"ISO-8859-1"));
bw.write("你好,中国");
bw.write("123");
bw.write("io");
bw.close();
}
}