转换流
-
转换流也是处理流的一种,提供了在字节流和字符流之间的转换
-
Java API提供的两个转换流
- InputStreamReader:将InputStream转换为Reader,即将一个字节的输入流转换为字符的输入流(解码)
package www.bh.c.iotest; import java.io.*; public class Test12 { public static void main(String[] args) { InputStreamReader iReader = null; try { FileInputStream fileInputStream = new FileInputStream("calss\\hello1.txt"); //使用系统默认的字符集 //InputStreamReader iReader = new InputStreamReader(fileInputStream); //指明使用的字符集,使用哪一个字符集具体看保存hello1.txt时使用的字符集 iReader = new InputStreamReader(fileInputStream, "utf-8"); char[] chars = new char[6]; int len; while ((len=iReader.read(chars))!=-1){ String s = new String(chars, 0, len); System.out.println(s); } } catch (IOException e) { e.printStackTrace(); } finally { if (iReader!=null){ try { iReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
- OutputStreamWriter:将Writer转换为OutputStream,即将一个字符的输出流转换为字节的输出流(编码)
package www.bh.c.iotest; import java.io.*; public class Test13 { public static void main(String[] args) { InputStreamReader isr = null; OutputStreamWriter osw = null; try { File file = new File("calss\\hello1.txt"); File file1 = new File("calss\\hello3.txt"); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file1); isr = new InputStreamReader(fis); osw = new OutputStreamWriter(fos); char[] c=new char[6]; int len; while ((len=isr.read(c))!=-1){ osw.write(c,0,len); } } 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(); } } } } }
-
字节流中的数据都是字符时,转成字符流操作更高效
-
可以使用转换流来处理文件乱码问题
字符编码
-
为了方便计算机识别各个国家的文字,将各个国家的文字用数字来表示,并一一对应,形成了一张表,这就是编码表
-
常见的编码表
- ASCII:美国标准信息交换码:用一个字节的7位可以表示
- ISO08859-1:拉丁码表,欧洲码表:用一个字节的8位表示
- GB2312:中国的中文编码表,最多两个字节编码所有字符
- GBK:中国的中文编码表升级,融合了更多的中文文字符号,做多两个字节编码(最高位用1表示由两个字节表示,用0表示由一个字节表示)
- Unicode:国际标准码,融合了世界所有的字符,为每一个字符分配唯一的字符吗,所有的文字都有两个字节来表示
- UTF-8(每次8个位传输数据):变长的编码方式,可用1-6个字节来表示一个字符
- 1个字节:0xxxxxxx
- 2个字节:110xxxxx 10xxxxxx
- 3个字节:1110xxxx 10xxxxxx 10xxxxxx
- 4个四节:11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
-
注:1.ANSI编码:通常指的是平台的默认编码
2.Unicode字符集只是定义了字符的集合和唯一编号,Unicode编码,则是对UTF-8、UTF-16等编码方案的统称而已,并不是具体的编码方案