• Java InputStream和Reader


    1. 字节流和字符流的区别:

      字节流操作的数据单元是8位的字节,而字符流操作的数据单元是16位的字符。

    2. 节点流和处理流的区别:

      可以从/向一个特定的IO设备(如磁盘、网络)读、写数据的流,称为节点流,也被称为低级流。

      处理流则用于对一个已经存在的流进行连接或封装,通过封装后的流实现数据读写功能。

    3. InputStream 和Reader

      InputStream 和Reader 是所有输入流的抽象基类,本身不能创建实例或执行输入,但成为所有输入流的模板,它们的方法是所有输入流都可使用的方法。

      InputStream包含如下方法:

    // 从输入流种读取单个字节,返回所读取的字节数据
    int read();    
     // 从输入流种最多读取b.length个字节的数据,并存储在字节数组b种,返回实际读取的字节数
    int read( byte b[]);
     // 从输入流中最多读取len个字符的数据,并将其存储在字符数组buff中,并不是从数组起点开始,而是从off位置开始
    int read( byte b[], int off, int len)

      Reader包含如下方法:

    // 从输入流中读取单个字符,返回所读取的字符数据
    int read();
     // 从输入流中最多读取cbuf.length个字符数据,并存储在cbuf中,返回实际读取的字符数
    int read( char cbuf[]);
     // 从输入流中最多读取cbuf.length个字符数据,并存储在cbuf中,从off开始存储,返回实际读取的字符数
    int read( char cbuf [], int off, int len);

      InputStream和Reader是抽象基类,本身不能创建实例,但它们分别有一个用于读取文件的输入流: FileInputStream, FileReader

    public  class FileInputStreamTest {
         public  static  void main(String[] args) {
            InputStream fis = null ;
             try {
                fis = new FileInputStream("D:/rding/testfile2/file.txt" );
                 byte [] bbuf = new  byte [1024 ];
                 int hasRead = 0 ;
                 while ((hasRead = fis.read(bbuf)) > 0 ) {
                    System.out.println( new String(bbuf, 0 , hasRead));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                 try {
                     if (fis != null ) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    }
    public  class FileReaderTest {
         public  static  void main(String[] args) {
            Reader fr = null ;
             try {
                fr = new FileReader("D:/rding/testfile2/file.txt" );
                 char [] cbuf = new  char [32 ];
                 int hasRead = 0 ;
                 while ((hasRead = fr.read(cbuf)) > 0 ) {
                    System.out.print( new String(cbuf, 0 , hasRead));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                 if (fr != null ) {
                     try {
                        fr.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

     4. OutputStream和Writer

      OutputStream 和 Writer都有这三个方法:

      

      

        

        

      

  • 相关阅读:
    带有通配符的字符串匹配算法-C/C++
    二叉树的遍历(一)
    What is "dll"?
    MFC中的CRect(区域)
    枚举顶级窗口函数EnumWindows和它的回调函数的使用!
    一个鼠标指针有关的启发(存在问题,可以参考一下 呵呵)
    9针串口
    About “PostMessage” &"SendMessage"
    Pocket pc模拟器与PC机传输文件的方法
    overlapped I/O的学习笔记
  • 原文地址:https://www.cnblogs.com/lfdingye/p/7597622.html
Copyright © 2020-2023  润新知