JAVA-流

1、特点

  1.输入流  定义:数据从数据源(磁盘文件、网络)流向程序(内存)

    表示:I   in

  2.输出流  定义:数据从程序(内存)流向目的地(磁盘文件、网络)

    表示:O  out

2、字节流  

  1.输出流:FileOutputStream

    构造: new FileOutputStream(“文件全路径”)      覆盖方法

        new FileOutputStream(File对象)        覆盖方法

        new FileOutputStream(“文件全路径”,true)   追加方法

        new FileOutputStream(File,true)       追加方法

    用法:   write(byte[])  写入字节数组

        close()    关闭流,释放资源

  2.输入流:FileInputStream

    构造: new FileInputStream(“文件全路径”)

        new FileInputStream(File对象)

    用法: read(byte[])    读数据到byte[],返回读到数据的长度  

                   分段按顺序读取    

                   读完或读取失败返回-1  

                   while((i=in.read(b))>0){组装数组}

        close()  关闭流

3、字符流

  1.FileWriter  写

    覆盖方式写入   new FileWriter(“文件全路径”) 

             new FileWriter(File对象)

    追加方式写入   new FileWriter(“文件全路径”,true)

             new FileWriter(File对象,true)

    用法  write(String)  写入字符串

        close()  关闭流  

  2.FileReader  读

    构造  new FileReader(“文件全路径”)

        new FileReader(File对象)

    用法  read(char[])  读取字符数据,具体方法和字节流类似

        close()  关闭流

3、带缓存的字符流

  1.BufferedWriter

    构造:  new BufferedWriter(Write实现类对象)

    用法:  Write(String)  写入字符串

         close()     关闭流

  2.BufferedReader

    构造:  new BufferedReader(Reader实现类的对象)

    用法:  ReadLine  按行读取,返回String

原文地址:https://www.cnblogs.com/jingfengling/p/5908650.html