IT/Android
Kotlin] 내부저장소 저장 & 가져오기
깻잎쌈
2020. 3. 5. 20:40
반응형
https://www.javatpoint.com/kotlin-android-read-and-write-internal-storage
Kotlin Android Read and Write Internal Storage - javatpoint
Kotlin Android Read and Write Internal Storage with introduction, architecture, class, object, inheritance, interface, generics, delegation, functions, mixing java and kotlin, java vs kotlin etc.
www.javatpoint.com
//저장버튼 누르면
saveButton.setOnClickListener {
val text = textField.text.toString()
when{
TextUtils.isEmpty(text) -> Toast.makeText(applicationContext, "텍스트 볏다", Toast.LENGTH_LONG).show()
else -> {
saveToInnerStorage(text, filename)
Toast.makeText(applicationContext,"${text}가 저장되엇읍니다",Toast.LENGTH_LONG).show()
textField.setText("초기화요")
}
}
}
//불러오기 버튼
loadButton.setOnClickListener {
try {
textField.setText(loadFromInnerStorage(filename))
}catch (e : FileNotFoundException){
Toast.makeText(applicationContext, "저장된 텍스트 업승ㅁ", Toast.LENGTH_LONG).show()
}
}
// 내부 저장소에 저장
fun saveToInnerStorage(text: String, filename: String ){
// 다른 앱에서는 접근불가
val fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE)
fileOutputStream.write(text.toByteArray())
fileOutputStream.close()
}
// 저장된거 가져오기
fun loadFromInnerStorage(filename: String) :String{
val fileInputStream = openFileInput(filename)
return fileInputStream.reader().readText()
}
반응형