簡體   English   中英

Android從資產讀取文件並存儲在數組列表中

[英]Android reading file from assets and storing in array list

我的應用程序需要讀取資產文件夾中的幾個文件。 但是我的文件中有分隔符$$$和||。 文件的結構是這樣的。

施工$$$
所有的工作都涉及組裝資源以及將形成新的或變更的設施所需的材料放在一起。

建築承包商$$$
與該組織訂立合同以進行建築工作的法人或個人。

以$$$結尾的句子將存儲在單獨的數組列表中,以||結尾的句子將存儲在單獨的數組列表中 將存儲在單獨的數組列表中。 我怎樣才能做到這一點? 任何示例或示例代碼將不勝感激。 請注意,這些文件非常長。

    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]);

上面是我現在發現的代碼,該代碼如何基於結束定界符分割句子?

假設每個句子都在一行中,並且以$$或||結尾,則可以根據其結尾將行存儲在不同的數組中:

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