Android Application

Kotlin 파일처리 : 앱(App) Cache Write ,Read

작성자 임베디드코리아 작성일23-10-03 23:04 조회1,784회 댓글0건

첨부파일

-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?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">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/editText"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintVertical_bias="0.2"/>
    <Button
        android:text="Cache Write"
        android:layout_width="148dp"
        android:layout_height="81dp"
        android:id="@+id/write"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintVertical_bias="0.361"
        app:layout_constraintHorizontal_bias="0.1"/>

    <Button
        android:text="Cache Read"
        android:layout_width="148dp"
        android:layout_height="81dp"
        android:id="@+id/read"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintVertical_bias="0.361"
        app:layout_constraintHorizontal_bias="0.8"/>

    <Button
        android:id="@+id/clear"
        android:layout_width="396dp"
        android:layout_height="78dp"
        android:text="EditText Clear"
        app:layout_constraintBottom_toBottomOf="@+id/DataTV"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.8"
        tools:layout_editor_absoluteX="1dp" />

    <TextView
        android:id="@+id/DataTV"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#E5FB15"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/clear"
        app:layout_constraintVertical_bias="0.15"
        tools:layout_editor_absoluteX="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val read =  findViewById<Button>(R.id.read)
        val write =  findViewById<Button>(R.id.write)
        val clear =  findViewById<Button>(R.id.clear)

        read.setOnClickListener(this)
        write.setOnClickListener(this)
        clear.setOnClickListener(this)

    }

    override fun onClick(view: View?) {
        val edit = findViewById<EditText>(R.id.editText)

        if(view?.id==R.id.read){

            loadCache()
        }
        else if(view?.id==R.id.write){
            var data:String = edit.text.toString()
          // data="ppppppp"
            val file = File(cacheDir, "myCache")
            val outputStream = FileOutputStream(file)
            outputStream.write(data.toByteArray())
            outputStream.close()
            Toast.makeText(applicationContext, "data.toByteArray() saved", Toast.LENGTH_LONG).show()
        }
        else if(view?.id==R.id.clear){
            edit.setText("")
            //clear 버튼을 누를시 입력창을 지운다.

        }

    }

    private fun saveCache(data: String) {
        try {
            val file = File(cacheDir, "myCache")
            val outputStream = FileOutputStream(file)
            outputStream.write(data.toByteArray())
            outputStream.close()
            Toast.makeText(applicationContext, "data.toByteArray() saved", Toast.LENGTH_LONG).show()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    private fun loadCache(){
        try {
            val file = File(cacheDir, "myCache")
            if (!file.exists()) file.createNewFile()
            val inputStream = FileInputStream(file)
            val s = Scanner(inputStream)
            var text:String = ""
            while (s.hasNext()) {
                text += s.nextLine()

            }

            inputStream.close()
            Toast.makeText(applicationContext, "FileInputStream", Toast.LENGTH_LONG).show()
            var dataTV: TextView = findViewById(R.id.DataTV)
            dataTV.text = text
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}