文件存储
fun writeFile(str: String) {
try {
val outputStream = openFileOutput("str", MODE_PRIVATE)
val writer =
BufferedWriter(OutputStreamWriter(outputStream))
writer.use {
it.write(str)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
fun readFile(): String {
val data = StringBuilder()
try {
val inputStream = openFileInput("str")
val reader =
BufferedReader(InputStreamReader(inputStream))
reader.use {
reader.forEachLine {
data.apply {
append(it)
append("/")
}
}
}
} catch (e: IOException) {
e.printStackTrace()
}
return data.toString()
}
SharedPreferences存储
val editor =
getSharedPreferences("sp", MODE_PRIVATE).edit()
editor.putString("data", "hello world")
editor.putInt("version", 1)
editor.putBoolean("bool", true)
editor.apply()
val sp = getSharedPreferences("sp", MODE_PRIVATE)
println(sp.getString("data",""))
println(sp.getInt("version",0))
println(sp.getBoolean("bool",false))
|