2025-05-23

 

Android數據緩沖區和數據流的學習總結(BufferedWriter、BufferedOutputStream和FileOutputStream)

老霍數據流總結之前,先上2個例子。

 

 ==================下面是正確的數據存儲方法==================

 

 

 /**

     * 把字節數組保存為一個文件

     * 

     * @param b

     * @param outputFile

     * @return

     */ 

    public static File getFileFromBytes(byte[] b, String outputFile) { 

        File ret = null; 

        BufferedOutputStream stream = null; 

        try {

               ret = new File(outputFile); 

               FileOutputStream fstream = new FileOutputStream(ret); 

               stream = new BufferedOutputStream(fstream); 

               stream.write(b);

         

        } catch (Exception e) { 

//            log.error("helper:get file from byte process error!"); 

            e.printStackTrace(); 

        } finally { 

            if (stream != null) { 

                try { 

                    stream.close(); 

                } catch (IOException e) { 

//                    log.error("helper:get file from byte process error!"); 

                    e.printStackTrace(); 

                } 

            } 

        } 

        return ret; 

    }

 

 ==================下面是錯誤的數據存儲方法==================

這個有問題的方法,很讓我們頭疼瞭幾天,周末多宅瞭幾天終於解決,就是用上面的方法存儲才是正確的。

 

到底會出現什麼樣的錯誤呢?大傢不妨說說。其實,我們隻需要初始讀來的數據,處理後再保存,反而是走瞭彎路,

 

1 是慢  2 是經處理的數據還有錯誤

  /**

  * 保存數據包

  * @param buffer

  * @throws IOException

  */

   public void saveDAT(byte[] buffer) throws IOException

 {

     SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhh");  //yyyyMMddhhmmss

  String wid=sDateFormat.format(new java.util.Date());

 

  //Toast.makeText(ClsWaveDiagram.this, "開始測量", 1000).show();

  Log.i("wid:", wid);

  try{

   BufferedWriter bw = new BufferedWriter(new FileWriter(

         "/sdcard/Data/"+wid, true));

   for ( int i = 0; i < buffer.length; i++ ) {

    if(buffer[i]==(byte)0xAA ){ //&& buffer[i+1]==(byte) 0x55

     bw.write(buffer[i+0] & 0xFF);    //AA

     bw.write(buffer[i+1] & 0xFF);    //55

     bw.write(buffer[i+2] & 0xFF);  //XD

      }

   }

   bw.flush();

   //bw.close();

  }

  catch (IOException e) {

  }

 }

 

用到的知識點詳細總結如下 

  ==================bufferedReader和inputstream的執行讀文件的速度比較==================

  bufferedreader的用法比inputstream要復雜,復雜的存在必然會導致優勢的存在!

  inputstream是一個字節一個字節的讀取,每次讀取都會執行一次IO,我們知道io的操作是很費時間的,這就必然會導致程序的效率,  bufferedreader很好的解決這一問題,它可以一次讀取大量的數據,大大減少瞭io次數,效率也就上去瞭好比百人座的大巴,一次拉1個學生到學校,和一次拉100個是效率不一樣的。

  ==================BufferedOutputStream和FileOutputStream==================

 

註意:BufferedOutputStream 一定要結合FileOutputStream實用。

   FileInputStream和FileOutputStream的例子中,使用瞭一個byte數組來作為數據讀入的緩沖區,以文件存取為例,硬盤存取的速度遠低於內存中的數據存取速度。為瞭減少對硬盤的存取,通常從文件中一次讀入一定長度的數據,而寫入時也是一次寫入一定長度的數據,這可以增加文件存取的效率。java.io.BufferedInputStream與java.io.BufferedOutputStream可以為InputStream、OutputStream類的對象增加緩沖區功能。

   BufferedOutputStream的數據成員buf是一個位數組,默認為512字節。當使用write()方法寫入數據時,實際上會先將數據寫至buf中,當buf已滿時才會實現給定的OutputStream對象的write()方法,將buf數據寫至目的地,而不是每次都對目的地作寫入的動作。

   為瞭確保緩沖區中的數據一定被寫出至目的地,建議最後執行flush()將緩沖區中的數據全部寫出目的流中。 

 

  ==================System.arraycopy(temp, 0, btBuf, 0, btBuf.length);==================

 Java標準類庫提供static方法System.arraycopy(),用它復制數組比用for循環復制要快得多,     System.arraycopy()針對所有的類型做瞭重載,需要5個參數。

    第一個參數:源數組。

    第二個參數:偏移量,即從哪個位置開始復制的索引。

    第三個參數:目標數組。

    第四個參數:偏移量。

    第五個參數:要從源數組中復制到目標數組元素的個數,一般情況下為目標數組的長度。

例:

    從A數組中復制元素到B數組?

    public class Practice {

        public static void main(String[] args){

            String[] A = {"H","e","l","l","o"};

            String[] B = new String[3];

            System.arraycopy(A, 0, B, 1, B.length – 1);

          for(int i = 0; i < B.length; i ++){

                 System.out.print(B[i] + " ");

            }

      }

    }

    運行結果為:null H e;

 

 

如轉載,請註明出處。老霍出品

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *