简体   繁体   中英

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.

I have a large file "table". Now, I can store the data as a csv like so

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

or as xml like so

<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.

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

  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:

    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/

http://opencsv.sourceforge.net/

These libraries will take care of reading and writing from and to CSV.

For XML refer following link.

http://simple.sourceforge.net/

It has very good documentation too.

Here's the way to do it using openCSV like EagleEye recommended.

    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/

And then add it to your libs folder which should be in the same directory as your build in and src folders.

Then in your build.gradle file under dependencies add the line compile files('libs/opencsv-2.4.jar') like so

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
}

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