21 IO流 位元組輸入輸出流 緩衝流

2021-07-24 22:52:18 字數 2915 閱讀 9508

2.io流常用父類

字元流的抽象父類:

3.io程式書寫

* fileinputstream fis = new fileinputstream(「aaa.txt」); //建立乙個檔案輸入流物件,並關聯aaa.txt

int b; //定義變數,記錄每次讀到的位元組

while((b = fis.read()) != -1)

fis.close();                                            //關閉流釋放資源
10000001 byte -1的原碼

11111110 -1的反碼

11111111 -1的補碼

int a=255;(int)(byte)a=?

怎麼算?能解析一下嗎?

int型是32位

byte型是8位的

int a=255;

a的儲存是這樣的:00000000

00000000

00000000

11111111

轉化成小範圍的是強制截去前面多餘的,

就是 (byte)a的儲存是這樣的:11111111

對應的值就是-1

轉化成大範圍是前面補0和1,是根據被轉化的最高位也就是這裡的1

那麼(int)(byte)a的儲存是這樣的:11111111

11111111

11111111

11111111

也就是-1

int a=127;

byte i=(byte)a;

system.out.println(i);

int a1=128;

byte i1=(byte)a1;

system.out.println(i1);

127

-128

fileoutputstream fos = new fileoutputstream(「bbb.txt」,true); //如果沒有bbb.txt,會建立出乙個

//雖然寫出的是乙個int數,但是在寫出的時候會將前面的24個0去掉,所以寫出的乙個byte

fos.write(98);

fos.write(99);

fos.close();

不寫後面的true則會清空原有內容。

弊端:效率太低

拷貝20個檔案,並在把他們刪除

public

static

void

main(string args) throws ioexception, interruptedexception

i++;

} }}

刪除

file

file=new

file("d://wb//c"+i+".txt");

file.delete();

i++;

read(byte b)讀的位元組流裝到陣列

* write(byte b)把陣列裡面的位元組寫

* write(byte b, int off,int len)寫出有效的位元組個數

* 定義陣列,索引起始位置,結束也就是len

public

class bytewriteread

}

}

while((len = fis.read(arr)) != -1) 

fis.close();

fos.close();

b.bufferedinputstream

c.bufferedoutputstream

d.拷貝的**

fileinputstream fis = new fileinputstream("致青春.***");           //建立檔案輸入流物件,關聯致青春.***

bufferedinputstream bis = new bufferedinputstream(fis); //建立緩衝區對fis裝飾

fileoutputstream fos = new fileoutputstream("copy.***"); //建立輸出流物件,關聯copy.***

bufferedoutputstream bos = new bufferedoutputstream(fos); //建立緩衝區對fos裝飾

int b;

while((b = bis.read()) != -1)

bis.close(); //只關裝飾後的物件即可

bos.close();

e.小陣列的讀寫和帶buffered的讀取哪個更快?

close()方法

位元組流出中文的問題

if(!file.exists()) else

if(file.isdirectory()) else

fos.close();

輸入輸出流(IO流

流 流 stream 的概念源於 unix 中管道 pipe 的概念,在 unix 中,管道是一條不間斷的位元組流,用來實現程式或程序間的通訊,或讀寫外圍裝置 外部檔案等。重要 1.流操作結束後必須關閉。2.inputstream reader outputstream write全是抽象,不能直接...

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

處理流 裝飾流 位元組緩衝流,字元緩衝流 用於提高位元組流的效能 bufferedinputstream,bufferedoutputstream 位元組緩衝流 bufferedinputstream is newbufferedinputstream new fileinputstream fil...

輸入輸出流緩衝

每乙個輸入輸出流都包含乙個指標,指向某種streambuf 即流緩衝,這依賴於它是否處理標準i o 檔案 記憶體等 我們可以直接訪問 streambuf。例如,可以向streambuf移進 移出原始位元組,而不必通 過輸入輸出流來格式化它們。當然,這時通過呼叫streambuf物件的 成員函式來完成...