2020-11-01

【Kotlin】讀取 CSV 檔案

參考資料 ----

kotlin-csv


Kotlin 已有免費套件,可用來讀取 CSV


app 層級的 build.gradle

  1.  
  2. dependencies {
  3.  
  4. ...
  5. ...
  6. implementation("com.github.doyaaaaaken:kotlin-csv-jvm:0.7.3")
  7. }
  8.  



在專案的 /src/main/ 建立 assets 資料夾,並將在 【C#】以程式列出 中文字 與 BIG5 內碼 的對應表 這篇筆記所生成的 CSV 檔置入 assets 下 


 



activity_main.xml 放 2 個 TextView,一個用來顯示中文,一個用來顯示 BIG5 內碼
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity">
  9.  
  10. <TextView
  11. android:id="@+id/lblBig5"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="Hello World!"
  15. app:layout_constraintBottom_toBottomOf="parent"
  16. app:layout_constraintHorizontal_bias="0.498"
  17. app:layout_constraintLeft_toLeftOf="parent"
  18. app:layout_constraintRight_toRightOf="parent"
  19. app:layout_constraintTop_toTopOf="parent"
  20. app:layout_constraintVertical_bias="0.36" />
  21.  
  22. <TextView
  23. android:id="@+id/lblCode"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="Hello World!"
  27. app:layout_constraintBottom_toBottomOf="parent"
  28. app:layout_constraintLeft_toLeftOf="parent"
  29. app:layout_constraintRight_toRightOf="parent"
  30. app:layout_constraintTop_toTopOf="parent" />
  31.  
  32. </androidx.constraintlayout.widget.ConstraintLayout>
  33.  



MainActivity.kt
  1.  
  2. class MainActivity : AppCompatActivity()
  3. {
  4. private val TAG = "MainActivity"
  5.  
  6. override fun onCreate(savedInstanceState: Bundle?)
  7. {
  8. super.onCreate(savedInstanceState)
  9. setContentView(R.layout.activity_main)
  10.  
  11. ReadCSVTask().execute(null, null, null)
  12. }
  13.  
  14.  
  15. private inner class ReadCSVTask : AsyncTask<Any, Any, Any>()
  16. {
  17. // 因為中文字 - 內碼是一對一的關係,所以我決定將讀進來的 CSV 存入 Map
  18. val mMap = emptyMap<String, String>().toMutableMap()
  19.  
  20. override fun onPreExecute()
  21. {
  22. Log.d(TAG, "ReadCSVTask onPreExecute...")
  23. }
  24.  
  25. override fun doInBackground(vararg p0: Any?): Any
  26. {
  27. Log.d(TAG, "ReadCSVTask doInBackground...")
  28. // 將 CSV 讀入字串中
  29. val fileText = applicationContext.assets.open("big5.csv").bufferedReader().use {
  30. it.readText()
  31. }
  32.  
  33.  
  34. val tsvReader = csvReader { charset = "UTF-8" // 文字格式
  35. quoteChar = '"' // 包覆元素的符號
  36. delimiter = ',' // 分隔字元
  37. }
  38. rows = tsvReader.readAll(fileText)
  39. for(mrow in rows)
  40. {
  41. // mrow[1] 是中文字
  42. // mrow[0] 是 BIG5 內碼
  43. mMap[mrow[1]] = mrow[0]
  44. }
  45. return ""
  46. }
  47.  
  48. override fun onPostExecute(args: Any)
  49. {
  50. Log.d(TAG, "ReadCSVTask onPostExecute...")
  51.  
  52. lblBig5.text = "麟"
  53. lblCode.text = mMap["麟"]
  54. }
  55. }
  56. }
  57.  




沒有留言:

張貼留言