Android data buffer and data stream learning Summary (bufferedwriter, bufferedoutputstream, and fileoutputstream)

Source: Internet
Author: User

 

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.