Based on Android, read the A.txt file in the assets directory and analyze it deeply _android

Source: Internet
Author: User
Android reads the contents of the assets file, usually using the Getasset.open () method, and passing the path of the file as a parameter, and when we parse the file in a directory when we need to parse it, for example: the contents of the A.txt file are:
Name
Android,liu
Class
1,2,3,4
These files are sometimes like the format of a database file, and we need to parse them.
We know that getting the assets file returns a InputStream instead of a file type, so we need to parse the InputStream. It is divided into two phases: the first phase is the line break, and the second is the decomposition of each line of information. PS: I am a rookie, this is my own try a method, I hope there is a better way to parse it.
We need to store the contents of the InputStream, temporarily stored in a byte "" array, then put the byte array in a string, and then parse the string.

First stage: go to line break. Through String.Split ("[\\n]") method PS: Why use [\\n], this can refer to regular expressions, and [\\n] means to match line breaks, so that we need to store the result of the split in a string array.

Phase two: decompose each line of information. It can be said that the first stage is to store each line of information into a string array, and then to parse each string, the same need to use the String.Split () method, because we use the comma as a delimiter, so "[,]" as a parameter to split, Also note that you need to remove the whitespace from the string in the conversion of the data and use the String.Trim () method so that you can convert the string to int.
The next step is how to use the information in the program.
Related Article

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.