字節流
字節流是指傳輸過程中,傳輸數據的最基本單位是字節的流,一個不包含邊界數據的連續流;字節流是由字節組成的,主要用在處理二進制數據。
OutputStream字節輸出流
常用方法
這個抽象類是所有表示字節輸出流的類的超類。具體方法如下:
- write(int b)
將指定的字節寫入此輸出流 - write(byte b[])
將指定字節數組中的b.length個字節寫入此輸出流 - write(byte b[], int off, int len)
將指定字節數組中的len個字節從偏移量off開始寫入此輸出流。 write(b, off, len)的一般約定是將數組b中的某些字節按順序寫入輸出流; 元素b[off]是寫入的第一個字節, b[off+len-1]是此操作寫入的最后一個字節 - flush()
刷新此輸出流並強制寫出任何緩沖的輸出字節 - close()
關閉此輸出流並釋放與此流關聯的任何系統資源
FileOutputStream
FileOutputStream繼承OutputStream,有兩個常用構造方法
- FileOutputStream(File file)
對文件file進行覆蓋寫入 - FileOutputStream(File file, boolean append)
append:true 代表追加寫入
寫入舉例
-
覆蓋寫入
public static void write() throws IOException { File f = new File("d:" + File.separator + "test.txt"); //如果文件不存在會自動創建 OutputStream out = new FileOutputStream(f); String str = "Hello World\nsddgdgdf"; //因為是字節流,所以要轉化成字節數組進行輸出 byte[] b = str.getBytes(); out.write(b); out.close(); }
-
追加寫入
public static void continueWrite() throws IOException { File f = new File("d:" + File.separator + "test.txt"); //如果文件不存在會自動創建 OutputStream out = new FileOutputStream(f,true); String str = "Hello World\nsddgdgdf3434"; //因為是字節流,所以要轉化成字節數組進行輸出 byte[] bytes = str.getBytes(); for (byte b: bytes){ out.write(b); } out.close(); }
InputStream字節輸入流
這個抽象類是表示字節輸入流的所有類的超類
常用方法
-
read()
從輸入流中讀取下一個字節的數據。 值字節以0到255范圍內的int形式返回。 如果由於已到達流末尾而沒有可用字節,則返回值-1 。 此方法會阻塞,直到輸入數據可用、檢測到流結束或拋出異常為止 -
read(byte b[])
從輸入流中讀取一定數量的字節並將它們存儲到緩沖區數組b 。 實際讀取的字節數作為整數返回。 此方法會阻塞,直到輸入數據可用、檢測到文件結尾或拋出異常。
如果b的長度為零,則不讀取字節並返回0 ; 否則,將嘗試讀取至少一個字節。 如果由於流位於文件末尾而沒有可用字節,則返回值-1 ; 否則,至少讀取一個字節並將其存儲到b 。
讀取的第一個字節存儲到元素b[0] ,下一個存儲到b[1] ,依此類推。 讀取的字節數最多等於b的長度。 令k為實際讀取的字節數; 這些字節將存儲在元素b[0]到b[ k -1] ,而元素b[ k ]到b[b.length-1]不受影響。 -
read(byte b[], int off, int len)
從輸入流中讀取最多len個字節的數據到一個字節數組中。 嘗試讀取多達len個字節,但可能會讀取較小的數字。 實際讀取的字節數作為整數返回。
讀取的第一個字節存儲到元素b[off] ,下一個存儲到b[off+1] ,依此類推 -
skip(long n)
跳過並丟棄此輸入流中的n字節數據,在跳過n個字節之前到達文件末尾, 返回實際跳過的字節數並拋出異常。 如果n為負,則InputStream類的skip方法始終返回 0 -
available()
返回當前可以跳過的最大值 -
close()
-
markSupported()
如果此流實例支持標記和重置方法,則為true ; 否則為false -
mark(int readlimit)
標記此輸入流中的當前位置。 對reset方法的后續調用將此流重新定位在最后標記的位置,以便后續讀取重新讀取相同的字節。 -
reset()
將此流重新定位到上次在此輸入流上調用mark方法時的位置
讀取舉例
-
文件大小確定
public static void read() throws IOException { File f = new File("d:" + File.separator + "test.txt"); InputStream inputStream = new FileInputStream(f); //可以根據文件的大小設置字節數組的長度 long length = f.length(); byte[] bytes = new byte[(int) length]; int len = inputStream.read(bytes); inputStream.close(); String s = new String(bytes, 0, len); System.out.println(s); }
-
文件大小不確定
public static void readNoSize2() throws IOException { File f = new File("d:" + File.separator + "test.txt"); InputStream inputStream = new FileInputStream(f); StringBuilder builder = new StringBuilder(); byte[] bytes = new byte[6]; int temp = 0, len = 0; //-1文件讀取完畢 while ((temp = inputStream.read()) != -1) { bytes[len] = (byte) temp; len++; if (len == 6) { builder.append(new String(bytes)); len = 0; } } builder.append(new String(bytes, 0, len)); inputStream.close(); System.out.println(builder); }
字符流
字節流就是普通的二進制流,讀出來的是bit,而字節流不便於處理Unicode形式存儲的信息。字符流就是在字節流的基礎按照字符編碼處理,處理的是char。
Reader
常用方法
- read()
- read(char cbuf[])
- read(char cbuf[], int off, int len)
- skip(long n)
- ready():告訴這個流是否准備好被讀取
- markSupported()
- mark(int readAheadLimit)
- reset()
- close()
字符流讀取舉例
-
文件大小確定
public static void read() throws IOException { File f = new File("d:" + File.separator + "test.txt"); Reader out = new FileReader(f); char[] chars = new char[(int) f.length()]; int len = out.read(chars); out.close(); System.out.println(new String(chars, 0, len)); }
-
文件大小不確定
public static void readNoSize() throws IOException { File f = new File("d:" + File.separator + "test.txt"); Reader out = new FileReader(f); char[] arrs = new char[6]; StringBuilder builder = new StringBuilder(); int len = 0; int temp = 0; while ((temp = out.read()) != -1) { arrs[len] = (char) temp; len++; if (len == 6) { builder.append(arrs); len = 0; } } builder.append(arrs, 0, len); out.close(); System.out.println(builder); }
Writer
用於寫入字符流的抽象類;內置一個保存字符串和單個字符寫入的臨時緩沖,大小為1024
常用方法
- write(char cbuf[])
- write(char cbuf[], int off, int len)
- write(String str) :寫入一個字符串
- write(String str, int off, int len)
- append(CharSequence csq):將指定的字符序列附加到此編寫器
- append(CharSequence csq, int start, int end)
- append(char c)
- flush()
沖洗流。 如果流已將來自各種 write() 方法的任何字符保存在緩沖區中,則立即將它們寫入其預期目的地。 然后,如果該目標是另一個字符或字節流,則刷新它。 因此,一次 flush() 調用將刷新 Writers 和 OutputStreams 鏈中的所有緩沖區 - close()
字符流寫入舉例
public static void writer() throws IOException {
File f = new File("d:" + File.separator + "test.txt");
//覆蓋原有的
Writer out = new FileWriter(f);
String str = "Hello World";
out.write(str);
out.close();
}
字符流和字節流區別
- 字節流可用於任何類型的對象,包括二進制對象,而字符流只能處理字符或者字符串;
- 字節流提供了處理任何類型的IO操作的功能,但它不能直接處理Unicode字符,而字符流就可以
- 字節流默認不使用緩沖區;字符流使用緩沖區
- 在硬盤上的所有文件都是以字節形式存在的(圖片,聲音,視頻),而字符值在內存中才會形成。