您的位置:首页 > 移动开发 > Android开发

Android数据缓冲区和数据流的学习总结

2012-04-28 13:41 232 查看
http://www.2cto.com/kf/201111/109764.html

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;

如转载,请注明出处。老霍出品

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: