IO流总结

字节流
* 字节输入流 读取数据 InputStream
* 字节输出流 写出数据 OutputStream
* 字符流
* 字符输入流 读取数据 Reader
* 字符输出流 写出数据 Writer

注意:每种基类的子类都是以父类名作为后缀名。
* XxxOutputStream
* XxxInputStream
* XxxReader
* XxxWriter
* 查看FileOutputStream的构造方法:
* FileOutputStream(File file) 
* FileOutputStream(String name)

 

 public void write(int b):写一个字节
* public void write(byte[] b):写一个字节数组
* public void write(byte[] b,int off,int len):写一个字节数组的一部分
*/

 

读取数据的方式:
* A:int read():一次读取一个字节

* B:int read(byte[] b):一次读取一个字节数组,此方法试图从文件中读取b.length个字节,并把读取的字节存放到数组b中

C:int read(byte[] b,int off,int len) 此方法试图读取len个字节,并将读取的字节存放在数组b中,off是首字节在数组中的存放位置。这两个方法都返回读取的字节个数,如果达到文件的末尾,方法返回-1

这种类被称为:缓冲区类(高效类)
* 写数据:BufferedOutputStream
* 读数据:BufferedInputStream

编码和解码

 String(byte[] bytes, String charsetName):通过指定的字符集解码字节数组
* byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组

字符转换流:把字节流转换为字符流

InputStreamReader(InputStream is):用默认的编码读取数据
* InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
*/

* 字符缓冲流的特殊方法:
* BufferedWriter:
* public void newLine():根据系统来决定换行符
* BufferedReader:
* public String readLine():一次读取一行数据
* 包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
*/

原文地址:https://www.cnblogs.com/cn-chy-com/p/7919820.html