如何在文件夹中创建文件并向其中写入数据

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

如何创建文件夹(例如在我的phone中的文件夹“Documents”),在这个文件夹中创建文件并在这个文件中写入数据?我有一个代码。但我不确定我的文件路径是否正确。我不想在 SD 卡上创建文件夹和文件/我想在设备内存中创建。

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();
                }

重复。本题回答了如何在内部存储器上创建文件:. 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?.