How to convert .txt asset to ArrayList<String> format in kotlin?

108 Views Asked by At

So I have thousands of coordinates that i need to plot and one of the best method i know is by saving the coordinates as .txt in asset folder and then pull it by using

val json_string = application.assets.open(file_name).bufferedReader().use{it.readText()}

But when the file is pulled, it read the file as a string, despite the file is saved in array format such as follow:

"97.951476367000055,3.155017079000061","97.951800004000063,3.154406602000051","97.949325673000033,3.156145963000029","97.948721732000081,3.15657051200003","97.948646156000052,3.156623638000042","97.947494978000066,3.15786896100002","97.94724135000007,3.158143331000076","97.947000011000057,3.158404405000056","97.946790649000036,3.158630887000072","97.946272953000062,3.15919092200005","97.945702608000033,3.15980791000004","97.945553326000038,3.159969400000023","97.944601930000033,3.160998592000055","97.944409762000078,3.161206474000039"....

I want to read the asset in an array format, for example, it is divided by comma and grouped by quotes, for example:

  • "97.951476367000055,3.155017079000061"
  • "97.951800004000063,3.154406602000051"
  • "97.949325673000033,3.156145963000029"
  • and so on...

How can I read this .txt file asset as string array or ArrayList< String > format in Kotlin?

Thanks!

2

There are 2 best solutions below

1
Amardurai On BEST ANSWER

Create a model like below

data class Coordinate(val latitude: String, val longitude: String)

// Function to read coordinates from the asset file and convert to ArrayList

fun readCoordinatesFromAsset(context: Context, assetFileName: String): 
   ArrayList<String> {
   val gson = Gson()
   val arrayList = ArrayList<String>()

   try {
        context.assets.open(assetFileName).use { inputStream ->
        val reader = InputStreamReader(inputStream)
        val coordinates = gson.fromJson(reader, ArrayList::class.java)

        // Convert the ArrayList of Coordinate objects to an ArrayList of strings
        for (coordinate in coordinates) {
            arrayList.add(coordinate.toString())
        }
    }
  } catch (e: Exception) {
    e.printStackTrace()
  }

return arrayList
}

Your text file should be like the below format

coordinate.txt

[
        "97.951476367000055,3.155017079000061",
        "97.951800004000063,3.154406602000051",
        "97.949325673000033,3.156145963000029",
        "97.948721732000081,3.15657051200003",
        "97.948646156000052,3.156623638000042",
        "97.947494978000066,3.15786896100002",
        "97.94724135000007,3.158143331000076",
        "97.947000011000057,3.158404405000056",
        "97.946790649000036,3.158630887000072",
        "97.946272953000062,3.15919092200005",
        "97.945702608000033,3.15980791000004",
        "97.945553326000038,3.159969400000023",
        "97.944601930000033,3.160998592000055",
        "97.944409762000078,3.161206474000039"
    ]

You can call the method as

val resulArray: ArrayList<String> =readCoordinatesFromAsset(this,"coordinate.txt")
0
Meet Miyani On

You can try something like this, see the code below.

https://pl.kotl.in/NQQdTW6m4?theme=darcula

Code for the problem

Click on the link above, to test and run the code, also you can define your own model class, instead of the Pair() which I have use to demonstrate in this case. Feel free to improve it!