IO位元組流與字元流

2021-10-02 20:45:57 字數 3923 閱讀 6552

輸出流

//1.建立檔案輸出流物件,並且關聯a.txt

fileoutputstream out = new fileoutputstream("a.txt");

//2.往檔案中寫入資料

out.write(99); //一次寫入乙個位元組

out.write();//一次寫入乙個位元組陣列

string str="我愛你中國";

byte bytes = str.getbytes();

out.write(bytes);

string str2="抗擊疫情,萬眾一心";

byte bytes1 = str2.getbytes();

//從位元組陣列的0索引處開始,寫入12個位元組到檔案中

out.write(bytes1,0,12);

out.close();//關閉流。

異常

fileoutputstream out1 = new fileoutputstream("b.txt",true);

//往檔案中寫資料

out1.write("沅水通波接武崗,".getbytes());

out1.write("\r\n".getbytes());

out1.close();

fileoutputstream out2 = null;

//流的異常處理

try catch (ioexception e) finally

} catch (ioexception e)

}

輸入流

fileinputstream in1 = new fileinputstream("e.txt");

byte bytes2 = new byte[1024];

//返回的是讀取到的有效的位元組個數

int len2 = in.read(bytes2);

system.out.println(len2);

string s = new string(bytes2, 0, len2);

system.out.println(s);

in1.close();

fileinputstream in2 = new fileinputstream("e.txt");

//自己建立乙個位元組陣列,來充當容器

byte bytes3 = new byte[1024];

int len3=in2.read(bytes3,0,3);

system.out.println("讀取到的有效的位元組個數:"+len3);

/* for (byte abyte : bytes) */

string s2 = new string(bytes3, 0, len3);

system.out.println(s2);

in2.close();

輸入輸出流應用

fileinputstream in =new fileinputstream("c:\\users\\shenmoumou\\music\\我的和**\\**\\辛曉琪 -  領悟.***"); 

fileoutputstream out = new fileoutputstream("c:\\users\\shenmoumou\\desktop\\辛曉琪 - 領悟.***"); int len=0;

byte bytes=new byte[1024*8];

long start = system.currenttimemillis();

while ((len=in.read(bytes))!=-1)

in.close();

out.close();

long end = system.currenttimemillis();

system.out.println("複製完成耗時" + (end - start) + "毫秒");

異常

> fileinputstream in = null;

> fileoutputstream out = null;

>

try>

}catch

(ioexception e)

finally

>

if(out != null)

>

}catch

(ioexception e)

>

}

outputstreamwriter out =

newoutputstreamwriter

(new

fileoutputstream

("a.txt"

,true))

; out.

write

('你');

//一次寫入乙個字元

out.

write

("\r\n");

//寫入換行符

char

chars =

; out.

write

(chars)

; out.

write

("\r\n");

//寫入換行符

out.

write

("自古多情空餘恨,此恨綿綿無決期",0

,8);

out.

write

("\r\n");

//寫入換行符

//釋放資源

out.

close()

;//關閉並重新整理。

//inputstreamreader 是位元組流通向字元流的橋梁:

// 它使用指定的 charset 讀取位元組並將其解碼為字元。

// 它使用的字符集可以由名稱指定或顯式給定,或者可以接受平台預設的字符集。

inputstreamreader in = new inputstreamreader(new fileinputstream(「c.txt」));

//讀取資料

int ch = in.read(); //一次讀取乙個字元,如果讀取不到返回 -1 經常用 -1 來判斷檔案是否讀取完畢

system.out.println(ch);

inputstreamreader in =

newinputstreamreader

(new

fileinputstream

("c.txt"))

;//定義乙個字元陣列,充當緩衝區

char

chars=

newchar

[1024];

//一次讀取多個字元,放到緩衝區中,返回值是,讀取到的有效的字元個數

int len = in.

read

(chars)

; string s = string.

valueof

(chars,

0,len)

; system.out.

println

(s);

string s1 =

newstring

(chars,

0, len)

; system.out.

println

(s1)

;int len= in.

read

(chars,0,

3); system.out.

println

(len)

;for

(char achar : chars)

in.close()

;

IO 字元流 位元組流

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

IO 字元流 位元組流

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

I O流 字元流和位元組流

一 位元組流 1 位元組輸出流 outputstream 往指定檔案寫資料 常用方法 close 釋放資源 flush 重新整理流,並強制寫出所有的緩衝的輸出位元組 write byte b 將指定的 byte 陣列寫入到輸出流 write byte b,int off,int len 將指定byt...