Search code examples
androidstringkotlinarraylistandroid-assets

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


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!


Solution

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