Kotlin How to read and write CSV file

Could you give me most simple code in Kotlin ?

I tried to find good example in seraching with Google, after 5 hours or more, I could NOT find what I want.

I would like to update done marks for solved Quiz like this photo.

from chk[1] to chk[20]

not yet : 0
done : 2 
// 2 is similar Victory Sign.

for example, if solved Quiz Number “1”

chk[1] = 2

var chk_Q: Array<Int> = arrayOf (
    0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
)

Now, while app is active, keep data 0 or 2. However, once app finished, chk_Q1 to [20] back to 0.

So, I would like to save data in checkDone.csv.

My textbook not explained how to save and read CSV file.

Bellow code is not worked as I want.

openFileOutput("checkDone.csv", MODE_PRIVATE)
         .bufferedWriter().use {
             for (i in 1..10) {
                 it.write(chk_Q[i].toString())
             }
         }
openFileInput("checkDone.csv", MODE_PRIVATE)
        .bufferedReader().forEachLine {
            str.append(it)
            str.append(System.getProperty("line.separator"))
        }

================================================== =

With Text File is done

================================================== =

MainActivity.kt

================================================== =

package com.surlofia.csv_exists_write_read

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.surlofia.csv_exists_write_read.databinding.ActivityMainBinding
import java.io.*

var chk_Q: Array<Int> = arrayOf (
    0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
)

class MainActivity : AppCompatActivity() {

    // View Binding Class のインスタンス
    // lateinit で宣言し初期化タイミングを onCreate() まで遅らせる
    // activity_main.xml の場合、ActivityMainBinding
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // setContentView(R.layout.activity_main) をコメントに変えた

        // bindingのイニシャライズ
        // super.onCreate(savedInstanceState) 直下に書く!!
        // Binding Classに含まれる静的 inflate() メソッドを呼び出す
        binding = ActivityMainBinding.inflate(layoutInflater)

        // root view への参照を取得
        val view = binding.root

        // view をsetContentView()にセット
        setContentView(view)

        // bindingのイニシャライズ 完了


        // Android - ファイル入出力の例(Read、Write、内部、外部ストレージ)
        // https://codechacha.com/ja/android-read-write-file/

    // Load saved data at game startup.
    // ゲーム起動時に、セーブデータを読み込んでおく
    val filePath = filesDir.path + "/memo.dat"
    val file = File(filePath)

    if (isFileExists(file)) {
        readTextFromFile(filePath)
    }

    // Game Program Run

    // and save Data
    writeTextToFile(filePath)

        binding.fileExists.text = isFileExists(file).toString()


    }



    // Kotlinにファイルが存在するかどうかを確認します
    // https://www.techiedelight.com/ja/check-if-a-file-exists-in-kotlin/

    private fun isFileExists(file: File): Boolean {
        return file.exists() && !file.isDirectory
    }


    // Android - ファイル入出力の例(Read、Write、内部、外部ストレージ)
    // https://codechacha.com/ja/android-read-write-file/
    // &
    // TECHNICAL MASTER はじめてのAndroidアプリ開発 Kotlin編 (TECHNICAL MASTER 98)
    // https://www.amazon.co.jp/dp/B09MHF7F6N/ref=dp_kinw_strp_1

    private fun readTextFromFile(path: String) {
        val file = File(path)
        val fileReader = FileReader(file)
        val bufferedReader = BufferedReader(fileReader)

        /*
        val readString = StringBuilder()

        bufferedReader.forEachLine {
            readString.append(it)
            readString.append(System.getProperty("line.separator"))
        }

        binding.readFile.text = readString

         */

        // Let'sプログラミング
        // Home › Java入門 › テキストファイルの入出力
        // まとめてテキストを読む
        // https://www.javadrive.jp/start/stream/index3.html

        chk_Q[0] = 0

        for (i in 1 .. 20) {
            chk_Q[i] = bufferedReader.readLine().toInt()
        }
        bufferedReader.close()


        var tempString = ""

        for(i in 0 .. 20) {
            tempString = tempString +i + "-->" + chk_Q[i] + "n"
        }

        binding.readFile.text = tempString

    }

    // Android - ファイル入出力の例(Read、Write、内部、外部ストレージ)
    // https://codechacha.com/ja/android-read-write-file/

    private fun writeTextToFile(path: String) {
        val file = File(path)
        val fileWriter = FileWriter(file, false)
        val bufferedWriter = BufferedWriter(fileWriter)


        for (i in 0 .. 20) {
            chk_Q[i] = 2
            bufferedWriter.append(chk_Q[i].toString())
            bufferedWriter.newLine()
        }

        bufferedWriter.close()

        /*
        bufferedWriter.append("Test1n")
        bufferedWriter.append("Test2")
        bufferedWriter.newLine()
        bufferedWriter.append("Test3n")
        bufferedWriter.close()

         */


    }
}

===================================

activity_main.xml

===================================

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/fileExists"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Hello World!"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/readFile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fileExists" />

</androidx.constraintlayout.widget.ConstraintLayout>

Leave a Comment