A summary of the operation of file reading (input and output streams) in Android _android

Source: Internet
Author: User

1. the principle of file reading and writing in Android:
(1). All files are stored in bytes.
(2). The characters that are not files are reserved on the disk, but the characters are encoded into bytes before they are stored to disk.
(3). When reading a file (especially a text file), it is also a byte-by-byte read to form a byte sequence.

2. the difference between the byte stream and the character streams:

(1). A byte stream provides the ability to handle any type of IO operation, but it cannot handle Unicode characters directly, and the character streams can.
(2). Byte-flow conversion to character streams can be inputstreamreader,outputstreamwriter.
In general, we usually use the word stream when using.

3. file Read and write steps:

(1). First establish the channel.
(2). Then set up the data storage space.
(3). Then start reading and writing data.
(4). Close the stream.

4. input and output streams:

(1). Input stream: The program reads the data source from the input stream. The data source includes the outside (keyboard, file, network ...), which is to read the data source into the communication channel of the program.

(2). Output stream: The program writes data to the output stream. Output data from the program to the outside (monitor, printer, file, network ...) ) Communication channel.

The code is as follows:

public static byte[] Readinputstream (InputStream inputstream) {

    //1. Establish Channel objects
    Bytearrayoutputstream OutputStream = new Bytearrayoutputstream ();
    2. Define storage space
    byte[] buffer = new byte[1024];
    3. Start reading file
    int len =-1;
    try {
      if (InputStream!= null) {while
        (len = inputstream.read (buffer)!=-1) {
          //write data in buffer to Outputstrea Outputstream.write in M object
          (buffer, 0, Len);
        }
      4. Close Flow
      outputstream.close ();
      Inputstream.close ();
    } catch (IOException e) {
      e.printstacktrace ();
    }
    return Outputstream.tobytearray ();
  }

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.