zoukankan      html  css  js  c++  java
  • 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,所以它是最低级的,

    例:

    public class Main {   
        public static void main(String[] args) {   
               
            try {   
                FileInputStream fis=new FileInputStream("d://desktop//test.txt");   
                int i;   
                   
                try {   
                    while((i=fis.read()) != -1){   
                        System.out.println(i);   
                    }   
                    /*假設test.txt檔裡頭只有一個文字 "陳" ,且其編碼為 utf8  
                     * 輸出  
                     *  233  
                        153  
                        179  
                     */  
                } catch (IOException e) {   
                    // TODO Auto-generated catch block   
                    e.printStackTrace();   
                }   
                   
                   
            } catch (FileNotFoundException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
                       
        }   
    }

     InputStreamReader封裝了InputStream在里头,它以较高级的方式,一次读取一个一个字符,

    import java.io.*;   
    public class Main {   
        public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {   
                       
                FileInputStream fis=new FileInputStream("d://desktop//test.txt");   
                try {   
                    InputStreamReader isr=new InputStreamReader(fis,"utf8");   
                    int i;   
                    while((i=isr.read()) != -1){   
                        System.out.println((char)i);  //輸出  陳   
                    }   
                } catch (Exception e) {   
                    // TODO Auto-generated catch block   
                    e.printStackTrace();   
                }                                          
                       
        }   
    }  

    BufferedReader则是比InputStreamReader更高级,它封裝了StreamReader类,一次读取取一行的字符

    import java.io.*;   
    public class Main {   
        public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {   
                       
                FileInputStream fis=new FileInputStream("d://desktop//test.txt");   
                   
                try {   
                    InputStreamReader isr=new InputStreamReader(fis,"utf8");                   
                    BufferedReader br=new BufferedReader(isr);   
                       
                    String line;   
                    while((line=br.readLine()) != null){   
                        System.out.println(line);   
                    }   
                } catch (Exception e) {   
                    // TODO Auto-generated catch block   
                    e.printStackTrace();   
                }                                          
                       
        }   
    }  
  • 相关阅读:
    7月的尾巴,你是XXX
    戏说Android view 工作流程《下》
    “燕子”
    Android开机动画bootanimation.zip
    戏说Android view 工作流程《上》
    ViewController里已连接的IBOutlet为什么会是nil
    My first App "Encrypt Wheel" is Ready to Download!
    iOS开发中角色Role所产生的悲剧(未完)
    UIScrollView实现不全屏分页的小技巧
    Apple misunderstood my app,now my app status changed to “In Review”
  • 原文地址:https://www.cnblogs.com/liushao/p/6545615.html
Copyright © 2011-2022 走看看