InputStream是字节流,InputStreamReader将字节流转成字符流,BufferedReader将字符流转成字符缓冲,开始读字符.

1.InputStream、OutputStream

处理字节流的抽象类

InputStream 是字节输入流的所有类的超类,一般我们使用它的子类,如FileInputStream等.

OutputStream是字节输出流的所有类的超类,一般我们使用它的子类,如FileOutputStream等.

 

2.InputStreamReader  OutputStreamWriter

处理字符流的抽象类

InputStreamReader 是字节流通向字符流的桥梁,它将字节流转换为字符流.

OutputStreamWriter是字符流通向字节流的桥梁,它将字符流转换为字节流.

 

3.BufferedReader BufferedWriter

BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,readLine读取一个文本行,

从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。

BufferedWriter  由Writer 类扩展而来,提供通用的缓冲方式文本写入, newLine使用平台自己的行分隔符,

将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

InputStream能从來源处读取一個一個byte,
所以它是最低级的,
例:

 1 import java.io.*;     
 2 public class Main {     
 3     public static void main(String[] args) {     
 4              
 5         try {     
 6             FileInputStream fis=new FileInputStream("d://desktop//test.txt");     
 7             int i;     
 8                  
 9             try {     
10                 while((i=fis.read()) != -1){     
11                     System.out.println(i);     
12                 }     
13                 /*假設test.txt檔裡頭只有一個文字 "陳" ,且其編碼為 utf8   
14                  * 輸出   
15                  *  233   
16                     153   
17                     179   
18                  */    
19             } catch (IOException e) {     
20                 // TODO Auto-generated catch block     
21                 e.printStackTrace();     
22             }     
23                  
24                  
25         } catch (FileNotFoundException e) {     
26             // TODO Auto-generated catch block     
27             e.printStackTrace();     
28         }     
29                      
30     }     
31 }   

 3.1 InputStreamReader

 它封裝了InputStream在里头,
 一次读取一个一个字符,
 下例是假设有一个编码为utf8的文档,
 其內容只有一个中文字 "陳"

 1 import java.io.*;     
 2 public class Main {     
 3     public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {     
 4                      
 5             FileInputStream fis=new FileInputStream("d://desktop//test.txt");     
 6             try {     
 7                 InputStreamReader isr=new InputStreamReader(fis,"utf8");     
 8                 int i;     
 9                 while((i=isr.read()) != -1){     
10                     System.out.println((char)i);  //輸出  陳     
11                 }     
12             } catch (Exception e) {     
13                 // TODO Auto-generated catch block     
14                 e.printStackTrace();     
15             }                                            
16                      
17     }     
18 }    

 

3.2 BufferedReader

BufferedReader则是比InputStreamReader提供更多的api,它封裝了StreamReader类,一次读取取一行的字符

 1 import java.io.*;     
 2 public class Main {     
 3     public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {     
 4                      
 5             FileInputStream fis=new FileInputStream("d://desktop//test.txt");     
 6                  
 7             try {     
 8                 InputStreamReader isr=new InputStreamReader(fis,"utf8");                     
 9                 BufferedReader br=new BufferedReader(isr);     
10                      
11                 String line;     
12                 while((line=br.readLine()) != null){     
13                     System.out.println(line);     
14                 }     
15             } catch (Exception e) {     
16                 // TODO Auto-generated catch block     
17                 e.printStackTrace();     
18             }                                            
19                      
20     }     
21 }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
猜你喜欢
  • 2022-12-23
  • 2022-02-01
  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2021-06-19
相关资源
相似解决方案