简体   繁体   中英

Export my data on CSV file from app android

I have a class:

public class A() {
private List<B> e;
private int nE;
private int nC;
private int tC;
private int bL;
private float mA;
private float mP;
private int eP;`      }

And a second class:

public class B() {
private String m;
private int v;
private int l;
private int c;
private String d;
private String n;
private Calendar data;
private int t;   }

How can i do to save this information into a CSV file from my app? I would offer compatibility from 2.3.3 to 4.2.2, so i wouldn't use library that cannot do this. Can you help me wirte some code? Thank you all!!

Another thing..to export this file what permission i must add in manifest.xml?

You'll need to use a library such as opencsv (found here: http://sourceforge.net/projects/opencsv/ )

To write data to a file you'll need to do something similar to this:

String csv = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
CSVWriter writer = new CSVWriter(new FileWriter(csv));

List<String[]> data = new ArrayList<String[]>();
data.add(new String[] {"India", "New Delhi"});
data.add(new String[] {"United States", "Washington D.C"});
data.add(new String[] {"Germany", "Berlin"});

writer.writeAll(data);

writer.close();

(modified from here: http://viralpatel.net/blogs/java-read-write-csv-file/ )

to write a file to storage you will need the following permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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