javaIO流(3):檔案位元組輸出資料流,javaio輸出資料流

來源:互聯網
上載者:User

javaIO流(3):檔案位元組輸出資料流,javaio輸出資料流
一:輸出資料流步驟


1.給出輸出資料流的目的地
2.建立指向目的地的輸出資料流
3.讓輸出資料流把資料寫入到目的地
4.關閉輸出資料流


位元組輸出資料流(OutputStream)。位元組輸出資料流的最大父類,是一個抽象類別,如果進行檔案輸出操作,要使用FileOutputStream進行執行個體化。


二:使用過程
1.構造方法
使用FileOutputStream類,下列具有重新整理功能的構造方法建立指向檔案的輸出資料流:


FileOutputStream(String  name);使用給定的檔案名稱name建立FileOutputStream流。
FileOutputStream(File  file);使用File對象建立FileOutptStream流


參數file和name指定的檔案稱為輸出資料流的目的地


使用方法一:

try{
FileOutputStream  out=new  FileOutputStream(“destin.txt”);
//建立指向destin.txt的輸出資料流
}
catch(IOException  e){
System.out.println(“File write  error”+e);
}


方法二:


File  f=new  File(“destin.txt”);//指定輸出資料流的目的地
try{
FileOutputStream  out=new  FileOutPutStream(f);建立指向目的地的輸出資料流
}
catch(IOException  e){
System.out.println(“File  read  error”+e);
}


可以使用FileOutPutStream類的下列功能選擇是否具有重新整理功能的構造方法建立指向檔案的輸出資料流:
FileOutPutStream(String  name,boolean  append);
FileOutPutStream(File  file,boolean  append);
假設檔案存在,當使用構造方法建立一個指向檔案的輸出資料流時,如果參數ppend取值true,輸出資料流不會重新整理所指
向的檔案,append如果去false,輸出資料流就會重新整理所指向的檔案。


2.使用輸出資料流寫位元組


FileOutputStream流順序的向檔案寫入內容,每次調用write方法就順序向檔案寫入內容,位元組輸出資料流的write方
法以位元組為單位向目的地寫資料:


void  write(int  n)調用該方法,向目的地寫入單個位元組
void  write(byte  b[])向目的地寫入一個位元組數組
void  write(byte  b[],int  off,int  len)給定位元組的數組中起始於位移量off處取len個位元組寫到目的地

void  close();關閉輸入資料流


三:案例

案例一:

<span style="font-size:14px;">package com.cloud.day1;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;public class Demo5 {public static void main(String[] args) {byte []a="Hello.world".getBytes();byte []b="Hello.China".getBytes();File file=new File("a.txt");try {//1.建立只想目的地的輸出資料流OutputStream out=new FileOutputStream(file);System.out.println(file.getName()+"的大小"+file.length()+"位元組");//2.向目的地寫入資料out.write(a);out.close();//準備在檔案末尾加內容out=new FileOutputStream(file,true);System.out.println(file.getName()+"的大小"+file.length()+"位元組");out.write(b, 0, b.length);System.out.println(file.getName()+"的大小"+file.length()+"位元組");out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}</span>


案例二:

<span style="font-size:14px;">package com.cloud.day1;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;public class Demo6 {public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("D:\\IO.txt");OutputStream os=null;//執行的代碼,其中可能有異常。一旦發現異常,則立即跳到catch執行。否則不會執行catch裡面的內容 try {//可能出現的錯誤os=new FileOutputStream(file);String str="Hello,world";byte []f=str.getBytes();//單個位元組輸入的方法for(int i=0;i<f.length;i++){os.write(f[i]);}} //列印出錯資訊catch (Exception e) {e.printStackTrace();}//不管什麼情況都會執行,包括try catch 裡面用了return ,可以理解為只要執行了try或者catch,就一定會執行 finallyfinally{try {if(os!=null){os.close();}} catch (Exception e2) {e2.printStackTrace();}}}}</span>



著作權聲明:博主原創文章,轉載請說明出處。http://blog.csdn.net/dzy21

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.