Java——位元組流,字元流

來源:互聯網
上載者:User

標籤:聲明變數   row   編碼   main   run   dem   static   throw   rect   

一、位元組輸出資料流OutputStream抽象類別

此抽象類別,是表示輸出位元組流的所有類的超類。操作的資料都是位元組,定義了輸出位元組流的基本共性功能方法。

  • 位元組: 這樣流每次只操作檔案中的1個位元組
  • 流對象:操作檔案的時候,自己不做依賴作業系統

作用:從Java程式,寫入檔案(可以寫任意檔案)

方法:

  • write(int b) 寫入1個位元組
  • write(byte[] b) 寫入位元組數組
  • write(byte[] b,int,int)寫入位元組數組,int 開始寫入的索引, int 寫幾個
  • close() 方法,關閉流對象,釋放與此流相關的資源
FileOutputStream類

OutputStream有很多子類,其中子類FileOutputStream可用來寫入資料到檔案。

FileOutputStream類,即檔案輸出資料流,是用於將資料寫入File的輸出資料流。

流對象使用步驟:

  • 1. 建立流子類的對象,綁定資料目的
  • 2. 調用流對象的方法write寫
  • 3. close釋放資源
1、寫入檔案
public static void main(String[] args)throws IOException {//流對象的構造方法,可以建立檔案,如果檔案存在,直接覆蓋FileOutputStream fos = new FileOutputStream("c:\\a.txt");//流對象的方法write寫資料//寫1個位元組,查詢編碼錶fos.write(97);//寫位元組數組byte[] bytes = {65,66,67,68};fos.write(bytes);//寫位元組數組的一部分,開始索引,寫幾個fos.write(bytes, 1, 2);//寫入位元組數組的簡便方式//寫字串fos.write("hello".getBytes());//關閉資源fos.close();}
2、IO流的異常處理
import java.io.FileOutputStream;import java.io.IOException;/* *   IO流的異常處理 *   try catch finally *    *   細節: *     1. 保證流物件變數,範圍足夠 *     2. catch裡面,怎麼處理異常 *         輸出異常的資訊,目的看到哪裡出現了問題 *         停下程式,從新嘗試 *     3. 如果流對象建立失敗了,需要關閉資源嗎 *         new 對象的時候,失敗了,沒有佔用系統資源 *         釋放資源的時候,對流對象判斷null *         變數不是null,對象建立成功,需要關閉資源 */public class FileOutputStreamDemo3 {public static void main(String[] args) {//try 外面聲明變數,try 裡面建立對象FileOutputStream fos = null;try{fos = new FileOutputStream("s:\\a.txt");fos.write(100);}catch(IOException ex){System.out.println(ex);throw new RuntimeException("檔案寫入失敗,重試");}finally{try{if(fos!=null)      fos.close();}catch(IOException ex){throw new RuntimeException("關閉資源失敗");}}}}
3、字串轉成byte數組

"hello,world!".getBytes()

4、檔案續寫

FileOutputStream fos = new FileOutputStream(file, true);

二、位元組輸入資料流InputStream抽象類別

通過InputStream可以實現。InputStream此抽象類別,是表示位元組輸入資料流的所有類的超類。,定義了位元組輸入資料流的基本共性功能方法。

  • int read():讀取一個位元組並返回,沒有位元組返回-1.

  • int read(byte[]): 讀取一定量的位元組數,並儲存到位元組數組中,返回讀取到的位元組數,沒有位元組返回-1.。

 

FileInputStream類

 

輸入資料流讀取檔案的步驟

  • 1. 建立位元組輸入資料流的子類對象
  • 2. 調用讀取方法read讀取
  • 3. 關閉資源

read()方法,

  • read()執行一次,就會自動讀取下一個位元組
  • 傳回值,返回的是讀取到的位元組, 讀取到結尾返回-1
1、讀取單個位元組
public static void main(String[] args) throws IOException{FileInputStream fis = new FileInputStream("c:\\a.txt");//讀取一個位元組,調用方法read 返回int//使用迴圈方式,讀取檔案,  迴圈結束的條件  read()方法返回-1int len = 0;//接受read方法的傳回值    while( (len = fis.read()) != -1){  // 傳回值儲存到len中System.out.print((char)len);}//關閉資源fis.close();}
2、讀取一個位元組數組
public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("c:\\a.txt");//建立位元組數組byte[] b = new byte[1024];int len = 0 ;while( (len = fis.read(b)) !=-1){  // 儲存到位元組數組b中System.out.print(new String(b,0,len));}fis.close();}
Copy檔案
public static void main(String[] args) {long s = System.currentTimeMillis();FileInputStream fis = null;FileOutputStream fos = null;try{fis = new FileInputStream("c:\\t.zip");fos = new FileOutputStream("d:\\t.zip");//定義位元組數組,緩衝byte[] bytes = new byte[1024*10];//讀取數組,寫入數組int len = 0 ; while((len = fis.read(bytes))!=-1){fos.write(bytes, 0, len);}}catch(IOException ex){System.out.println(ex);throw new RuntimeException("檔案複製失敗");}finally{try{if(fos!=null)fos.close();}catch(IOException ex){throw new RuntimeException("釋放資源失敗");}finally{try{if(fis!=null)fis.close();}catch(IOException ex){throw new RuntimeException("釋放資源失敗");}}}long e = System.currentTimeMillis();System.out.println(e-s);}
三、字元輸入/輸出流

 

1、字元輸入——FileReader類

字元輸入資料流只能讀取文字檔,所有字元輸入資料流的超類為java.io.Reader類

  • int read() 讀取1個字元
  • int read(char[] c) 讀取字元數組
public static void main(String[] args) throws IOException{FileReader fr = new FileReader("c:\\1.txt");// 讀取單個字元/*int len = 0 ;while((len = fr.read())!=-1){System.out.print((char)len);}*///字元數組char[] ch = new char[1024];int len = 0 ;while((len = fr.read(ch))!=-1){System.out.print(new String(ch,0,len));}fr.close();}
2、字元輸出資料流——FileWriter

所有字元輸出資料流的超類為java.io.Writer 

  • write(int c) 寫1個字元
  • write(char[] c)寫字元數組
  • write(char[] c,int,int)字元數組一部分,開始索引,寫幾個
  • write(String s) 寫入字串
public static void main(String[] args) throws IOException{FileWriter fw = new FileWriter("c:\\1.txt");//寫1個字元fw.write(100);fw.flush();//寫1個字元數組char[] c = {‘a‘,‘b‘,‘c‘,‘d‘,‘e‘};fw.write(c);fw.flush();//寫字元數組一部分fw.write(c, 2, 2);fw.flush();//寫入字串fw.write("hello");fw.flush();fw.close();}
Copy檔案
public static void main(String[] args) {FileReader fr = null;FileWriter fw = null;try{fr = new FileReader("c:\\1.txt");fw = new FileWriter("d:\\1.txt");char[] cbuf = new char[1024];int len = 0 ;while(( len = fr.read(cbuf))!=-1){fw.write(cbuf, 0, len);fw.flush();}}catch(IOException ex){System.out.println(ex);throw new RuntimeException("複製失敗");}finally{try{if(fw!=null)fw.close();}catch(IOException ex){throw new RuntimeException("釋放資源失敗");}finally{try{if(fr!=null)fr.close();}catch(IOException ex){throw new RuntimeException("釋放資源失敗");}}}}

 

Java——位元組流,字元流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.