2013-11-14 36 views
6

Tôi đang phát triển một ứng dụng Android trong netbeans. Tôi đang cố gắng đọc tập tin CSV bằng cách sử dụng opencsv. Khi tôi đặt tệp trong thư mục tài nguyên và cố đọc nó từ đó, có lỗi trong khi xây dựng thư mục tài nguyên không hợp lệ. Tôi nên lưu trữ tệp csv ở đâu để có thể đọc tệp mỗi khi ứng dụng bắt đầu?Đọc tệp CSV trong thư mục tài nguyên android

Trả lời

7

bạn nên đặt tập tin csv trong thư mục tài sản ..

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

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

        } 
1

bạn có thể sử dụng mã này

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(); 
       } 

bạn có thể tải về tập tin CSVReader từ http://sourceforge.net/projects/opencsv/files/latest/download

và nhập khẩu trong dự án của bạn

7

Một số lời khuyên;

  • Tạo đối tượng để lưu dữ liệu một hàng vào csv. (Ex: YourSimpleObject. Nó cung cấp cho bạn quản lý dữ liệu dễ dàng.)
  • Đọc tệp hàng theo hàng và gán cho đối tượng. Thêm đối tượng vào danh sách. (Ví dụ: ArrayList<YourSimpleObject >)

Code:

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(); 
      } 



} 
0

Sử dụng opencsv:

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

Là một thay thế, hãy nhìn vào uniVocityParsers. Nó cung cấp một số lượng lớn các cách để phân tích các tệp được phân tách. Ví dụ dưới đây tải một tệp Csv (xem trong hình bên dưới) từ thư mục res/raw vào đối tượng InputStream và đọc nó theo cách thức đại diện (bản đồ nơi khóa = Cột & giá trị = CộtValues).

calendario_bolsa.csv

//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(); 

Để thêm univocityParsers vào dự án Android của bạn:

compile group: 'com.univocity', name: 'univocity-parsers', version: '2.3.0' 
Các vấn đề liên quan