利用構造方法也可以寫入文件
FileOutputStream(String name)
底層:
public FileOutputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null, false);
}
相當於new file(name)
OutputStream的基本方法
//向輸出流中寫入一個字節數據該字節數據為參數b的低8位
void write(int b) throws IOException
public byte[] getBytes()
String
編碼為一系列字節,將結果存儲到新的字節數組中。
當該字符串不能在默認字符集中編碼時,此方法的行為是未指定的。 當需要對編碼過程進行更多控制時,應使用CharsetEncoder
類。
//將一個字節類型的數組中的數據寫入輸出流
void write (byte 【】 b) throws IOException 例如 file.write(hello.getBytes());
//將一個字節類型的數組中的數據從指定位置(off)開始的len個字節寫入到輸出流
void write(byte【】 b,int off,int len) throws IOException
//關閉流釋放內存資源
void close() throws IOException
將輸出流中緩沖的數據全部寫到目的地
void flush() throws IOException
注意:先寫flush再寫close
---------------------------------------------------------------------------------------------------------------------------------------------------|------|--------|----------------------------------------------
字節流寫數據加異常處理