您需要使用內部或外部存儲目錄作爲您的文件。
內部:
val path = context.getFilesDir()
外部:
val path = context.getExternalFilesDir(null)
如果你想使用外部存儲你需要一個許可添加到清單:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
創建目錄:
val letDirectory = File(path, "LET")
letDirectory.mkdirs()
然後創建文件:
val file = File(letDirectory, "Records.txt")
然後,你可以寫它:
FileOutputStream(file).use {
it.write("record goes here".getBytes())
}
或只是
file.appendText("record goes here")
,念道:
val inputAsString = FileInputStream(file).bufferedReader().use { it.readText() }
我認爲你必須使用.mkdirs()的調用中在「/ LET」可能還不存在的情況下存在某處。另外,爲了寫作,他可以使用文件。appendText(最終解決類似於你寫的東西,除了appendText設置字符集爲UTF8) – Les
未解決的引用與context.getFilesDir() – Khayyam
你使用'android.content.Context'嗎? – TpoM6oH