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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 447
1 CPallini 215
2 thatraja 210
3 OriginalGriff 195
4 Abhinav S 181
0 Sergey Alexandrovich Kryukov 8,809
1 OriginalGriff 5,025
2 Peter Leow 3,839
3 Maciej Los 3,535
4 Abhinav S 3,118


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