(1)字節流java
輸出字節流:OutputStream:字節寫入流抽象類數組
|--->FileOutputStream:緩存
字節寫入流
|--->BufferedOutputStream:
字節寫入流緩衝區
app
輸入字節流:InputStream:字節讀取流抽象類eclipse
|--->FileInputStream:
字節讀取流
|--->BufferedInputStream:
字節讀取流緩衝區
(2)字符流
輸出字符流:Writer:字符寫入流的抽象
|--->FileWriter:
字符寫入流
|--->BufferedWriter:
字符寫入流緩衝區
|--->OutputStreamWriter:
字符通向字節的轉換流(涉及鍵盤錄入時用)
輸入字符流:Reader: 字符讀取流的抽象類
|--->FileReader:
字符讀取流
|--->BufferedReader:
字符讀取流緩衝區
|--->InputStreamReader:
字節通向字符的轉換流(涉及鍵盤錄入時用)函數
經常使用的構造方法:大數據
public InputStreamReader(InputStream in)編碼
public InputStreamReader(InputStream in, String charsetName) 建立一個InputStreamReader,使用給定的名字的編碼spa
重要的屬性:code
private final StreamDecoder sd; 表示一個解碼器,說明讀取的數據會被轉成指定的編碼,若沒有指定,那麼將使用平臺默認的編碼。
重要的方法:
int read(); 讀取單個字符。
int read(char[] cbuf) 從流中的0位置讀取,讀取cbuf長度的字符。
int read(char[] cbuf, int offset, int length) offset表示從流中哪一個位置開始儲存字符,length表示最大的存儲個數。
public static void main(String[] args) throws IOException { InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\Desktop\\1.txt"),"gbk"); char[] cbuf = new char[10]; int len = 0; while((len = isr.read(cbuf))>0){ System.out.print(new String(cbuf,0,len)); } isr.close(); }
經常使用的構造方法:
public OutputStreamWriter(OutputStream out) 建立使用默認字符編碼的 OutputStreamWriter。
public OutputStreamWriter(OutputStream out, String charsetName) 建立使用指定字符集的 OutputStreamWriter。
重要的屬性:
private final StreamEncoder se; 表示一個編碼器,將內存中的字符流根據指定的編碼表編碼成爲字節儲存。
重要方法:
void write(char[] cbuf,int off,int len) 寫入字符數組的某一部分
void write(int c) 寫入單個字符
void write(String str,int off,int len) 寫入字符串的某一部分
void flash() 刷新該流的緩存
public static void main(String[] args) throws IOException { OutputStream os = new FileOutputStream("C:\\Users\\Desktop\\1.txt"); OutputStreamWriter osw = new OutputStreamWriter(os); String str = "你好嗎?\r\n我很好!"; osw.write(str); osw.close(); }
經常使用的構造函數:
FileInputStream(File file) 建立「File對象」對應的「文件輸入流」
FileInputStream(FileDescriptor fd) 建立「文件描述符」對應的「文件輸入流」
FileInputStream(String path) 建立「文件(路徑爲path)」對應的「文件輸入流」
重要方法:
int available() 返回「剩餘的可讀取的字節數」或者「skip的字節數」
void close() 關閉「文件輸入流」
FileChannel getChannel() 返回FileChannel
final FileDescriptor getFD()返回文件描述符 int read() 返回文件輸入流的下一個字節
int read(byte[] buffer, int off, int len) 讀取「文件輸入流」的數據並存在到buffer,從off開始存儲,存儲長度是len。
long skip(long n) 跳過n個字節
int read(); 讀取單個字節。
int read(byte[] b)從流中的0位置讀取,讀取b長度的字節。
int read(byte[] b, int offset, int length) offset表示從流中哪一個位置開始儲存字節,length表示最大的存儲個數。
經常使用的構造方法:
FileOutputStream(File file) 建立「File對象」對應的「文件輸入流」;默認「追加模式」是false,即「寫到輸出的流內容」不是以追加的方式添加到文件中。
FileOutputStream(File file, boolean append) 建「File對象」對應的「文件輸入流」;指定「追加模式」。
FileOutputStream(FileDescriptor fd) 建立「文件描述符」對應的「文件輸入流」;默認「追加模式」是false,即「寫到輸出的流內容」不是以追加的方式添加到文件中。
FileOutputStream(String path) 建立「文件(路徑爲path)」對應的「文件輸入流」;默認「追加模式」是false,即「寫到輸出的流內容」不是以追加的方式添加到文件中。
FileOutputStream(String path, boolean append) 建立「文件(路徑爲path)」對應的「文件輸入流」;指定「追加模式」。
重要方法:
FileChannel getChannel() 返回FileChannel
final FileDescriptor getFD() 返回文件描述符
void write(byte[] buffer, int off, int len) 將buffer寫入到「文件輸出流」中,從buffer的off開始寫,寫入長度是len。
void write(int n) 寫入字節n到「文件輸出流」中
void write(byte[] b) 將b.length個字節從指定的數組寫入到此輸出流中
是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)的方法。不能爲其指定編碼格式,使用平臺默認的編碼格式讀取字符,在不要求轉碼的狀況下,這是一個簡便的寫法,用於從文件中讀取字符。
經常使用的構造方法:
BufferReader(Reader in)建立一個使用默認大小輸入緩衝區的緩衝字符輸入流。
BufferReader(Reader in,int sz)建立一個使用指定大小的輸入緩衝區的緩衝字符輸入流。
重要方法:
String readLine()讀取一個文本行,到達文件結尾返回null
int read() 讀取單個字符,到達文件結尾返回-1
int read(char[] cbuf,int off,int len)將字符讀入數組的某一部分
boolean ready() 判斷該流是否已經準備好被讀取
void reset()將流重置到最新的標記
void mark(int readAheadLimit)標記流中的當前位置
long skip(Long n)跳過n個字符
經常使用構造方法:
BufferedWriter bf = new BufferedWriter(Writer out ); 建立一個使用默認大小輸出緩衝區的緩衝字符輸出流。
重要方法:
void newLine() 寫入一個行分隔符。
void write (int c)寫入單個字符
void write(char[] cbuf,int off,int len) 寫入字符數組的某一部分
void write (String s,int off,int len)寫入字符串的某一部分
void close()關閉此流,可是先刷新它
實現文件的複製:
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Desktop\\1.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\Desktop\\2.txt")); String str; while((str = br.readLine())!=null){ bw.write(str); bw.newLine(); } br.close(); bw.close(); }
經過getEncoding()方法能夠返回編碼方式,默認是平臺(eclipse)編碼,txt文件是GBK編碼,複製的文本會出現亂碼,由於讀取的時候是根據UTF-8讀,寫的時候是根據UTF-8寫,這時候就不能使用FileReader的方式,要使用
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Desktop\\1.txt"),"gbk"));
這裏的緩衝區不是減小IO操做次數,而是減小編碼轉換器的操做次數。這要和BufferInputStream、BufferOutputStream區別開來。
經常使用構造函數:
public BufferedInputStream(InputStream in); 建立一個內部緩衝區數組並將其存儲在 buf 中,該buf的大小默認爲8192。
BufferedInputStream(InputStream in,int size); 指定內部緩衝區的大小
重要屬性:
private static int defaultBufferSize = 8192;//內置緩存字節數組的大小 8KB protected volatile byte buf[]; //內置緩存字節數組 protected int count; //當前buf中的字節總數、注意不是底層字節輸入流的源中字節總數 protected int pos; //當前buf中下一個被讀取的字節下標 protected int markpos = -1; //最後一次調用mark(int readLimit)方法記錄的buf中下一個被讀取的字節的位置 protected int marklimit; //調用mark後、在後續調用reset()方法失敗以前雲尋的從in中讀取的最大數據量、用於限制被標記後buffer的最大值
重要方法:
int available(); //返回底層流對應的源中有效可供讀取的字節數 void close(); //關閉此流、釋放與此流有關的全部資源 boolean markSupport(); //查看此流是否支持mark void mark(int readLimit); //標記當前buf中讀取下一個字節的下標 int read(); //讀取buf中下一個字節 int read(byte[] b, int off, int len); //讀取buf中下一個字節 void reset(); //重置最後一次調用mark標記的buf中的位子 long skip(long n); //跳過n個字節、 不單單是buf中的有效字節、也包括in的源中的字節
該類主要是提供給InputStream的沒有緩衝功能的子類來使用的,常常看見有人把其和FileInputStram來一塊兒使用,FileInputStram自己具備緩存,速度更快,直接調用底層 readBytes(byte b[], int off, int len)。