Page 1 of 1

CreateNewFile funktioniert nicht – Datei nicht erstellt (Kotlin. Android Studio)

Posted: 23 Dec 2024, 16:58
by Anonymous
Ich verwende createNewFile(), aber die Datei wird nicht erstellt. Ich verwende den „Pixel 8 API 34“-Emulator.
Der Code wird in try{} ausgeführt, bis isNewFileCreated, und wechselt dann zu Catch(error: Exception){}
isNewFileCreated ist „false“, aber die Datei existiert nicht
*`//------------Kotlin:

private var filename: String = "readme.txt"
private lateinit var newFile: File

fun cfile() {

val isNewFileCreated: Boolean

try {
newFile = File("/data/data/com.example.myapplication2/files/",filename)
isNewFileCreated = newFile.createNewFile()
}
catch (error: FileNotFoundException) {
error.printStackTrace();
}
catch (error: Exception) {
error.printStackTrace();
}

}`*
//-------------------

Ich versuche, .use {} in try {} hinzuzufügen, aber wenn „fun cfile()“ ein zweites Mal ausgeführt wird – isExist == true, aber isNewFileCreated == false
`//-----kotlin

try {

newFile = File("/data/data/com.example.myapplication2/files/",filename)

isExist = setFile.exists()

val sFile = FileOutputStream(setFile)

sFile.use{
isNewFileCreated = setFile.createNewFile()
}
}
catch { ...`

I assume that this problem is only in the latest versions of SDK.