FileInputStream/FileOutputStream

直接插在文件上,讀寫文件數(shù)據(jù)
創(chuàng)建對(duì)象
new FileOutputStream(文件)
不管文件是否存在,都會(huì)信件一個(gè)空文件
new FileOutputStream(文件,true)
文件存在,追加數(shù)據(jù)
高級(jí)流、操作流
與其他流對(duì)接,提供特點(diǎn)的數(shù)據(jù)處理功能
對(duì)高級(jí)流的操作,會(huì)對(duì)詳解的流執(zhí)行相同操作

FileOutputStream

import java.io.FileOutputStream;
import java.io.IOException;

public class Test1OutputStream {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("f3");
        out.write(97);//00 00 00 61->61
        out.write(98);//00 00 00 62->62
        out.write(99);//00 00 00 63->63
        out.write(356);//00 00 01 64->64
        byte[] a={
                101,102,103,104,105,
                106,107,108,109,110
        };
        out.write(a);
        out.write(a, 2, 4);
        out.flush();
        out.close();
        System.out.println("寫入完成");
    }
}

運(yùn)行結(jié)果

寫入完成

FileInputStream

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

public class Test2InputStream {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("f3");
        //單字節(jié)讀取 5個(gè)一批
        readByte(in);
        in.close();
        //流只能順序讀取一次
        in = new FileInputStream("f3");
        //批量讀取
        readMore(in);
        in.close();
    }

    private static void readMore(FileInputStream in) throws IOException {
        
        byte[] buff=new byte[5];
        int n;
        while((n=in.read(buff))!=-1)
        {
            System.out.println(n+" "+Arrays.toString(buff));
        }
        System.out.println();
        
    }

    private static void readByte(FileInputStream in) throws IOException {
        
        int b;
        while((b=in.read())!=-1)
        {
            System.out.print(b+" ");
        }
        System.out.println();
    }
}

運(yùn)行結(jié)果

97 98 99 100 101 102 103 104 105 106 107 108 109 110 103 104 105 106 
5 [97, 98, 99, 100, 101]
5 [102, 103, 104, 105, 106]
5 [107, 108, 109, 110, 103]
3 [104, 105, 106, 110, 103]
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容