IO輸出輸入流

2021-10-21 17:57:16 字數 1835 閱讀 6923

這個東西的方法就是兩個

read(); 讀取檔案

close(); 釋放資源

fileinputstream in =

newfileinputstream

("e:\\io\\fis.txt");

int b =0;

while

(true

) system.out.

println

(b);

}//釋放資源

in.close()

;

也就兩個兩個方法

write(); 新增資料

close(); 釋放資源

fileoutputstream fos =

newfileoutputstream

("fos.txt");

fileoutputstream fos =

newfileoutputstream

("fos.txt"

);fos.

write(97

);

如果想把一段文字新增進去,可以使用以下方法
fileoutputstream fos =

newfileoutputstream

("fos.txt");

/*建立乙個檔案,冰把fos指向剛剛建立的檔案

*///將指定的位元組寫入此檔案輸出流

// fos.write(97);

//釋放資源

string str =

"你好啊"

;//把

byte

b = str.

getbytes()

;for

(int i =

0; i < b.length

; i++

)for

(int i =

0; i ) fos.

close()

;

解釋 str.getbytes();

string str =

"你好啊"

;byte

b = str.

getbytes()

;

byte …是位元組型陣列

getbytes() 方法是把str字串裡的**「你好啊」** 變成位元組型放進byte b 這個陣列

//輸入流物件

fileinputstream fis =

newfileinputstream

("c:\\users\\yzz\\desktop\\io.txt");

//輸出流物件

fileoutputstream fos =

newfileoutputstream

("e:\\測試\\io.txt");

int by;

//fis 讀取資料賦值給by

while

((by = fis.

read()

)!=-1

)//釋放資源

fis.

close()

; fos.

close()

;

複製的原理就是把這個檔案

while

((by = fis.

read()

)!=-1

)

如果在讀取的時候,檔案裡面沒有資料了,他就是返回乙個-1。while 判斷是 -1就是跳出這個while 迴圈

IO流 字元輸入流 輸出流,緩衝字元輸入流 輸出流

輸出字元流 緩衝字元輸入流 緩衝字元輸出流 輸入字元流 輸出字元流 filereader的用法 找到目標檔案 建立資料的輸入通道 讀取資料 關閉資源 方法一 public static void readtest1 throws ioexception filereader.close 方法二 使用...

IO流 輸入流和輸出流

1.輸入流和輸出流的聯絡和區別,字元流和位元組流的聯絡和區別 輸入流是得到資料,輸出流是輸出資料。字元流和位元組流是流的一種劃分,按處理照流的資料單位進行的劃分。兩類都分為輸入和輸出操作。在位元組流中輸出資料主要是使用outputstream完成,輸入使inputstream 在字元流中輸出主要是使...

輸入流 輸出流

接受乙個字串,遇 空格 tab 回車 都結束 用法1 單個接收 cin.get 字元變數名 可以用來接收字元 include using namespace std main char ch ch cin.get 或者cin.get ch cout 用法2 cin.get 字元陣列名,接收字元數目 ...