一次写多个字节的方法 || 数据追加续写
?一次寫多個字節(jié)的方法:
? ? ? ? ?public void write(byte[] b):將 b.length字節(jié)從指定的字節(jié)數(shù)組寫入此輸出流。
? ? ? ? ?public void write(byte[] b, int off, int len) :從指定的字節(jié)數(shù)組寫入 len字節(jié),從偏移量 off開始輸出到此輸出流。
public void write(byte[ ] b):將 b.length字節(jié)從指定的字節(jié)數(shù)組寫入此輸出流。
? ? ? ? ? ? 一次寫多個字節(jié):
? ? ? ? ? ? ? ? 如果寫的第一個字節(jié)是正數(shù)(0-127),那么顯示的時候會查詢ASCII表
? ? ? ? ? ? ? ? 如果寫的第一個字節(jié)是負(fù)數(shù),那第一個字節(jié)會和第二個字節(jié),兩個字節(jié)組成一個中文顯示,查詢系統(tǒng)默認(rèn)碼表(GBK)
public void write(byte[] b, int off, int len) :把字節(jié)數(shù)組的一部分寫入到文件中
? ? ? ? ? ? ? ? int off:數(shù)組的開始索引
? ? ? ? ? ? ? ? int len:寫幾個字節(jié)
?寫入字符的方法:可以使用String類中的方法把字符串,轉(zhuǎn)換為字節(jié)數(shù)組
? ? ? ? ? ? ? ? byte[ ] getBytes() ?把字符串轉(zhuǎn)換為字節(jié)數(shù)組
package com.itheima.demo01.OutputStream;import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays;public class Demo02OutputStream {public static void main(String[] args) throws IOException {//創(chuàng)建FileOutputStream對象,構(gòu)造方法中綁定要寫入數(shù)據(jù)的目的地FileOutputStream fos = new FileOutputStream(new File("09_IOAndProperties\\b.txt"));//調(diào)用FileOutputStream對象中的方法write,把數(shù)據(jù)寫入到文件中//在文件中顯示100,寫個字節(jié)fos.write(49);fos.write(48);fos.write(48);byte[] bytes = {65,66,67,68,69};//ABCDE//byte[] bytes = {-65,-66,-67,68,69};//烤紻Efos.write(bytes);fos.write(bytes,1,2);//BCbyte[] bytes2 = "你好".getBytes();System.out.println(Arrays.toString(bytes2));//[-28, -67, -96, -27, -91, -67]fos.write(bytes2);//釋放資源fos.close();} }
數(shù)據(jù)追加續(xù)寫
?參數(shù):
? ? ? ? ? ?String name? ? File file:寫入數(shù)據(jù)的目的地
? ? ? ? ? ?boolean append:追加寫開關(guān)
? ? ? ? ? ? true:創(chuàng)建對象不會覆蓋源文件,繼續(xù)在文件的末尾追加寫數(shù)據(jù)
? ? ? ? ? ? false:創(chuàng)建一個新文件,覆蓋源文件
? ? 寫換行:寫換行符號
? ? ? ? windows:\r\n
? ? ? ? linux:/n
? ? ? ? mac:/r
總結(jié)
以上是生活随笔為你收集整理的一次写多个字节的方法 || 数据追加续写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字节流 的知识1
- 下一篇: 字节输入流 InputStream