s-day10--转换流_指定编码读取(InputStreamReader)
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
package com.inmind.transferstream02;
|
package com.inmind.transferstream02;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
9.编码表的介绍
|
9.编码表的介绍
|
||||||
编码:将字符转换成字节(类似加密)
|
编码:将字符转换成字节(类似加密)
|
||||||
@@ -25,8 +29,57 @@ utf-32 :使用4个字节表示字符,比较占用内存
|
|||||||
注意:
|
注意:
|
||||||
1.GBK针对国内的字符进行编解码操作,它前128个字符完全兼容ascii,一个中文占2个字节
|
1.GBK针对国内的字符进行编解码操作,它前128个字符完全兼容ascii,一个中文占2个字节
|
||||||
2.utf-8针对国际的字符进行编解码操作,它前128个字符完全兼容ascii,一个中文占3个字节
|
2.utf-8针对国际的字符进行编解码操作,它前128个字符完全兼容ascii,一个中文占3个字节
|
||||||
|
---------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
10.转换流_指定编码读取(InputStreamReader)
|
||||||
|
|
||||||
|
InputStreamReader是从字节流到字符流的桥梁:解码
|
||||||
|
|
||||||
|
构造方法:
|
||||||
|
InputStreamReader(InputStream in) 创建一个使用默认字符集的InputStreamReader。
|
||||||
|
InputStreamReader(InputStream in, String charsetName) 创建一个使用命名字符集的InputStreamReader。
|
||||||
|
|
||||||
|
常用方法:
|
||||||
|
close()
|
||||||
|
int read();
|
||||||
|
int read(char[] chars)
|
||||||
|
|
||||||
|
注意:转换流的作用是指定编码方式去读写文件
|
||||||
|
|
||||||
*/
|
*/
|
||||||
public class Demo06 {
|
public class Demo06 {
|
||||||
|
//使用转换流,指定utf-8编码方式去读取utf-8文件
|
||||||
|
public static void main(String[] args) throws Exception{
|
||||||
|
InputStreamReader isr = new InputStreamReader(
|
||||||
|
new FileInputStream("D:\\io_test\\file_utf8.txt")
|
||||||
|
,"utf-8");//当前未指定编码方式,那么该编码方式是utf-8
|
||||||
|
char[] chars = new char[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = isr.read(chars)) != -1) {
|
||||||
|
//读多少个字符就展示多少个字符
|
||||||
|
System.out.println(new String(chars,0,len));
|
||||||
|
}
|
||||||
|
|
||||||
|
isr.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//使用转换流,指定GBK编码方式去读取gbk文件
|
||||||
|
public static void method(String[] args) throws Exception{
|
||||||
|
InputStreamReader isr = new InputStreamReader(
|
||||||
|
new FileInputStream("D:\\io_test\\file_gbk2.txt")
|
||||||
|
, "GBK");
|
||||||
|
char[] chars = new char[1024];
|
||||||
|
int len;
|
||||||
|
while ((len = isr.read(chars)) != -1) {
|
||||||
|
//读多少个字符就展示多少个字符
|
||||||
|
System.out.println(new String(chars,0,len));
|
||||||
|
}
|
||||||
|
|
||||||
|
isr.close();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user