How to export data to a CSV file in Android?

I created a csv file with the following format, which I am going to output to the device’s SD card:

Ship Name,Scientist Name,Scientist Email,Sample Volume,Sample Colour,Longitude,Latitude,Material,Date 

Each of the vales in csv will have a type string, with the exception of the last date value. Csv file name AnalysisData.csv

I looked at examples on Stackoverflow, such as Exporting my data to a CSV file from an android application , but this creates a new file that I don't need.

I already added opencsv jar to my project, so I just need an appropriate example.

Does anyone have any tips for achieving this on Android?

+8
android csv opencsv sd-card
source share
1 answer

Try this piece of code:

 String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath(); String fileName = "AnalysisData.csv"; String filePath = baseDir + File.separator + fileName; File f = new File(filePath ); CSVWriter writer; // File exist if(f.exists() && !f.isDirectory()){ mFileWriter = new FileWriter(filePath , true); writer = new CSVWriter(mFileWriter); } else { writer = new CSVWriter(new FileWriter(filePath)); } String[] data = {"Ship Name","Scientist Name", "...",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").formatter.format(date)}); writer.writeNext(data); writer.close(); 
+16
source share

All Articles