简体   繁体   中英

Android reading file from assets and storing in array list

My app needs to read from several files in the assets folder. But my file has delimiters $$$ and ||. The structure of the file is like this.

Construction$$$
All the work involved in assembling resources and putting together the materials required to form a new or changed facility.||

Construction Contractor$$$
A corporation or individual who has entered into a contract with the organization to perform construction work.||

The sentences ending with $$$ are to be stored in seperate array list and the sentences ending with || are to be stored on seperate array list. How can i do this? Any sample or example code will be appreciated. Note that these files are very long.

    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open("c.txt"))); //throwing a FileNotFoundException?
        String word;
        while((word=br.readLine()) != null)
            A_Words_array.add(word); //break txt file into different words, add to wordList
    }
    catch(IOException e) {
        e.printStackTrace();
    }
    finally {
        try {
            br.close(); //stop reading
        }
        catch(IOException ex) {
            ex.printStackTrace();
        }
    }
    String[]words = new String[A_Words_array.size()];
    A_Words_array.toArray(words); //make array of wordList

    for(int i=0;i<words.length; i++)
        Log.i("Read this: ", words[i]);

Above is the code i found now how to split my sentences based upon ending delimiters?

Asuming that each sentence is in one line and they finish either with $$$ or ||, you can store the lines in different arrays depending on its endings:

List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>();
String line;
while (line = br.readLine()) != null) {
    if (line.endsWith("$$$")) {
        list1.add(line);
    } else {
        list2.add(line);
    }
}
String[] dollarlines = list1.toArray(new String[list1.size()]);
String[] verticalLines = list2.toArray(new String[list2.size()]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM