Android Application

Kotlin 파일처리 : 외부 저장소 : 저장 읽기

작성자 임베디드코리아 작성일23-10-05 01:11 조회1,592회 댓글0건

첨부파일

-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/direct_saveBT"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Direct External"/>

    <EditText
        android:id="@+id/dataET"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:hint="Enter your message"/>

    <Button
        android:id="@+id/external_saveBT"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="External Save"/>

    <Button
        android:id="@+id/external_loadBT"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Read message" />

    <TextView
        android:id="@+id/tv_output"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#F2F89B"/>
</LinearLayout>

------------------------------------------------------------------------------------
    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.direct_saveBT)
        val write =  findViewById<Button>(R.id.external_saveBT)
        val clear =  findViewById<Button>(R.id.external_loadBT)

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

    }

    override fun onClick(view: View?) {

        var dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).toString() +
                File.separator + "my_folder"

        if(view?.id==R.id.direct_saveBT){
            val EXTERNAL_PATH: String = "/sdcard/Documents"
            var message = "Embedded\nKorea"

            val SDcardFile = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val formatDate = SDcardFile.format(System.currentTimeMillis())
            val filename = "ek"

            //message=formatDate
            //val file = File("$EXTERNAL_PATH/$formatDate.txt")
            val DirFileName = File("$EXTERNAL_PATH/$filename.txt")
            if (!DirFileName.exists()) { DirFileName.createNewFile() }

            val writer = FileWriter(DirFileName.absolutePath)
            val buffer = BufferedWriter(writer)
            buffer.write(message)
            buffer.close()

            Toast.makeText(applicationContext, "direct_saveBT:$formatDate", Toast.LENGTH_LONG).show()

        }

        if(view?.id==R.id.external_saveBT){
            val dataET = findViewById<EditText>(R.id.dataET)

            val file = File(dir)
            if (!file.exists()) {
                file.mkdirs()
            }

            var output = FileOutputStream(dir+ "/sdkfile.txt")
            var dos = DataOutputStream(output)

            var readMessage:String = dataET.text.toString()

            dos.writeUTF(readMessage)
            dos.flush()
            dos.close()

            Toast.makeText(applicationContext, "external_saveBT", Toast.LENGTH_LONG).show()
        }

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

            val dataTV = findViewById<TextView>(R.id.tv_output)

            var input = FileInputStream(dir + "/sdkfile.txt")
            var dis = DataInputStream(input)

            var valueUTF = dis.readUTF()
            dis.close()

            dataTV.text = valueUTF

            Toast.makeText(applicationContext, "Read message", Toast.LENGTH_LONG).show()
        }

    }
}

------------------------------------------------------------------------------------
    AndroidManifest.xml
------------------------------------------------------------------------------------
<!-- 외부 저장소 읽기 권한 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <!-- 외부 저장소 쓰기 권한 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>