java之IO流 位元組流和字元流

2021-09-23 06:06:42 字數 1684 閱讀 4586

file類是對檔案操作的類。

在windows和非windows系統,目錄分隔符是不同的:

windows:採用\

非windows:採用/

絕對路徑:從磁碟的根目錄開始,一級一級直到找到這個檔案

c:/users/admin/desktop/abc.txt

相對路徑:以某乙個檔案作為參照,相對於這個檔案的路徑

abc.txt

按照方向:輸入流和輸出流

按照單位:位元組流和字元流

inputstream:位元組輸入流

outputstream:位元組輸出流

位元組流:

inputstream ins =

newfileinputstream

("file\\a.txt");

ins.

close()

;

read(byte array, int offset, int length)

read()

如果例項化輸出流的目標檔案不存在,執行程式寫資料的時候,會自動建立這個檔案。

outputstream ops =

newfileoutputstream

("file\\b.txt");

ops.

write

(array,

0, length)

;

案例:

inputstream ins = null;

outpustream ops = null;

try}

catch

(ioexception e)

finally

catch

(ioexception e)}if

(ops != null)

catch

(ioexception e)

}}

字元流:

字元流是按照字元來讀取的,字元輸入流和字元輸出流的類分別是:reader、writer

資料的讀取

read(char array)

**示例:

reader:

//此時try中括號裡面例項化的reader物件,只能在try**段中使用,並且使用結束後,會自動進行流關閉

//try小括號中的內容不是隨便寫的,只有autoclosable介面的實現類可以在try的小括號裡宣告和例項化

try(reader reader =

newfilereader

("c:/a.txt"))

}catch

(ioexception e)

writer:

try

(writer writer =

newfilewriter

("file\\a.txt"

,true))

catch

(ioexception e)

I O流 字元流和位元組流

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

io位元組流和字元流

2014 11 30 晚上 位元組流system.in是基本的 inputstream 流,system.out 是基本的 outputstream 流,如果要實現字元流從控制台讀入 bufferedreader bufferedreader new bufferreader reader inpu...

IO流 位元組流和字元流總結

一.位元組流總結 1.位元組輸入流 inputstream fileinputstream 從檔案讀取位元組 bufferedinputstream 加入緩衝功能,提高檔案的讀取效率 bytearrayinputstream 讀取位元組陣列 2.位元組輸出流 outputstream fileout...