从app android导出我的CSV文件数据

时间:2013-07-15 00:17:00

标签: java android csv export

我有一个班级:

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

第二节课:

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

如何从我的应用中将此信息保存到CSV文件中?我会提供从2.3.3到4.2.2的兼容性,所以我不会使用不能这样做的库。你能帮帮我一些代码吗?谢谢大家!!

另一件事......导出这个文件我必须在manifest.xml中添加什么权限?

1 个答案:

答案 0 :(得分:28)

您需要使用opencsv等库(在此处找到:http://sourceforge.net/projects/opencsv/

要将数据写入文件,您需要执行与此类似的操作:

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

(从此处修改:http://viralpatel.net/blogs/java-read-write-csv-file/

要将文件写入存储,您需要以下权限:

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