Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Android
I will select a csv file and then import the data to sqlite.i can also export the data from sqlite to csv file.if you simply show me to read/write CSV in android that will be good enough for me..Thanks in advanced
Posted 9-Nov-12 3:55am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Java doesn’t support parsing of CSV files natively, we have to rely on third party library. There was 3rd party library Opencsv is very good library to read and write CSV file in android platform.
At first you need to download this library Download. Then try...
 
Reading CSV file in Java:
String csvFilename = "C:\\sample.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
String[] row = null;
while((row = csvReader.readNext()) != null) {
    System.out.println(row[0]
              + " # " + row[1]
              + " #  " + row[2]);
}
//...
csvReader.close();
Above code read csv file line by line. If you want to read full document once using below code:
String[] row = null;
String csvFilename = "C:\\work\\sample.csv";
 
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
List content = csvReader.readAll();
 
for (Object object : content) {
    row = (String[]) object;
     
    System.out.println(row[0]
               + " # " + row[1]
               + " #  " + row[2]);
}
//...
csvReader.close();
 
Writing CSV file in Java:
String csv = "C:\\work\\output.csv";
CSVWriter writer = new CSVWriter(new FileWriter(csv));
 
String [] country = "India#China#United States".split("#");
 
writer.writeNext(country);
 
writer.close();
Above code use to write a string array in csv, but you can write a full document or list of string array using below code:
String csv = "C:\\work\\output.csv";
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();
 
Try this i think it work fine for you...
  Permalink  
Comments
anel91 at 28-Aug-13 8:35am
   
i have a question , i can't find the file output.csv in "C:\\work\\output.csv" ? where can i find it
csharpbd at 28-Aug-13 11:16am
   
1st you need to create "work" folder in "C:" root dir. then try this...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Nirav Prabtani 384
1 OriginalGriff 289
2 Sergey Alexandrovich Kryukov 225
3 _Amy 195
4 Kumarbs 144
0 OriginalGriff 7,799
1 Sergey Alexandrovich Kryukov 6,966
2 Maciej Los 3,994
3 Peter Leow 3,708
4 CHill60 2,742


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100