溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java _io_字節(jié)數(shù)組輸入流

發(fā)布時間:2020-07-10 02:03:04 來源:網(wǎng)絡(luò) 閱讀:221 作者:wx5d21d5e6e5ab1 欄目:編程語言

創(chuàng)建源:字節(jié)數(shù)組 不要太大
選擇流: InputStream is =new ByteArrayInputStream(byte[] byte);
操作(輸入)
無釋放資源(使用.close()會是一個空方法,為了保證風(fēng)格統(tǒng)一)

源頭由文件(硬盤)換成字節(jié)數(shù)組:
因為字符數(shù)組是內(nèi)存而不是文件(硬盤),所以java虛擬機(jī)可直接調(diào)用,由垃圾回收機(jī)制
(gc)來釋放,所以無需手動.close()釋放

public class test{
    public static void main(String[]args)
    {
        //創(chuàng)建源
        byte[] src="ad ad and".getBytes();  
        //選擇流
        InputStream is=null;
        try {
            is=new ByteArrayInputStream(src);

        byte[] flush=new byte[5]; //緩沖字符器
        int len=-1;
        while((len=is.read(flush))!=-1)
        {
            String s=new String(flush,0,len);  //解碼,有字節(jié)到字符串
            System.out.println(s);
        }
    }catch(IOException e)
    {
        e.printStackTrace();
    }

}

}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI