我有下面的代码来删除使用 FileOutputStream 创建的文件,但它似乎只是删除文件的内容,而不是文件本身。
我尝试按照以下方式编写我的代码:删除使用 FileOutputStream 创建的文件
我需要做什么才能删除文件而不仅仅是其内容?
val file = File("myFileName.txt")
try {
val fileOutputStream: FileOutputStream =
context.openFileOutput(
"myFileName.txt",
Context.MODE_PRIVATE
)
fileOutputStream.close()
file.delete()
} catch (e: IOException) {
Log.d(
"Error deleting file",
"${e.localizedMessage}"
)
}
您可以尝试创建文件、写入文件(或执行其他操作)以及删除文件:
val file = File("C:\\foo.txt")
// Use FileOutputStream to write the content to the file
FileOutputStream(file).use { fos ->
val content = "Hello World!".toByteArray()
fos.write(content)
}
// Verify that the content is written to file
println(file.readText())
// Delete the file along with its contents
file.delete()
println(file.exists()) // false