第九章:IO流

来源:互联网 发布:两个表格相同数据合并 编辑:程序博客网 时间:2024/05/21 22:56

1. java.io.File类: 用来代表真实文件或目录的抽象表现形式。操作File类的实例,就相当于操作真实的文件。
 1) 属性:static final String separator 代表与操作系统相关的路径分隔符。
 2) 构造方法:File(String path)
 3) 常用方法:
    boolean exists();
    String getName();
    String getAbsolutePath();
    String getCanonicalPath();
    boolean isDirectory();
    boolean isFile();
   
    boolean createNewFile();
    boolean delete();
    boolean mkdir();
    boolean mkdirs();
   
    String[] list();
    File[] listFiles();

 

2. IO流类的分类: Stream流,数据通信的通道。
 1) 流向:输入流(源-->程序)、输出流(程序-->目标)
 2) 传输单位:字节流(以字节为传输单位)、字符流
 3) 功能:节点流(直接操作设备的流)、过滤流(处理流)-->对已经存在的流的包装(套接),提供更强大,更灵活,更高效的数据处理功能。
 
3. 四个基本抽象流类
 1) InputStream:字节输入流
    常用方法:
    int read() throws IOException //一次读取一个字节,返回读取到的字节数据的整数表示。如果遇到文件尾,返回-1。
    int read(byte[] buf) throws IOException //一次性读取buf.length个字节数据,读取到的数据存放在buf数组中,并返回读到的字节数。如果遇到文件尾,返回-1。
    int read(byte[] buf, int off, int len) throws IOException //
    void close() throws IOException //流操作完毕后,一定要关闭,以释放它所关联的系统资源。

    OutputStream:字节输出流
    常用方法:
    void write(int b) throws IOException //一次写出一个指定的字节。
    void write(byte[] buf) throws IOException //一次写出指定数组的字节数据。
    void write(byte[] buf, int off, int len) throws IOException
    void flush() throws IOException //刷新缓冲区,强制把缓冲区中的数据写出到目标设备。
    void close() throws IOException //流操作完毕后,一定要关闭,以释放它所关联的系统资源。

 2) Reader和Writer
    Reader:字符输入流
    常用方法:
    int read() throws IOException //一次读取一个字符,返回读取到的字符数据的整数表示。如果遇到文件尾,返回-1。
    int read(char[] cbuf) throws IOException
    int read(char[] cbuf, int off, int len) throws IOException
    void close() throws IOException
   
    Writer:字符输出流
    常用方法:
    void write(int b) throws IOException //一次写出一个指定的字符。
    void write(char[] cbuf) throws IOException //一次写出指定数组的字符数据。
    void write(char[] cbuf, int off, int len) throws IOException
    void write(String str) throws IOException
    void flush() throws IOException //刷新缓冲区,强制把缓冲区中的数据写出到目标设备。
    void close() throws IOException //流操作完毕后,一定要关闭,以释放它所关联的系统资源。

 

4. 文件流:专门用来操作目标设备上的文件。
  1) FileInputStream: 文件字节输入流
     构造方法:FileInputStream(String name) 和 FileInputStream(File file)
     常用方法:跟父类一样。
     FileOutputStream:文件字节输出流
     构造方法:覆盖模式                         append为true时,追加模式
               FileOutputStream(String name) 和 FileOutputStream(String name, boolean append)
               FileOutputStream(File file) 和 FileOutputStream(File file, boolean append)
     常用方法:跟父类一样。
     这两个流类适用于读写二进制文件,如:图像,音频,视频等文件。
    
  2) FileReader:文件字符输入流
     构造方法:FileReader(String name) 和 FileWriter(File file)
     常用方法:跟父类一样。
     FileWriter:文件字符输出流
     构造方法:覆盖模式                  append为true时,追加模式
               FileWriter(String name) 和 FileWriter(String name, boolean append)
               FileWriter(File file) 和 FileWriter(File file, boolean append)
     常用方法:跟父类一样。
     这两个流类适用于读写文本文件。

 

5. 缓冲流(属于处理流):针对四个抽象流类提供对应的缓冲流。缓冲流可以大大提高IO效率。
  1) BufferedInputStream: 对InputStream系列进行包装的缓冲流。
     构造方法:BufferedInputStream(InputStream in)
     常用方法:跟InputStream一样。
     BufferedOutputStream:对OutputStream系列进行包装的缓冲流。
     构造方法:BufferedOutputStream(OutputStream out)
     常用方法:跟OutputStream一样。
    
  2) BufferedReader: 对Reader系列进行包装的缓冲流。
     构造方法:BufferedReader(Reader in)
     常用方法:在Reader基础上新增了pubic String readLine() throws IOException
     BufferedWriter:对Writer系列进行包装的缓冲流。
     构造方法:BufferedWriter(Writer out)
     常用方法:在Writer基础上新增了public void newLine() throws IOException
    
6. 流类的常见使用方式:
    BufferedInputStream bps = null;
  BufferedOutputStream bos = null;
  byte[] buff = new byte[8192];
  try {
   bps = new BufferedInputStream(new FileInputStream(src));
   bos = new BufferedOutputStream(new FileOutputStream(desc));
   for(int count = -1;(count = bps.read(buff)) != -1;){
    bos.write(buff, 0, count);
   }
   bos.flush();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   if(null!=bps){
    try {    bps.close();  } catch (IOException e) {    e.printStackTrace(); }
   }
   if(null!=bos){
    try {    bos.close();  } catch (IOException e) {    e.printStackTrace(); }
   }
  }
   
7. 转换流:仅适用于,别人提供的是字节流,但明确知道传输只是字符。那么就可以转成字符操作方式。
   1) InputStreamReader: 字节输入流转换成字符输入流
      构造器:InputStreamReader(InputStream in);
   2) OutputStreamWriter: 字符输出流转换成字节输出流
      构造器:OutputStreamWriter(OutputStream out);

 

8. 数据流

 

9. 打印流:
   PrintStream:与其他输出流不同,PrintStream永远不会抛出IOException。System.out就是一个PrintStream对象
   PrintWriter
  
9. 对象流:
   ObjectOutputStream: 对象保存到目标设备。序列化。
   ObjectInputStream: 从目标设备中读取出对象。反序列化。
   可序列的对象对应的类必须实现java.io.Serializable接口,还要有一个序列版本ID:static final long serialVersionUID
   static属性和transient属性不会被序列化。
  
10. RandomAccessFile:随机访问文件类。可以随机从指定位置开始读写文件。

原创粉丝点击