Click here to Skip to main content
12,635,237 members (23,442 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Mobile 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 4: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 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 28-Aug-13 11:16am
   
1st you need to create "work" folder in "C:" root dir. then try this...
Member 11838412 16-Apr-16 5:21am
   
in mobile there have no directory root like computer drive

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161208.2 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2016
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