Search code examples
androidfileoutputstreamandroid-file

How to create file in the folder and write data to it


How to create folder (for example in the folder "Documents" in my phone), in this folder create the file and write data in this file? I have a code. But I am not shure that I do my file path is right. I want create folder and file not at SD card / I want do it in the device memory.

var filePath = File(Environment.DIRECTORY_DOCUMENTS + "myFolder/myFile.txt")
                filePath.mkdirs()
          
                try {
                    var outPut = FileOutputStream(filePath)
                    outPut.write("Hello")
                    outPut.close()
                } catch (e: Exception) {
                    e.printStackTrace();
                }

Solution

  • Duplicate. This question answers how to create a file on the internal memory: Android: how to write a file to internal storage. And this question answers how to create a folder on the internal memory: How to create a folder in the phone memory (not SD card) in Android?.