zoukankan      html  css  js  c++  java
  • InputStream、InputStreamReader、BufferedReader

    (本文非原创,转自http://blog.csdn.net/double2hao/article/details/50321219)

      最进在梳理java的文件读取,读取文件,当然要理解当中几个重要的IO流,下面是转自一片比较清晰的博客。

     

    一.java IO流

      1.处理字节流的抽象类:  InputStream、OutputStream

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

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

      2.处理字符流的抽象类:InputStreamReader  OutputStreamWriter

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

     

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

      3.BufferedReader BufferedWriter

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

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

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

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

     

        代码:

          public class FileIo {
        public static void main(String[] args) throws IOException {
            //1.按字节读写文件,用FileInputStream、FileOutputStream
            String path = "D:\iotest.txt";
            File file = new File(path);
            InputStream in;
            //每次只读一个字节
            try {
                System.out.println("以字节为单位,每次读取一个字节");
                in = new FileInputStream(file);
                int c;
                while((c=in.read())!=-1){
                    if(c!=13&&c!=10){    // 回车的Ascall码是10 , 换行是Ascall码是13,不现实挥着换行
                        System.out.println((char)c);
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            //每次读多个字节
            try {
                System.out.println("以字节为单位,每次读取多个字节");
                in = new FileInputStream(file);
                byte[] bytes = new byte[10];   //每次读是个字节,放到数组里
                int c;
                while((c=in.read(bytes))!=-1){
                    System.out.println(Arrays.toString(bytes));
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            
            
            //2.按字符读取文件,用InputStreamReader,OutputStreamReader
            Reader reader = null;
            try {//每次读取一个字符
                System.out.println("以字符为单位读取文件,每次读取一个字符");
                in = new FileInputStream(file);
                reader = new InputStreamReader(in);
                int c;
                while((c=reader.read())!=-1){
                    if(c!=13&&c!=10){    // 回车的Ascall码是10 , 换行是Ascall码是13,不现实挥着换行
                        System.out.println((char)c);
                    }                
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            
            try {//每次读取多个字符
                System.out.println("以字符为单位读取文件,每次读取一个字符");
                in = new FileInputStream(file);
                reader = new InputStreamReader(in);
                int c;
                char[] chars = new char[5];
                while((c=reader.read(chars))!=-1){
                    System.out.println(Arrays.toString(chars));
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            
            //3.按行读取文件
            try {
                System.out.println("按行读取文件内容");
                in = new FileInputStream(file);
                Reader reader2 = new InputStreamReader(in);
                BufferedReader bufferedReader = new BufferedReader(reader2);
                String line;
                while((line=bufferedReader.readLine())!=null){
                    System.out.println(line);
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

        

    12.4  转换流--OutputStreamWriter类与InputStreamReader类

      整个IO包实际上分为字节流和字符流,但是除了这两个流之外,还存在一组字节流-字符流的转换类。

      OutputStreamWriter:是Writer的子类,将输出的字符流变为字节流,即将一个字符流的输出对象变为字节流输出对象。

      InputStreamReader:是Reader的子类,将输入的字节流变为字符流,即将一个字节流的输入对象变为字符流的输入对象。

      以文件操作为例,内存中的字符数据需要通过OutputStreamWriter变为字节流才能保存在文件中,读取时需要将读入的字节流通过InputStreamReader变为字符  流。

     

     
  • 相关阅读:
    SendInput模拟键盘输入的问题 <转>
    tinyMce3.21 使用随笔
    ORM的一些思考
    VS2008 , 1330 , 数字签名错误.
    .Net Reflector 工具 过期后使用小工具。
    就当是一个新的设计模式!
    OpenSUSE 11 下 Mono 2 开发笔记。
    转:)CNBlogs引用第三方组(控)件明细^_^
    DBLinq ,没实现 Log !
    DotNet 的一些知识点
  • 原文地址:https://www.cnblogs.com/Zchaowu/p/7353348.html
Copyright © 2011-2022 走看看