Android
안드로이드 내장메모리에 있는 텍스트파일 읽어오기
일용직 코딩노동자
2022. 12. 30. 10:37
728x90
반응형
fun readTextFile(path: String?): String? {
var line: String? = null // 한줄씩 읽기
var msg = ""
val saveFile = File(path) // 저장 경로
try {
val buf = BufferedReader(FileReader(saveFile))
while (true) {
val line = buf.readLine()
if(line == null) break
msg += line + "\n"
}
buf.close()
return msg
}
catch (e: java.lang.Exception) {
e.printStackTrace()
return "NOT FILE"
}
}
인자값으로 경로를 넣어주시면 리턴값으로 텍스트 파일의 텍스트를 읽어옵니다.
파일이 없으면 NOT FILE이 리턴됩니다.
val txt = readTextFile("/storage/emulated/0/Pictures/test.txt")
사용법의 예시입니다.
728x90
반응형