How can create .csv file in android application, in which we will store some data like some employe details?
2 Answers
download library from here
and jar file add to your application.
Inside your application do
CSVWriter writer = null;
try
{
writer = new CSVWriter(new FileWriter("/sdcard/myfile.csv"), ',');
String[] entries = "first#second#third".split("#"); // array of your values
writer.writeNext(entries);
writer.close();
}
catch (IOException e)
{
//error
}
2 Comments
Manoj Kumar
u there, and can help for that using this code, the file only read only, I wan to give the permission for read & write, how can I do that...
Dhrupal
If I want to append the data in same csv how to do that?
You can write your own implementation for example using String.format or MessageFormat, you can use a template engine like StringTemplate or you can use tools like Bindy.
Example using bindy:
@CsvRecord(isOrdered = true)
public Class Order {
@DataField(pos = 1, position = 11)
private int orderNr;
@DataField(pos = 2, position = 10)
private String clientNr;
...
}
I'm not sure you can use bindy without camel-core, if not then it's probably a dependency you don't want in an Android app ;)
There are loads of other options as well.