Home  >  Article  >  Java  >  In-depth analysis based on reading and parsing the a.txt file in the assets directory in Android

In-depth analysis based on reading and parsing the a.txt file in the assets directory in Android

高洛峰
高洛峰Original
2017-01-17 15:20:521356browse

android reads the contents of the assets file, generally using the getAsset.open() method, and passing the file path as a parameter. When we parse a file in a directory and need to parse it, For example: the content of a.txt file is:
name
android,liu
class
1,2,3,4
These files are sometimes in the same format as database files. We It needs to be parsed.
We know that after obtaining the assets file, an inputstream is returned instead of a file type, so we need to parse the inputstream. It is mainly divided into two stages: the first stage is to remove line breaks, and the second stage is to decompose each line of information. (ps: I am a novice, this is a method I tried myself, I hope there is a better way to parse)
We need to store the content in the inputstream, first temporarily store it in a byte[] array, Then put the byte array into the string, and then parse the string.

The first stage: remove newlines. Through the string.split("[\\n]") method (ps: why use [\\n]? You can check the regular expression for this, and [\\n] means matching newlines), so that we The split results need to be stored in a string array.

The second stage: decompose each line of information. It can be said that the first stage is to store the information of each line into the string array. The next step is to parse each string. The string.split() method also needs to be used, because we use commas as separators. symbol, so "[,]" is used as a parameter to split. At the same time, it should be noted that when converting data, the spaces in the string need to be removed, and the string.trim() method is used, so that the string can be converted into int.
The next step is how to use this information into the program.

For more in-depth analysis based on reading and parsing the a.txt file in the assets directory in android, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn