2013-11-14 26 views
6

Netbeans'te bir android uygulaması geliştiriyorum. Opencsv kullanarak CSV dosyasını okumaya çalışıyorum. Dosyayı kaynak klasörüne yerleştirdiğimde ve oradan okumaya çalıştığımda, geçersiz kaynak dizini belirlerken bir hata oluştu. Uygulamanın her başlatılışında okunabilmesi için csv dosyasını nerede saklamalıyım? Eğer varlıklar klasöründe csv dosyasını koymalıyızKaynaklar klasöründeki CSV dosyasını okuma android

cevap

7

..

InputStreamReader is = new InputStreamReader(getAssets() 
         .open("filename.csv")); 

       BufferedReader reader = new BufferedReader(is); 
       reader.readLine(); 
       String line; 
       while ((line = reader.readLine()) != null) { 

        } 
1

Eğer projenizde http://sourceforge.net/projects/opencsv/files/latest/download

ve ithalattan CSVReader dosyasını indirebilirsiniz

try { 
       InputStream csvStream = assetManager.open(CSV_PATH); 
       InputStreamReader csvStreamReader = new  InputStreamReader(csvStream); 
       CSVReader csvReader = new CSVReader(csvStreamReader); 
       String[] line; 

       // throw away the header 
       csvReader.readNext(); 

       while ((line = csvReader.readNext()) != null) { 
        questionList.add(line); 
       } 
       } catch (IOException e) { 
       e.printStackTrace(); 
       } 

bu kodu kullanabilirsiniz

7

Bazı öneriler;

  • Bir satır verisini csv'ye tutmak için bir nesne oluşturun. (Ex: YourSimpleObject. Verileri kolayca yönetmenizi sağlar.)
  • Dosya satırını satırlara göre okuyun ve nesneye atayın. Nesneyi listeye ekle. (Örn: ArrayList<YourSimpleObject >)

Kodu: uniVocityParsers bakmak, bir alternatif olarak

InputStream is = context.getAssets().open(path); 
InputStreamReader reader = new InputStreamReader(is, Charset.forName("UTF-8")); 
List<String[]> csv = new CSVReader(reader).readAll(); 
0

opencsv kullanma. Sınırlandırılmış dosyaları ayrıştırmak için çok sayıda yol sunar. Örnek aşağıda bir Csv Dosyası (aşağıdaki resme bakınız) bir Res/raw klasöründen bir InputStream nesnesine yüklenir ve bunu colunar bir şekilde (anahtarın = Column & value = ColumnValues ​​olduğu bir harita) okuyun.

//Gets your csv file from res/raw dir and load into a InputStream. 
InputStream csvInputStream = getResources().openRawResource(R.raw.calendario_bolsa); 

//Instantiate a new ColumnProcessor 
ColumnProcessor columnProcessor = new ColumnProcessor(); 

//Define a class that hold the file configuration 
CsvParserSettings parserSettings = new CsvParserSettings(); 
parserSettings.getFormat().setLineSeparator("\n"); 
parserSettings.setHeaderExtractionEnabled(true); 
parserSettings.setProcessor(columnProcessor); 

//Creates a new CsvParser, passing the settings into its construtor: 
CsvParser csvParser = new CsvParser(parserSettings); 

//Calls parse method, instantiating an InputStreamReader, passing to its constructor the InputStream object 
csvParser.parse(new InputStreamReader(csvInputStream)); 

//Gets the csv data as a Map of Column/column values. 
Map<String, List<String>> columnarCsv = columnProcessor.getColumnValuesAsMapOfNames(); 

calendario_bolsa.csv

Android Projesi içine univocityParsers eklemek için:

compile group: 'com.univocity', name: 'univocity-parsers', version: '2.3.0' 
1

:

private void readAndInsert() throws UnsupportedEncodingException { 


ArrayList<YourSimpleObject > objList= new ArrayList<YourSimpleObject >(); 
AssetManager assetManager = getAssets(); 
InputStream is = null; 

      try { 
       is = assetManager.open("questions/question_bank.csv"); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      BufferedReader reader = null; 
      reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); 

      String line = ""; 
      StringTokenizer st = null; 
      try { 

       while ((line = reader.readLine()) != null) { 
        st = new StringTokenizer(line, ","); 
        YourSimpleObject obj= new YourSimpleObject(); 
            //your attributes 
        obj.setX(st.nextToken()); 
        obj.setY(st.nextToken()); 
        obj.setZ(st.nextToken()); 
        obj.setW(st.nextToken()); 

        objList.add(sQuestion); 

       } 
      } catch (IOException e) { 

       e.printStackTrace(); 
      } 



}