处理流之二:转换流
转换流提供了在字节流和字符流之间的转换
-
Java API提供了两个转换流:
-
InputStreamReader:将InputStream转换为Reader
-
OutputStreamWriter:将Writer转换为OutputStream
-
-
字节流中的数据都是字符时,转成字符流操作更高效。
-
很多时候我们使用转换流来处理文件乱码问题。实现编码和解码的功能。
InputStreamReader
InputStreamReader isr = null;
try {
FileInputStream fis = new FileInputStream("dbcp.txt");
isr = new InputStreamReader(fis, "utf-8");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1) {
String str = new String(cbuf, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-
实现将字节的输入流按指定字符集转换为字符的输入流。
-
需要和InputStream“套接”。
-
构造器
-
public InputStreamReader(InputStream in)
-
public InputSreamReader(InputStream in,String charsetName)
-
如:Reader isr= new InputStreamReader(System.in,”gbk”);
gbk指定字符集
OutputStreamWriter
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
FileInputStream fis = new FileInputStream("dbcp.txt");
FileOutputStream fos = new FileOutputStream("dbcp1.txt");
isr = new InputStreamReader(fis, "utf-8");
osw = new OutputStreamWriter(fos, "gbk");
char[] cbuf = new char[20];
int len;
while ((len = isr.read(cbuf)) != -1) {
String str = new String(cbuf, 0, len);
osw.write(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
-
实现将字符的输出流按指定字符集转换为字节的输出流。
-
需要和OutputStream“套接”。
-
构造器
-
public OutputStreamWriter(OutputStream out)
-
public OutputSreamWriter(OutputStream out,String charsetName)
-
补充:字符编码
编码表的由来
计算机只能识别二进制数据,早期由来是电信号。为了方便应用计算机,让它可以识别各个国家的文字。就将各个国家的文字用数字来表示,并一一对应,形成一张表。这就是编码表。
常见的编码表
-
ASCII:美国标准信息交换码。
- 用一个字节的7位可以表示。
-
ISO8859-1:拉丁码表。欧洲码表
- 用一个字节的8位表示。
-
GB2312:中国的中文编码表。最多两个字节编码所有字符
-
GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码
-
Unicode:国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
-
UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。
-
Unicode不完美,这里就有三个问题,一个是,我们已经知道,英文字母只用一个字节表示就够了,第二个问题是如何才能区别Unicode和ASCII?计算机怎么知道两个字节表示一个符号,而不是分别表示两个符号呢?第三个,如果和GBK等双字节编码方式一样,用最高位是1或0表示两个字节和一个字节,就少了很多值无法用于表示字符,不够表示所有字符。Unicode在很长一段时间内无法推广,直到互联网的出现。
-
面向传输的众多UTF(UCS Transfer Format)标准出现了,顾名思义,UTF-8就是每次8个位传输数据,而UTF-16就是每次16个位。这是为传输而设计的编码,并使编码无国界,这样就可以显示全世界上所有文化的字符了。
-
Unicode只是定义了一个庞大的、全球通用的字符集,并为每个字符规定了唯一确定的编号,具体存储成什么样的字节流,取决于字符编码方案。推荐的Unicode编码是UTF-8和UTF-16。
编码:字符串->字节数组
解码:字节数组->字符串
转换流的编码应用
-
可以将字符按指定编码格式存储
-
可以对文本数据按指定编码格式来解读
-
指定编码表的动作由构造器完成
处理流之三:标准输入、输出流(了解)
-
System.in和System.out分别代表了系统标准的输入和输出设备
-
默认输入设备是:键盘,输出设备是:显示器
-
System.in的类型是InputStream
-
System.out的类型是PrintStream,其是OutputStream的子类FilterOutputStream的子类
-
重定向:通过System类的setIn,setOut方法对默认设备进行改变。
-
public static void setIn(InputStreamin)
-
public static void setOut(PrintStreamout)
-
例题
从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
public static void main(String[] args) {
BufferedReader br = null;
try {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true) {
System.out.println("请输入字符串:");
String date = br.readLine();
if ("e".equalsIgnoreCase(date) || "exit".equalsIgnoreCase(date)) {
System.out.println("程序结束");
break;
}
String upperCase = date.toUpperCase();
System.out.println(upperCase);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}