第九章:IO流

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:隨機訪問文件類。可以隨機從指定位置開始讀寫文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章