學習JAVA的標準資料流和輸入輸出資料流

來源:互聯網
上載者:User

 

學習JAVA的標準資料流和輸入\輸出資料流

學習筆記    宋鶴賢 2011-8-29

資料流分為輸入資料流(InputStream)和輸出資料流(OutputScream)兩大類。

採取資料流的目的是,使程式的輸入\輸出操作獨立於相關裝置。因為每個裝置的實現細節由系統執行完成,所以程式中不需要關心這些細節問題,使得一個程式能夠用於多種輸入\輸出裝置,不需要對原始碼甚至目標代碼做任何修改,從而增強程式的可移植性。

Java的標準資料流

1、標準輸入System.in

Public int read() throws IOException:返回讀入的一個位元組,如果到達留的末尾,則返回-1。

2、標準輸出System.out

Public int read(byte[] i) throws IOException:返回讀入緩衝區的位元組總位元組數,如果因為已經到達流末尾而不再有資料可用,則返回-1。

使用read()方法發生IO錯誤時,拋出IOException異常。

3、標準的錯誤輸出 System.err

位元組流

1.位元組輸入資料流InputStream類

InputStream類是抽象類別,不能直接產生對象,它是所有位元組輸入資料流類的父類。該類提供了輸入處理的基本方法,它的子類一般都重寫這些方法。

注意:該類中的大多數方法都可能拋出IOException異常,因此調用它們時,應放在try…catch塊中,捕獲和處理IOException異常。

讀取資料的方法

int read() throws IOException ; 

int read(byte[] b) throws IOException ;

int read(byte[] b,int off,int len) throws IOException ;

關閉輸入資料流

public void close() throws IOException;

擷取流中可讀的位元組數

public int available() throws IOException;

移動讀取指標

public long skip(long n) throws IOException;

標記流中的位置和重設讀取位置

public boolean markSupported();

public void mark(int readlimit);

public void reset();

2.位元組輸出資料流 OutputStream

 OutputStream類是抽象類別,不能直接產生對象,它是所有位元組輸出資料流類的父類。該類提供了輸出處理的基本方法,它的子類一般都重寫這些方法。

輸出資料的方法

void write(int b) throws IOException ; 

void write(byte[] b) throws IOException ;

void write(byte[] b,int off,int len) throws IOException ;

關閉輸出資料流

public void close() throws IOException;

清空緩衝區

public void flush() throws IOException;

檔案位元組輸入/輸出流類

檔案資料流類FilelnputStream和FileOutputStream是用於進行檔案輸入輸出的處理,其資料對象都是檔案。 

檔案位元組輸入資料流類FileInputStream

FileInputStream用於順序訪問本地檔案。它從父類InputStream中繼承read()、close()等方法對本機上的檔案進行操作,但不支援mark()方法和reset()方法。 

構造方法

public FileInputStream(string name) throws FileNotFoundException

public FileInputStream(File file) throws FileNotFoundException

讀取位元組的方法

     public int read() throws IOException

     public int read(byte[] b)throws IOException

     public int read(byte[] b,int off,int len) throws IOException

例題:開啟檔案

import java.io.*;

public class ReadFileTest

{

   public static void main(String[] args) throws IOException

   {

  try

  { 

 //建立檔案輸入資料流對象

 FileInputStream fis =new FileInputStream("ReadFileTest.java");       

         int n=fis.available();        

         byte b[]=new byte[n];        

         //讀取輸入資料流         

         while((fis.read(b,0,n))!=-1)    

         {                  

          System.out.print(new String(b));         

         }

         System.out.println();

         //關閉輸入資料流

         fis.close();    

      }

      catch(IOException ioe)

      {

         System.out.println(ioe.getMessage());

      }

      catch(Exception e)

      {

         System.out.println(e.getMessage());

      }

}

}

用available()方法得到輸入資料流的可讀取位元組數,再用read(byte[]b)方法,從來源程式檔案ReadFileTest.java中讀取檔案儲存到位元組數組b中,然後將以b中的值構造字串new String(b) 顯示在螢幕上。程式運行時,將來源程式檔案ReadFileTest.java的內容顯示在螢幕上。

檔案位元組輸出資料流FileOutputStream類 

 FileOutputStream類用於向一個檔案寫資料。它從超類OutputStream中繼承write()、close()等方法。

構造方法

public FileOutputStream(String name) throws FileNotFoundException

public FileOutputStream(File file) throws FileNotFoundException

public FileOutput.Stream (String name,boolean append) throws FileNotFoundException

寫入位元組的方法

public void write(int b) throws IOException

public void write(byte[] b) throws IOException

public void write(byte[] b,int off,int len) throws IOException

例題12.3 寫入檔案

import java.io.*;

public class WriteFileTest

{

public static void main(String[] arg)

    {

       try

       {

         System.out.println("please input:");

         int count;

         byte b[]=new byte[512];

         count=System.in.read(b);    

         // 讀取標準輸入資料流

         FileOutputStream fos=new FileOutputStream("data.txt");

         //建檔案輸出資料流對象

         fos.write(b,0,count);        

         //寫入檔案

         fos.close();   

         //關閉輸出資料流

         System.out.println("儲存檔案成功!");

       }

       catch(IOException ioe)

       {

          System.out.println(ioe.getMessage());

       }

   }

}

相關文章

聯繫我們

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