IO流筆記常用字節流字元流的使用

2022-09-09 20:51:27 字數 1633 閱讀 6193

流的概念和作用

流是一組順序的,有起點和終點的位元組集合,是對資料傳輸的總成或抽象,即資料在兩裝置間傳輸稱為流,流的本質是資料傳輸,根據資料傳輸特性將流抽象為各種類,方便更直觀的進行資料操作。

根據 處理的資料型別不同可以分為:字元流和位元組流

根據資料的流向可以分為:輸入流和輸出流

字元流和位元組流字元流的由來: 因為資料編碼的不同,而有了對字元進行高效操作的流物件。本質其實就是基於位元組流讀取時,去查了指定的碼表。 位元組流和字元流的區別:- 讀寫單位不同:位元組流以位元組(8bit)為單位,字元流以字元為單位,根據碼表對映字元,一次可能讀多個位元組。- 處理物件不同:位元組流能處理所有型別的資料(如、**i等),而字元流只能處理字元型別的資料。

結論:只要是處理純文字資料,就優先考慮使用字元流。 除此之外都使用位元組流。

// 將讀取的資料存放在buffer中,返回讀取的長度,當取完之後返回的是-1

while ((length = filereader.read(buffer)) != -1)

} catch (filenotfoundexception e) catch (ioexception e) finally catch (ioexception e) }}

}

@test

public void testinputoutputstream()

system.out.println("success");

} catch (filenotfoundexception e) catch (ioexception e) finally catch (ioexception e)

}if (fileoutputstream != null) catch (ioexception e) }}

}

import day12.student;

import org.junit.test;

import j**a.io.*;

public class objectiodemo catch (filenotfoundexception e) catch (ioexception e) finally catch (ioexception e)

}if (fileoutputstream != null) catch (ioexception e) }}

}@test

public void testobjectinputstream() catch (filenotfoundexception e) catch (ioexception e) catch (classnotfoundexception e) finally catch (ioexception e)

}if (objectinput != null) catch (ioexception e) }}

}}

IO 字元流 位元組流

io流 流按運算元據分為兩種 位元組流與字元流 流按流向分為 輸入流 輸出流 位元組流的抽象基類 inputstream outputstream 字元流的抽象基類 reader witer 這四個類派生出來的子類名稱都是以其父類名作為子類名的字尾。字元流 寫入流writer 用於操作檔案的writ...

IO 字元流 位元組流

直接和檔案互動,沒有快取區。所以即便沒有close 資料也已寫入檔案中。操作的是位元組 byte 可對任何檔案進行讀寫操作 private static void write throws ioexception private static void read throws ioexception...

IO流 位元組流,字元流,緩衝流

一 io流的分類 組織架構 這麼龐大的體系裡面,常用的就那麼幾個,我們把它們抽取出來,如下圖 二 字元位元組,輸入輸出流的概念 字元流的由來 因為資料編碼的不同,而有了對字元進行高效操作的流物件。本質其實就是基於位元組流讀取時,去查了指定的碼表。位元組流和字元流的區別 結論 只要是處理純文字資料,就...