Android data buffer and data stream learning Summary

Source: Internet
Author: User

 

Android data buffer and data stream learning Summary (BufferedWriter, BufferedOutputStream, and FileOutputStream)

Before summarizing the old Huo data stream, let's take two examples.

 

======================== The following is the correct data storage method ======================== ===

 

 

/**

* Save the byte array as a file

*

* @ 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;

}

 

= ===

This problematic method has caused us a headache for a few days. After several days of the weekend, we finally solved the problem by using the above method for storage.

 

What kind of errors will happen? Let's talk about it. In fact, we only need to first read the data and store it after processing. Instead, we have taken a detour,

 

1 is slow. 2. Processed data and errors.

/**

* Save data packets

* @ 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 (clswavedi.pdf. this, "Start measurement", 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 ){

}

}

 

The knowledge points used are summarized as follows:

========================== BufferedReader and inputstream ======

The usage of bufferedreader is more complex than that of inputstream. complicated existence will inevitably lead to advantages!

Inputstream reads one byte and one byte. IO is executed every time it is read. We know that io operations are time-consuming, which will inevitably lead to program efficiency, bufferedreader can solve this problem very well. It can read a large amount of data at a time, greatly reducing the number of I/O operations, and the efficiency is like a bus with hundreds of people, pulling one student to school at a time, it is different from pulling 100 at a time.

======================== BufferedOutputStream and FileOutputStream =================

 

Note: BufferedOutputStream must be used in conjunction with FileOutputStream.

In the example of FileInputStream and FileOutputStream, a byte array is used as the buffer for Data Reading. Taking file access as an example, the hard disk access speed is much lower than the data access speed in the memory. To reduce access to the hard disk, data of a certain length is usually read from the file at a time, and data of a certain length is written at the time of writing, which can increase the efficiency of file access. Java. io. BufferedInputStream and java. io. BufferedOutputStream can add the buffer function for InputStream and OutputStream objects.

The data member buf of BufferedOutputStream is a single-digit group. The default value is 512 bytes. When data is written using the write () method, the data is actually written to the buf first. When the buf is full, the write () method of the given OutputStream object is implemented, write the buf data to the destination, instead of writing the data to the destination every time.

To ensure that the data in the buffer zone must be written to the destination, we recommend that you execute flush () to write all the data in the buffer zone to the destination stream.

 

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

The Java Standard Library provides the static method System. arraycopy (), using it to copy an array is much faster than using for loop replication, System. arraycopy () is overloaded for all types. Five parameters are required.

The first parameter is the source array.

The second parameter is the offset, that is, the index from which the replication starts.

Third parameter: target array.

Fourth parameter: offset.

Fifth parameter: number of elements to be copied from the source array to the target array, which is generally the length of the target array.

Example:

Copy elements from array A to array 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] + "");

}

}

}

Running result: null H e;

 

 

If it is reproduced, indicate the source. Produced by laohuo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.