生活随笔
收集整理的這篇文章主要介紹了
字节流写数据的三种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
-
寫數據的方法分類
方法名說明
| void write(int b) | 將指定的字節寫入此文件輸出流 一次寫一個字節數據 |
| void write(byte[] b) | 將 b.length字節從指定的字節數組寫入此文件輸出流 一次寫一個字節數組數據 |
| void write(byte[] b, int off, int len) | 將 len字節從指定的字節數組開始,從偏移量off開始寫入此文件輸出流 一次寫一個字節數組的部分數據 |
-
示例代碼
public class FileOutputStreamDemo02 {public static void main(String[] args) throws IOException {//FileOutputStream(String name):創建文件輸出流以指定的名稱寫入文件FileOutputStream fos = new FileOutputStream("myByteStream\\fos.txt");//new File(name)
// FileOutputStream fos = new FileOutputStream(new File("myByteStream\\fos.txt"));//FileOutputStream(File file):創建文件輸出流以寫入由指定的 File對象表示的文件
// File file = new File("myByteStream\\fos.txt");
// FileOutputStream fos2 = new FileOutputStream(file);
// FileOutputStream fos2 = new FileOutputStream(new File("myByteStream\\fos.txt"));//void write(int b):將指定的字節寫入此文件輸出流
// fos.write(97);
// fos.write(98);
// fos.write(99);
// fos.write(100);
// fos.write(101);// void write(byte[] b):將 b.length字節從指定的字節數組寫入此文件輸出流
// byte[] bys = {97, 98, 99, 100, 101};//byte[] getBytes():返回字符串對應的字節數組byte[] bys = "abcde".getBytes();
// fos.write(bys);//void write(byte[] b, int off, int len):將 len字節從指定的字節數組開始,從偏移量off開始寫入此文件輸出流
// fos.write(bys,0,bys.length);fos.write(bys,1,3);//釋放資源fos.close();}
}
?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結
以上是生活随笔為你收集整理的字节流写数据的三种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。