简体   繁体   English

从/ res / raw /检索列表数据并将其存储到Android上的2D数组的最快方法

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

I'm working on my first android app, so feel free to point out if I'm doing things incorrectly or suggest another way of doing things. 我正在开发我的第一个android应用程序,所以请随时指出我是在做错事情还是建议另一种做事方法。

I have a large file "table". 我有一个大文件“表”。 Now, I can store the data as a csv like so 现在,我可以像这样将数据存储为csv

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

or as xml like so 或像这样的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>

I'll use whichever I can retrieve and store faster. 我将使用任何可以更快地检索和存储的东西。 However, I don't want to hardcode the data. 但是,我不想对数据进行硬编码。

I need to read the data from /res/raw/table.csv or /res/raw/table.xml and store it into a list of lists, an array of lists, or an array of arrays, again, I'll use whichever is fastest. 我需要从/res/raw/table.csv/res/raw/table.xml读取数据,并将其存储到列表列表,列表数组或数组数组中,再次使用以最快的速度为准。

I've tried a couple things: 我已经尝试了几件事:

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

Where readTextFile is 其中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();
    }

This took too long, so thinking Scanner might be too slow and lists might take longer than arrays I tried: 这花费了太长时间,因此认为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];
    }

Which also took too long. 这也花费了太长时间。

What's a better method of doing this? 有什么更好的方法?

Thanks in advance. 提前致谢。

Try to refer following links. 尝试引用以下链接。 They should help you. 他们应该帮助您。

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

http://opencsv.sourceforge.net/ http://opencsv.sourceforge.net/

These libraries will take care of reading and writing from and to CSV. 这些库将负责从CSV读取和写入CSV。

For XML refer following link. 对于XML,请参考以下链接。

http://simple.sourceforge.net/ http://simple.sourceforge.net/

It has very good documentation too. 它也有很好的文档。

Here's the way to do it using openCSV like EagleEye recommended. 这是使用像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();

To add a library in Android Studio, you can download the jar file here: http://sourceforge.net/projects/opencsv/ 要在Android Studio中添加库,您可以在此处下载jar文件: http : //sourceforge.net/projects/opencsv/

And then add it to your libs folder which should be in the same directory as your build in and src folders. 然后将其添加到您的libs文件夹中,该文件夹应与build in和src文件夹位于同一目录中。

Then in your build.gradle file under dependencies add the line compile files('libs/opencsv-2.4.jar') like so 然后在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