1. 程式人生 > >I/O流基礎整理

I/O流基礎整理

I/O即資料的輸入輸出。點這裡

而流式分為位元組流和字元流
在這裡插入圖片描述

1 首先認識下 位元組流
點這裡
InputStream是輸入位元組流的超類

public abstract class InputStream implements Closeable {
 		public abstract int read() throws IOException;
	    public int read(byte b[]) throws IOException {
        	return read(b, 0, b.length);
        }
        public int read(byte b[], int off, int len) throws IOException {
	//實現細節請看api
		}
		public long skip(long n) throws IOException {}//忽略輸入流中的n個位元組
	    public int available() throws IOException {
      		  return 0;} 	//返回輸入流中可以讀取的位元組數。
		public void close() throws IOException {}
	    public synchronized void mark(int readlimit) {}//在流中標記一個位置,要與markSupported()連用
	    public synchronized void reset() throws IOException {
        	throw new IOException("mark/reset not supported");
        }
	    public boolean markSupported() {
       		 return false;
        }		
    }

其子類

在這裡插入圖片描述

FileInputStream 把一個檔案作為InputStream,實現對檔案的讀取操作
ByteArrayInputStream:把記憶體中的一個緩衝區作為InputStream使用
StringBufferInputStream:把一個String物件作為InputStream
PipedInputStream:實現了pipe的概念,主要線上程中使用
SequenceInputStream:把多個InputStream合併為一個InputStream

OutputStream是輸出位元組流的超類
方法:

 public abstract void write(int b) throws IOException;
  public void write(byte b[]) throws IOException {
        write(b, 0, b.length);
    }
     public void write(byte b[], int off, int len) throws IOException {}
      public void flush() throws IOException {
    }
     public void close() throws IOException {
    }

其子類

FileOutputStream
ByteArrayOutputStream
FilterOutputStream
BufferedOutputStream
DataOutputStream

FileInputStream/FileOutputStream
構造方法(檔案路徑,檔案物件)
FileInputStream

 public FileInputStream(String name)
 public FileInputStream(File file)

FileOutputStream
建構函式

    FileOutputStream(String filePath)
	FileOutputStream(File fileObj) 
	FileOutputStream(String filePath,boolean append)

對比於FileInputStream 增加了FileOutputStream(String filePath,boolean append)建構函式,表示是覆蓋方式(false),還是追加方式(true)

例:

	FileInputStream fin = new FileInputStream("hello.txt");
            byte[] buffer = new byte[1024];
            int x = fin.read(buffer,0,buffer.length);
            String str = new String(buffer);
            System.out.println(str);
            System.out.println(x);
            fin.close();

我們可以選用read中的內容
1 一個位元組一個位元組讀取
2 一個固定位元組陣列的大小讀取,如1024
3 跟檔案內容大小一樣的位元組陣列讀取

ByteArrayInputStream/ByteArrayOutputStream
由於定義讀取的位元組陣列的大小不確定性,因此我們選用動態位元組陣列流
由於ByteArrayInputStream 中定義了一個byte buf[];當我們的陣列大小不夠時會進行自動擴充。

ByteArrayOutputStream也會首先建立一個預設的容器量, capacity = 32 = 32b,
每次在寫的時候都會去比對capacity是否還夠用, 如果不夠用的時候, 就重新建立buf的容量, 一直等到內容寫完,這些資料都會一直處於記憶體中.

BufferedInputStream/BufferedOutputStream
快取位元組流

private static int DEFAULT_BUFFER_SIZE = 8192;
protected volatile byte buf[];
protected int pos;
protected int count;
public BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in, int size)
public synchronized int read()
public synchronized void mark(int readlimit)
public synchronized void reset()

BufferedOutputStream會首先建立一個預設的容器量, capacity = 8192 = 8KB,
每次在寫的時候都會去比對capacity是否還夠用, 如果不夠用的時候, 就flushBuffer(),
把buf中的資料寫入對應的outputStream中, 然後將buf清空, 一直這樣等到把內容寫完.
在這過程中主要起到了一個數據緩衝的功能.

DataInputStream/DataOutputStream
可以指定基本資料的形式讀取
例如:readChar(),readInt()等

 public DataOutputStream(OutputStream out)
 public synchronized void write(byte b[], int off, int len)
 public final void writeBoolean(boolean v)
 public final void writeByte(int v)
 public final void writeShort(int v)
 public final void writeInt(int v) 
 public final void writeDouble(double v)

字元流
位元組流只能按位元組讀取,而字元流可以對資料按字元讀取/寫入
Reader/Writer 是字元流的超類
在這裡插入圖片描述
在這裡插入圖片描述
Reader:

int read():從輸入流中讀取單個字元,返回所讀取的字元資料
int read(char[]ch):從輸入流中最多讀取ch.length個字元的資料,並將其儲存在字元陣列ch中,返回實際讀取的字元數。
int read(char[]ch,int off,int len):從輸入流中最多讀取len個字元的資料,並將其儲存在字元陣列ch中,放入陣列時,從陣列的off位置開始,返回實際讀取的字元數。

FileReader/FileWriter
它們的功能與前面講過的FileInputStream/FileOutputStream基本類似,只是前者是基於字元流後者是基於位元組流,它們都能從檔案中讀取或者寫入資料

public class FileReader extends InputStreamReader {
    public FileReader(String fileName) throws FileNotFoundException {
        super(new FileInputStream(fileName));
    }
    public FileReader(File file) throws FileNotFoundException {
        super(new FileInputStream(file));
    }
    public FileReader(FileDescriptor fd) {
        super(new FileInputStream(fd));
    }

}

InputStreamReader /OutputStreamWriter

輸入/輸出體系中提供了兩個轉換流InputStreamReader和OutputStreamWriter,這兩個轉換流用於實現將位元組流轉換成字元流,其中InputStreamReader將位元組輸入流轉換成字元輸入流,OutputStreamWriter將位元組輸出流轉換成字元輸出流。

BufferedReader和BufferedWriter
BufferedReader和BufferedWriter類各擁有8192字元的緩衝區。當BufferedReader在讀取文字檔案時,會先儘量從檔案中讀入字元資料並置入緩衝區,而之後若使用read()方法,會先從緩衝區中進行讀取。如果緩衝區資料不足,才會再從檔案中讀取,使用BufferedWriter時,寫入的資料並不會先輸出到目的地,而是先儲存至緩衝區中。如果緩衝區中的資料滿了,才會一次對目的地進行寫出。如果要在緩衝區中資料未滿的情況下將資料寫入到目的地,需要使用flush()方法,將資料重新整理一下。
bufferedReader 還提供了readLine按行讀取。