1

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 can't find any code that I can copy and paste that will work by doing a Google search.

Nor can I find any applicable description in books.

This may be easy for intermediate or advanced users, but for me it is difficult.

If you are willing to explain this for beginners, please share your time.

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"))
        }

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

<Add on Thr 2 June 2022>

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("Test1\n")
        bufferedWriter.append("Test2")
        bufferedWriter.newLine()
        bufferedWriter.append("Test3\n")
        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>
Surlofia
  • 29
  • 2
  • 8
  • `String.append()` returns a new String. Strings are immutable. – Tenfour04 Jun 01 '22 at 18:03
  • You can't just post homework assignments on here and imply that you want someone to do it for you. It's too open-ended. You need to ask a specific question about something you're stuck on. – Tenfour04 Jun 02 '22 at 12:32
  • Sorry if I sounded rude at first. What I'm trying to say is this question is too time-consuming to answer as it is currently written. I can't tell exactly what you're stuck on. You said the original problem is fixed, so I don't know what exactly you still need help with. It just says text is OK, and now you want to do CSV. But CSV *is* text. There is too much code to read through and understand just to be able to guess what you're asking about. I can't offer that much time. See here: https://stackoverflow.com/help/how-to-ask and here: https://stackoverflow.com/help/minimal-reproducible-example – Tenfour04 Jun 02 '22 at 14:27
  • Thank you for your kind words. I believe most of the people reading here are advanced users. I am sure there is a model code for advanced users of CSV files. Maybe an advanced user could publish a model for us. I hope my post "template for using txt files (which took me many days to complete)" will be useful to someone. [note] If advanced users know that I am pleased with this level of code, they may be more willing to give me more excellent code. – Surlofia Jun 02 '22 at 15:12
  • For example, here is a site that explains CSV https://stackabuse.com/reading-and-writing-csv-files-in-kotlin-with-apache-commons/ However, many people are discouraged from answering when I post links to such sites. (This is my life experience.) However, even after reading more than 30 sites, my level of knowledge does not allow me to execute it correctly. [Note] I think this is due to frequent android version upgrades. So, I thought it would be better to ask a question like this one. In the end, I feel happy to get a good impression from you. https://www.deepl.com/ – Surlofia Jun 02 '22 at 15:13

1 Answers1

3

That tutorial on using Apache Commons for reading CSV is written with the assumption that you are writing for JVM, not Android. It's not simple to adapt it to Android. I don't think the Apache Commons library is necessary anyway, because CSV is such a simple text-based format.

Here are a couple of simple extension functions that can be called on a File to either read a CSV as a 2D List of Strings, or write a 2D List of Strings to a File. Think of the 2D list as a List of lines of data. Each value in the inner List is all the values in that line.

@Throws(IOException::class)
fun File.readAsCSV(): List<List<String>> {
    val splitLines = mutableListOf<List<String>>()
    forEachLine {
        splitLines += it.split(", ")
    }
    return splitLines
}

@Throws(IOException::class)
fun File.writeAsCSV(values: List<List<String>>) {
    val csv = values.joinToString("\n") { line -> line.joinToString(", ") }
    writeText(csv)
}

You should not do File IO operations on the main thread, because this freezes the UI and risks making your application crash with the Application Not Responding Error. It looks like you have some kind of game loop so I'm not sure how to apply this advice in your case.

Here are alternate versions of the above functions that use FileInputStream and FileOutputStream so they are more versatile on Android (since you can't get direct File access for some storage locations):

@Throws(IOException::class)
fun FileInputStream.readAsCSV() : List<List<String>> {
    val splitLines = mutableListOf<List<String>>()
    reader().buffered().forEachLine {
        splitLines += it.split(", ")
    }
    return splitLines
}

@Throws(IOException::class)
fun FileOutputStream.writeAsCSV(values: List<List<String>>) {
    val csv = values.joinToString("\n") { line -> line.joinToString(", ") }
    writer().buffered().use { it.write(csv) }
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • Could you give me example how to use ? If I need to hundle bellow CSV file 0,0 1,OK 2,OK 3,Not yet 4,Not yet 5,Not yet 6,Not yet 7,Not yet 8,Not yet 9,Not yet 10,Not yet – Surlofia Jun 11 '22 at 17:30
  • Take the space out of the split call if your CSV file doesn’t have spaces after the commas. – Tenfour04 Jun 11 '22 at 17:47
  • Could you see bellow URL to check my code that finally worked. https://stackoverflow.com/questions/72590885/kotlin-how-to-use-csv-functions – Surlofia Jun 12 '22 at 09:16