简体   繁体   English

Android从资产读取文件并存储在数组列表中

[英]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()]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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