导出从应用程序的Andr​​oid我的CSV文件数据我的、应用程序、文件、数据

2023-09-05 01:09:40 作者:水果硬糖

我有一个类:

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,所以我不会用库,无法做到这一点。你能帮我WIRTE一些code?谢谢大家!

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

另外thing..to导出该文件的权限是什么,我必须在manifest.xml的增加?

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

推荐答案

您需要使用的库如opencsv(这里找到:的 http://sourceforge.net/projects/opencsv/ )

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

(修改从这里开始: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" />