Java 3: IO Stream

image.png

IO Stream 核心方法

InputStream類
read():int
read(byte[]) :int
read(byte[],int,int):int

OutputStream類
write(int):void //寫入int(32位 4個(gè)字節(jié))的低8位跷叉,其他24個(gè)字節(jié)將被忽略
write(byte[]):void
write(byte[],int,int):void

Reader類
read(Charbuffer):int
read():int
read(char[]):int
read(char[],int,int):int

Writer類
wirte(int):void //寫入一個(gè)character( int 32位,但是char 16位 ,2個(gè)字節(jié))的低16位涌韩,高16位將被忽略
wirte(char[]):void
wirte(char[],int ,int):void
write(String):void
write(String,int,int):void

FileInputStream
FileOutputStream

DataInputStream
DataOutputStream
DataXXX 是InputStream,OutputStream的包裝類呼巷,基于InputStream潦蝇,OutputStream提供了很多操作數(shù)據(jù)的方法癣疟,比如操作int,string等摸袁;

BufferedInputStream
BufferedOutputStream

字節(jié)流

1: 單一字節(jié)讀取 悼潭;
2:批量讀缺蛹伞(自定義數(shù)組大小)舰褪;
3:使用Buffered皆疹,BufferedInputStream中。原理是:先單字節(jié)讀取到buffer中占拍,在從buffer中轉(zhuǎn)移到目標(biāo)位置略就;
BufferedInputStream 中使用buffer減少訪問磁盤的次數(shù),達(dá)到提升性能晃酒;
其中批量讀取最高效表牢;

 public synchronized int read() throws IOException {
        if (pos >= count) {
            fill();
            if (pos >= count)
                return -1;
        }
        return getBufIfOpen()[pos++] & 0xff;
    }

 /**
     * Fills the buffer with more data, taking into account
     * shuffling and other tricks for dealing with marks.
     * Assumes that it is being called by a synchronized method.
     * This method also assumes that all data has already been read in,
     * hence pos > count.
     */
    private void fill() throws IOException {
        byte[] buffer = getBufIfOpen();
        if (markpos < 0)
            pos = 0;            /* no mark: throw away the buffer */
        else if (pos >= buffer.length)  /* no room left in buffer */
            if (markpos > 0) {  /* can throw away early part of the buffer */
                int sz = pos - markpos;
                System.arraycopy(buffer, markpos, buffer, 0, sz);
                pos = sz;
                markpos = 0;
            } else if (buffer.length >= marklimit) {
                markpos = -1;   /* buffer got too big, invalidate mark */
                pos = 0;        /* drop buffer contents */
            } else if (buffer.length >= MAX_BUFFER_SIZE) {
                throw new OutOfMemoryError("Required array size too large");
            } else {            /* grow buffer */
                int nsz = (pos <= MAX_BUFFER_SIZE - pos) ?
                        pos * 2 : MAX_BUFFER_SIZE;
                if (nsz > marklimit)
                    nsz = marklimit;
                byte nbuf[] = new byte[nsz];
                System.arraycopy(buffer, 0, nbuf, 0, pos);
                if (!bufUpdater.compareAndSet(this, buffer, nbuf)) {
                    // Can't replace buf if there was an async close.
                    // Note: This would need to be changed if fill()
                    // is ever made accessible to multiple threads.
                    // But for now, the only way CAS can fail is via close.
                    // assert buf == null;
                    throw new IOException("Stream closed");
                }
                buffer = nbuf;
            }
        count = pos;
        int n = getInIfOpen().read(buffer, pos, buffer.length - pos); // 這里是真正讀內(nèi)容的地方
        if (n > 0)
            count = n + pos;
    }

字符流

1:編碼問題
2:文本,文本文件
文本:Java的文本(char)是16位無符號(hào)整數(shù)贝次,是字符的unicode編碼(雙字節(jié)編碼 2字節(jié) 16位)崔兴;
文件: byte byte byte ... 的數(shù)據(jù)序列
文本文件:文本文件是文本(char)按照某種編碼方案(utf-8,utf-16be,gbk)序列號(hào)為byte的存儲(chǔ)結(jié)果;
3:字符流(Reader,Writer)
字符的處理,一次處理一個(gè)字符
字符的底層基本的字節(jié)序列敲茄;

InputStreamReader / OutputStreamWriter

InputStreamReader OutputStreamWriter是字符流和字節(jié)流的橋梁位谋,提供需要解析編碼時(shí)的轉(zhuǎn)換;
InputStreamReader:實(shí)現(xiàn)輸入的字節(jié)流(byte)解析為字符流(char),按照編碼解析折汞; OutputStreamWriter:實(shí)現(xiàn)輸出的字符流(char)解析為字節(jié)流(byte),按照編碼解析倔幼;

FileReader / FileWriter

BufferedReader / BufferedWriter,PrinterWriter

序列化

1:對(duì)象序列化:是將Object 轉(zhuǎn)換為byte序列,反之叫對(duì)象的反序列化爽待;
2:序列化流 ObjectOutputStream,ObjectInputStream. 關(guān)鍵反復(fù)writerObject,readObject;
3:序列化需要實(shí)現(xiàn)序列化接口(Serializable)

序列化 - transient 關(guān)鍵字

加了transient 關(guān)鍵字 损同,那么該元素不會(huì)進(jìn)行jvm默認(rèn)的序列化;

ArrayList 中的 transient 序列化

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
   
    /**
     * The array buffer into which the elements of the ArrayList are stored.
     * The capacity of the ArrayList is the length of this array buffer. Any
     * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
     * will be expanded to DEFAULT_CAPACITY when the first element is added.
     */
    transient Object[] elementData; // non-private to simplify nested class access
}

ArrayList 中的 對(duì) 標(biāo)有transient關(guān)鍵字的元素進(jìn)行序列化

 /**
     * Save the state of the <tt>ArrayList</tt> instance to a stream (that
     * is, serialize it).
     *
     * @serialData The length of the array backing the <tt>ArrayList</tt>
     *             instance is emitted (int), followed by all of its elements
     *             (each an <tt>Object</tt>) in the proper order.
     */
    private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException{
        // Write out element count, and any hidden stuff
        int expectedModCount = modCount;
        s.defaultWriteObject(); //1: 第一步使用默認(rèn)的序列化

        // Write out size as capacity for behavioural compatibility with clone()
        s.writeInt(size);

        // Write out all elements in the proper order.
        for (int i=0; i<size; i++) {
            s.writeObject(elementData[i]); //第二步 只序列化有用的元素
        }

        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
    }

 /**
     * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is,
     * deserialize it).
     */
    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        elementData = EMPTY_ELEMENTDATA;

        // Read in size, and any hidden stuff
        s.defaultReadObject(); //1: 第一步 使用默認(rèn)的序列化

        // Read in capacity
        s.readInt(); // ignored

        if (size > 0) {
            // be like clone(), allocate array based upon size not capacity
            ensureCapacityInternal(size);

            Object[] a = elementData;
            // Read in all elements in the proper order.
            for (int i=0; i<size; i++) {
                a[i] = s.readObject();  //2:第二部 只序列化有用的元素
            } 
        }
    }

序列化父子類

1:父類實(shí)現(xiàn)了序列化接口鸟款,子類也能實(shí)現(xiàn)序列化膏燃;
2:子類需要實(shí)現(xiàn)序列化,只需要子類實(shí)現(xiàn)序列化接口何什,父類不一定要實(shí)現(xiàn)序列化接口
3:對(duì)子類對(duì)象進(jìn)行反序列化操作時(shí)组哩,如果父類沒有實(shí)現(xiàn)序列化接口,那么其父類的構(gòu)造函數(shù)會(huì)被顯示的調(diào)用处渣;如果父類實(shí)現(xiàn)了序列化接口伶贰,反序列化可以直接讀取到構(gòu)造中的內(nèi)容,無需顯示調(diào)用罐栈;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末黍衙,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子荠诬,更是在濱河造成了極大的恐慌琅翻,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柑贞,死亡現(xiàn)場(chǎng)離奇詭異方椎,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)钧嘶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門棠众,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人康辑,你說我怎么就攤上這事摄欲。” “怎么了疮薇?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵胸墙,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我按咒,道長(zhǎng)迟隅,這世上最難降的妖魔是什么但骨? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮智袭,結(jié)果婚禮上奔缠,老公的妹妹穿的比我還像新娘。我一直安慰自己吼野,他們只是感情好校哎,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著瞳步,像睡著了一般闷哆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上单起,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天抱怔,我揣著相機(jī)與錄音,去河邊找鬼嘀倒。 笑死屈留,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的测蘑。 我是一名探鬼主播灌危,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼碳胳!你這毒婦竟也來了乍狐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤固逗,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后藕帜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體烫罩,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年洽故,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了贝攒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡时甚,死狀恐怖隘弊,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情荒适,我是刑警寧澤梨熙,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站刀诬,受9級(jí)特大地震影響咽扇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一质欲、第九天 我趴在偏房一處隱蔽的房頂上張望树埠。 院中可真熱鬧,春花似錦嘶伟、人聲如沸怎憋。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绊袋。三九已至,卻和暖如春耽装,著一層夾襖步出監(jiān)牢的瞬間愤炸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國打工掉奄, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留规个,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓姓建,卻偏偏與公主長(zhǎng)得像诞仓,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子速兔,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容