Java Io流总结

来源:互联网 发布:淘宝挂钩 编辑:程序博客网 时间:2024/06/04 19:46

IO流部分框架图


字符流


字节流


字节流


InputStream | OutputStream

该抽象类是所有字节输入流的父类。

FileInputStream | FileOutputStream

从文件系统中按字节读取(写入)数据的输入(输出)流。

构造方法

FileInputStream(File file) 链接到一个实际的文件对象FileInputStream(String name) 传入的是一个实际文件的绝对路径FileOutputStream(File file) 创建一个文件输出流写入指定 File对象表示的文件。  FileOutputStream(File file, boolean append) append表示是否追加  FileOutputStream(String name) 创建一个文件输出流,用指定的名称写入文件。  FileOutputStream(String name, boolean append) append表示是否追加

常用方法

int read() 从这个输入流读取一个字节的数据,并返回。int read(byte[] b) 从输入流读取最多b.length 的字节数据,并存入字节数组b。  int read(byte[] b, int off, int len)  从off开始读取长度为len的字节数据放入到字节数组b中。void write(byte[] b) 输出一个字节数组的数据。  void write(byte[] b, int off, int len) 从off开始输出长度为len的字节数组b的数据。  void write(int b) 将指定的字节写入该文件输出流中。 

范例

        // 文件的复制        File file = new File("d:"+File.separator+"123.pdf");        FileInputStream fis = null;        FileOutputStream fos = null;        try {            fis = new FileInputStream(file);            fos = new FileOutputStream(new File("d:"+File.separator+"456.pdf"));            int len = 0;            byte[] arr = new byte[1024];            while((len = fis.read(arr)) != -1) {                fos.write(arr,0,len);            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            if(fis != null) {                try {                    fis.close();                    fis = null;                } catch (IOException e) {                    e.printStackTrace();                }            }            if(fos != null) {                try {                    fos.close();                    fos = null;                } catch (IOException e) {                    e.printStackTrace();                }            }        }

ByteArrayInputStream | ByteArrayOutputStream

内存流:利用内存流实现输入与输出的操作,不产生文件。是以内存为终端的。

构造方法

ByteArrayInputStream(byte[] buf) 使用 buf作为缓冲数组。  ByteArrayInputStream(byte[] buf, int offset, int length) 使用 buf作为缓冲数组。ByteArrayOutputStream() 创建一个新的字节数组输出流。  ByteArrayOutputStream(int size) 创建一个新的字节数组输出流,具有指定大小的缓冲容量,以字节为单位的。  

常用方法

int read() 从这个输入流读取下一个数据字节。  int read(byte[] b, int off, int len) 从输入流中的字节数组读到 len数据的字节。 String toString() 使用该平台的默认字符集将缓冲区的内容转换为字符串解码字节。  String toString(String charsetName) 将缓冲区的内容到一个字符串使用命名 charset解码字节。  void write(byte[] b, int off, int len) 写 len字节指定字节数组中的起始偏移 off这个字节数组输出流。  void write(int b) 将指定的字节写入该字节数组输出流中。  

范例

        // 内存流        ByteArrayInputStream bis = null;        ByteArrayOutputStream bos = null;        byte[] buf = "hello world!".getBytes();        bis = new ByteArrayInputStream(buf);        bos = new ByteArrayOutputStream();        int b = -1;        while ((b = bis.read())!= -1) {            bos.write(Character.toUpperCase(b));        }        System.out.println((bos.toString()));

ObjectInputStream | ObjectOutputStream

用来实现对象序列化。要序列化的对象必须实现 Serializable 接口。(使用了装饰设计模式来增强了字节输入流的功能。)

构造方法

ObjectInputStream(InputStream in) 需要传入一个基本的字节输入流。ObjectOutputStream(OutputStream out) 创建一个对象写入到指定的输出流。 

常用方法

Object readObject() 从对象输入流对象。   void writeObject(Object obj) 写入指定的对象的对象。 

范例

        // Book必须实现Serializable        List<Book> arr = new ArrayList<Book>();        arr.add(new Book("JAVA",78.3));        arr.add(new Book("PHP",73.3));        arr.add(new Book("ANDROID",22.3));        ObjectInputStream ois = null;        ObjectOutputStream oos = null;        File file = new File("D:"+File.separator+"demo.txt");        try {            oos = new ObjectOutputStream(new FileOutputStream(file));            ois = new ObjectInputStream(new FileInputStream(file));            oos.writeObject(arr);            List<Book> arr1 = (List<Book>) ois.readObject();            for (Book book : arr1) {                System.out.println(book);            }        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }finally {            if(ois != null) {                try {                    ois.close();                } catch (IOException e) {                    e.printStackTrace();                }                ois = null;            }            if(oos != null) {                try {                    oos.close();                } catch (IOException e) {                    e.printStackTrace();                }                oos = null;            }        }

FilterInputStream | FilterOutputStream

过滤器字节输入(输出)流,本类只是一个标准,不提供具体的装饰功能,其装饰功能由其子类完成。装饰设计模式:就是两个类继承同一个父类(或接口),被装饰的类提供一个基础的功能,进行装饰的类,来在外层嵌套一个新的功能,相当于组合功能。增加了代码的复用率,避免了当需要实现很多嵌套组合功能时带来的子类爆炸式增长的问题。其子类有:BufferedInputStream :为输入流提供一个缓冲的功能。CheckedInputStream : 为输入流提供一个校验功能。BufferedOutputStream : 为输出流提供一个缓冲的功能。PrintStream :打印各种数据值表示的功能。更多具体内容请参考API

BufferedInputStream | BufferedOutputStream

缓冲输入(输出)流,内部使用一个字节数组和一系列标记来实现了一个存储数据的缓冲区阵列。

构造方法

BufferedInputStream(InputStream in) 创建一个 BufferedInputStream和保存它的参数,输入流 in,供以后使用。  BufferedInputStream(InputStream in, int size) 创建一个具有指定的缓冲区大小 BufferedInputStream,并保存它的参数,输入流 in,供以后使用。  BufferedOutputStream(OutputStream out) 创建一个新的缓冲输出流,将数据写入到指定的基本输出流中。  BufferedOutputStream(OutputStream out, int size) 创建一个新的缓冲输出流,用指定的缓冲区大小写数据到指定的基本输出流中。参数2用来设置缓冲区大小,单位字节 

使用方法

使用方法与基本的字节输入输出流一样,带缓冲区的好处就是,在读取写入大文件时,能够有效的减少IO操作,从而节约时间提升效率。

PrintStream

打印流,提供各种数据的打印输出。

构造方法

PrintStream(OutputStream out) 创建一个新的打印流。

常用方法

void print(boolean b) 打印布尔值。  void print(char c) 打印一个字符。  void print(char[] s) 打印一个字符数组。  void print(double d) 打印一个双精度浮点数。  void print(float f) 打印一个浮点数。  void print(int i) 打印一个整数。  void print(long l) 打印一个长整数。  void print(Object obj) 打印一个对象。  void print(String s) 打印一个字符串。  void println() 通过编写行分隔符终止当前行。  void println(boolean x) 打印一个布尔值,然后终止该行。  void println(char x) 打印一个字符,然后终止行。  void println(char[] x) 打印一个字符数组,然后终止行。  void println(double x) 打印一个双,然后终止线。  void println(float x) 打印一个浮动,然后终止行。  void println(int x) 打印一个整数,然后终止该行。  void println(long x) 打印一个长,然后终止线。  void println(Object x) 打印一个对象,然后终止该行。  void println(String x) 打印一个字符串,然后终止该行。  提供了各种基本数据类型的打印,使用方便。

范例

        // 向控制台打印        PrintStream ps = System.out;        ps.println("122");        ps.println(122);        ps.print(1.22);        // 向文件中打印        PrintStream ps1 = null;        try {            ps1 = new PrintStream(new File("d:"+File.separator+"demo.txt"));            ps1.println("122");            ps1.println(122);            ps1.print(1.22);        } catch (FileNotFoundException e) {            e.printStackTrace();        }finally {            if(ps1 != null) {                ps1.close();            }        }

字符流


Reader | Writer

读取字符流的抽象类。这一类必须实现的唯一方法是读(char [],int,int)和()。大多数子类重写这里定义的一些方法,以提供更高的效率,额外的功能,或两者都有。用于写入到字符流的抽象类。这一类必须实现的唯一方法是写(char [],int,int),flush(),和()。大多数子类重写这里定义的一些方法,以提供更高的效率,额外的功能,或两者都有。

BufferedReader | BufferedWriter

缓冲字符输入输出流,提高了读取效率(减少了IO操作)

构造方法

BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。  BufferedReader(Reader in, int sz) 创建一个使用指定大小的输入缓冲区的缓冲字符输入流。 BufferedWriter(Writer out) 创建一个使用默认大小输出缓冲区的缓冲字符输出流。  BufferedWriter(Writer out, int sz) 创建一个新的缓冲字符输出流,该流使用给定大小的输出缓冲区。 

常用方法

int read() 读取单个字符。  int read(char[] cbuf, int off, int len) 将字符读入一个数组的一部分。  String readLine() 读一行文本。  void write(char[] cbuf, int off, int len) 写入一个字符数组的一部分。  void write(int c) 写一个字符。  void write(String s, int off, int len) 写入字符串的一部分。  void flush() 刷新缓冲区,写入到指定目标。  void newLine() 写行分隔符。  

CharArrayReader | CharArrayWriter

这个类实现了一个可以作为字符输入流使用的字符缓冲区。 这个类实现了一个可以作为字符输出流使用的字符缓冲区。当数据写入流时,缓冲区会自动增长。数据可以用tochararray()和tostring()检索。

构造方法

CharArrayReader(char[] buf) 创建从指定的字符数组的一个chararrayreader。  CharArrayReader(char[] buf, int offset, int length) 创建从指定的字符数组的一个chararrayreader。 

常用方法

int read() 读取单个字符。  int read(char[] b, int off, int len) 将字符读入一个数组的一部分。  boolean ready() 告诉是否该流已准备好阅读。  

字节流与字符流区别

1.字节流直接与终端数据交互,字符流需要将字符数据通过缓冲区处理后才能输出。2.有中文推荐使用字符流