2016-07-24
InputStream
1,FileInputStream
因为字节流每次读取一个字节,而一个字节是无法完整的描述一个中文的,
所以会出现英文正常,中文乱码的情况。
package com.java1995; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class TestFileInputStream { public static void main(String[] args) { FileInputStream fis=null; int i=0; try { fis=new FileInputStream("C:\Users\Administrator\Desktop\hello.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { i=fis.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //当i==-1时文件到达结尾 while(i!=-1){ System.out.print((char)i); try { i=fis.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package com.java1995; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class TestFileInputStream { public static void main(String[] args) { FileInputStream fis=null; int i=0; try { fis=new FileInputStream("D:\workspace\java_io\hello.txt"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { i=fis.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //当i==-1时文件到达结尾 while(i!=-1){ System.out.print((char)i); try { i=fis.read(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
【参考资料】