java的 IO流即輸入輸出資料流,流是一組有順序的,有起點和終點的位元組結合,是對資料轉送的總稱。即資料在兩裝置間的傳輸稱為流,流的本質是資料轉送。 IO流可以分為位元組流和字元流。給出相應的IO結構圖: 650) this.width=650;" id="aimg_293" src="http://techfoxbbs.com/data/attachment/forum/201505/22/132147au70qijq0b0u5je0.png" class="zoom" width="570" alt="132147au70qijq0b0u5je0.png" /> 在接下來的一段時間裡,將會慢慢介紹各種流的使用,本篇部落格先介紹位元組流的FileOutputStream和相對應的FileInputStream。
一.FileOutputStream(檔案輸出資料流) OutputStream是一個抽象類別,抽象類別必須通過子類實現。現在要向檔案裡輸出就要用FileOutputStream。
FileOutputStream有四個構造方法,分別為 1.FileOutputStream(File file)-------------向File對象的檔案寫入資料 2.FileOutputStream(File file,boolean append);------向File對象的檔案追加寫入資料 3.FileOutputStream(String path)-------------向指定檔案寫入資料 4.FileOutputStream(String path,boolean append);--------向指定檔案追加寫入資料 當append的值為true時,向檔案中寫入的資料會追加到原資料的後面,否則會重寫該檔案的資料。預設為false。
寫入方法1:一個個位元組寫入
public static void main(String[] args) {
try {
//建立一個檔案位元組輸出資料流對象
OutputStream os=new FileOutputStream("L:\\io.txt");
//寫入的資料
String string="hello IO Stream";
byte[]bytes=string.getBytes();//轉化為位元組數組
for(int i=0;i<bytes.length;i++){
//向檔案輸出
os.write(bytes[i]);
}
os.close();//關閉流
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
複製代碼 方法二:全部一次寫入
public static void main(String[] args) {
try {
//建立一個檔案位元組輸出資料流對象
OutputStream os=new FileOutputStream("L:\\io.txt",true);//追加
//寫入的資料
String string="hello IO Stream";
byte[]bytes=string.getBytes();//轉化為位元組數組
os.write(bytes);//全部寫入
//os.write(bytes,0,5)表示從0開始,寫入長度為5個位元組
os.close();//關閉流
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
複製代碼 一.FileInputStream(檔案輸入資料流) FileInputStream是從系統的某個檔案中獲得輸入位元組,有兩個構造方法 1.FileInputStream(File file) 2.FileInputStream(String path)
讀取位元組方法1:一個個位元組讀取
public static void main(String[] args) {
try {
//建立一個位元組輸入資料流對象
InputStream is=new FileInputStream("L:\\io.txt");
int b=-1;
while((b=is.read())!=-1)//位元組讀取,當為-1時讀取完畢
{
System.out.print((char)b);//轉化為字元輸出
}
is.close();//關閉流
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
複製代碼 輸出結果為:hello IO Stream。這樣一個一個位元組讀取,速度很慢。
讀取位元組方法2:一次性讀取全部位元組
public static void main(String[] args) {
try {
File file=new File("L:\\io.txt");
//建立一個位元組輸入資料流對象
InputStream is=new FileInputStream(file);
//根據檔案大小來建立位元組數組
byte[]bytes=new byte[(int)file.length()] ;
int len=is.read(bytes);//返回讀取位元組的長度
System.out.println("讀取位元組長度為:"+len);
System.out.println("讀取的內容為: "+new String(bytes));//構建成字串輸出
is.close();//關閉流
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
複製代碼 運行結果: 650) this.width=650;" id="aimg_294" src="http://techfoxbbs.com/data/attachment/forum/201505/22/132250nrc1di5kpr2zi16d.png" class="zoom" width="349" alt="132250nrc1di5kpr2zi16d.png" />
這種讀取方法主要缺點是要構建一個和檔案大小一樣大的位元組數組,檔案小的時候還可以,當檔案很大,記憶體可能無法構架出如此大的位元組數組。所以,這種方法只適合小檔案。
讀取位元組方法3:每次讀取指定長度(最常用的方法)
public static void main(String[] args) {
try {
//建立一個位元組輸入資料流對象
InputStream is=new FileInputStream("L:\\io.txt");
//指定每次讀取的大小--可根據效能位元組修改
byte[]bytes=new byte[8] ;
StringBuffer sb=new StringBuffer();
int len=-1;//每次讀取的實際長度
while((len=is.read(bytes))!=-1)
{
sb.append(new String(bytes,0,len));
}
System.out.println("讀取位元組為:"+sb);
is.close();//關閉流
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
複製代碼 輸出結果: 650) this.width=650;" id="aimg_295" src="http://techfoxbbs.com/data/attachment/forum/201505/22/132320ku7uoivhohhz5h4h.png" class="zoom" width="336" alt="132320ku7uoivhohhz5h4h.png" /> |