-1

I tried this Kotlin code to save a text file in external storage:

    try {
        var filePath = Environment.getExternalStorageState() + File.separator + "Text.txt"
        var file = File(filePath)
        file.createNewFile()

        var fileWriter = FileWriter(file.path)
        fileWriter.write("My Name Is Amirhosein")
        fileWriter.close()

    }
    catch (ex: Exception) {
        Toast.makeText(this,ex.message,Toast.LENGTH_LONG).show()
    }

and this exception came in Toast:

No such file or directory

anyone knows how to fix it?

blackgreen
  • 18,419
  • 19
  • 55
  • 71

1 Answers1

0

Try using this!!

val file = File(getExternalFilesDir(null), "Text.txt")
file.mkdirs()