3

How can create .csv file in android application, in which we will store some data like some employe details?

2 Answers 2

6

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

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...
If I want to append the data in same csv how to do that?
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.