簡體   English   中英

從/ res / raw /檢索列表數據並將其存儲到Android上的2D數組的最快方法

[英]Fastest way to retrieve tabulated data from /res/raw/ and store it into a 2D array on Android

我正在開發我的第一個android應用程序,所以請隨時指出我是在做錯事情還是建議另一種做事方法。

我有一個大文件“表”。 現在,我可以像這樣將數據存儲為csv

a, "stuff with , commas"
b, "foo bar"
c, "test,"

或像這樣的xml

<row>
    <number>a</number>
    <equipment>"stuff with , commas"</equipment>
</row>
<row>
    <number>b</number>
    <equipment>"foo bar"</equipment>
</row>
<row>
    <number>c</number>
    <equipment>"test"</equipment>
</row>

我將使用任何可以更快地檢索和存儲的東西。 但是,我不想對數據進行硬編碼。

我需要從/res/raw/table.csv/res/raw/table.xml讀取數據,並將其存儲到列表列表,列表數組或數組數組中,再次使用以最快的速度為准。

我已經嘗試了幾件事:

    InputStream equipmentCSV =  getResources().openRawResource(R.raw.equipment);
    String equipmentString = readTextFile(equipmentCSV);

    ArrayList<ArrayList<String>> equipmentTable = new ArrayList<ArrayList<String>>();

    Scanner scanner = new Scanner(equipmentString);

    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        String [] splitLines = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
        ArrayList <String> splitLinesList = new ArrayList<String>(Arrays.asList(splitLines));
        equipmentTable.add(splitLinesList);
    }

其中readTextFile是

  public String readTextFile(InputStream inputStream) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {

        }
        return outputStream.toString();
    }

這花費了太長時間,因此認為Scanner可能太慢並且列表可能比我嘗試的數組花費更長的時間:

    String[] lines = equipmentString.split(System.getProperty("line.separator"));
    String [][] equipmentTable = new String[lines.length][2];

    for(int i = 0; i < lines.length ; i++){
        String [] splitLines = lines[i].split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
        equipmentTable[i][0] = splitLines[0];
        equipmentTable[i][1] = splitLines[1];
    }

這也花費了太長時間。

有什么更好的方法?

提前致謝。

嘗試引用以下鏈接。 他們應該幫助您。

https://code.google.com/p/secrets-for-android/source/browse/trunk/src/au/com/bytecode/opencsv/

http://opencsv.sourceforge.net/

這些庫將負責從CSV讀取和寫入CSV。

對於XML,請參考以下鏈接。

http://simple.sourceforge.net/

它也有很好的文檔。

這是使用像EagleEye推薦的openCSV做到的方法。

    List<String []> equipmentList = new ArrayList< String [] >();

    try {
        // where your file in /res/raw/equipment.csv
        InputStream file = getResources().openRawResource(R.raw.equipment);
        CSVReader reader = new CSVReader(new InputStreamReader(file));

        String [] nextLine;
        while ((nextLine = reader.readNext()) != null) {
            equipmentList.add(nextLine);
        }
    } catch(IOException ie){
        // deal with exception
    }

    // if you want to convert it to a 2d array from an arraylist of arrays
    String[][] equipmentTable = (String[][])equipmentList.toArray();

要在Android Studio中添加庫,您可以在此處下載jar文件: http : //sourceforge.net/projects/opencsv/

然后將其添加到您的libs文件夾中,該文件夾應與build in和src文件夾位於同一目錄中。

然后在dependencies項下的build.gradle文件中添加如下行compile files('libs/opencsv-2.4.jar')

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
    // whatever other dependencies ...
    compile files('libs/opencsv-2.4.jar') // add this line for your jar file
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM