2012年7月3日 星期二

Android read and parse .csv

在Android上簡易的const data建立與讀取方式
一、使用google文件的試算表建立資料表格
二、將檔案下載為csv檔
三、將檔案放到Android方案中的assets目錄
四、使用Android的InputStream讀入csv檔
五、使用BufferedReader來parse

       try 
        {
   InputStream is = getResources().getAssets().open("score.csv");
   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      try 
      {
          String line;
          while ((line = reader.readLine()) != null) 
          {
               String[] RowData = line.split(",");
               Log.e("TAG", RowData[0]);
               Log.e("TAG", RowData[1]);
               Log.e("TAG", RowData[2]);
              // do something with "data" and "value"
          }
      }
      catch (IOException ex) 
      {
          // handle exception
      }
      finally 
      {
          try 
          {
              is.close();
          }
          catch (IOException e) 
          {
              // handle exception
          }
      }
  } 
        catch (IOException e) 
        {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }  

沒有留言:

張貼留言