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;
如轉載,請註明出處。老霍出品