본문 바로가기
안드로이드 코틀린

[Kotlin][Android] 파일 쓰고 불러오기

by teamnova 2022. 7. 13.

이번시간에는 텍스트 파일을 만들고 불러오겠습니다.

 

먼저 activity_main 레이아웃입니다.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/textbox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="텍스트를 입력해주세요."
        android:minLines="3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_save"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="저장하기"
        app:layout_constraintEnd_toStartOf="@+id/btn_load"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textbox" />

    <Button
        android:id="@+id/btn_load"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="불러오기"
        app:layout_constraintBottom_toBottomOf="@+id/btn_save"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btn_save" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

다음으로는 MainActivity 입니다.

package com.rai.LineProject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout);

        val textBox = findViewById<EditText>(R.id.textbox)
        val btnSave = findViewById<Button>(R.id.btn_save)
        val btnLoad = findViewById<Button>(R.id.btn_load)

        val fileName = "file.txt"

        btnSave.setOnClickListener {
            val os = openFileOutput(fileName, MODE_PRIVATE)
            os.write(textBox.text.toString().toByteArray())
            os.close()

            Toast.makeText(this, "파일을 저장했습니다.", Toast.LENGTH_SHORT).show()
        }
        btnLoad.setOnClickListener {
            try {

                val stream = openFileInput(fileName)
                if (stream != null) {
                    val iStreamReader = InputStreamReader(stream)
                    val reader = BufferedReader(iStreamReader)
                    val sb = StringBuilder()
                    var line = reader.readLine()
                    while (line != null) {
                        sb.append(line)
                        line = reader.readLine()
                    }
                    textBox.setText(sb.toString())

                    stream.close()
                }
            } catch (e : IOException) {
                Toast.makeText(this, "파일이 없습니다.", Toast.LENGTH_SHORT).show()

            }
        }

    }

저장하기 버튼을 누를 경우 textBox에 작성한 내용을 저장할 수 있고,

불러오기를 할 경우, 저장한 내용을 textBox에 불러올 수 있습니다.