Dev/Android

Android (파일 읽기, 쓰기, raw 폴더 파일 처리)

Walker_ 2024. 5. 1. 16:15

1. 파일 읽기, 쓰기

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnWrite"
        android:text="내장 메모리에 파일 쓰기"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnRead"
        android:text="내장 메모리에 파일 읽기"/>

</LinearLayout>
package kr.jeongmo.a0501project_2

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    lateinit var btnWrite:Button
    lateinit var btnRead:Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val btnWrite = findViewById<Button>(R.id.btnWrite)
        val btnRead = findViewById<Button>(R.id.btnRead)

        btnWrite.setOnClickListener {
            val outputStream = openFileOutput("file.txt", Context.MODE_PRIVATE)
            val string = "파일 처리 테스트"
            outputStream.write(string.toByteArray())
            outputStream.close()
            Toast.makeText(applicationContext, "file.txt가 생성됨.", Toast.LENGTH_SHORT).show()
        }

        btnRead.setOnClickListener {
            try {
                val inputStream = openFileInput("file.txt")
                val txt = ByteArray(30)
                inputStream.read(txt)
                val string = txt.toString(Charsets.UTF_8)
            } catch (e : Exception){
                
            }
        }
    }
}

 

2. raw 폴더 파일 처리

 - 프로제트 /res/raw 폴더에 필요한 파일을 저장하려 사용하는 방법

 

 - res > new > Android Resource Directory

 

 - raw로 생성

 

 - text1.txt 파일 생성 (UTF-8) 

 

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnRead"
        android:text="/res/raw에서 파일 찾기"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editRaw"
        android:lines="10"/>

</LinearLayout>

 

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.layout02)

    val btnRead = findViewById<Button>(R.id.btnRead)
    val editRaw = findViewById<EditText>(R.id.editRaw)

    btnRead.setOnClickListener {
        val inputStream = resources.openRawResource(R.raw.text1)
        val txt = ByteArray(inputStream.available())
        inputStream.read(txt)
        editRaw.setText(txt.toString(Charsets.UTF_8))
        inputStream.close()
    }

 

 

 


공부 과정을 정리한 것이라 내용이 부족할 수 있습니다.

부족한 내용은 추가 자료들로 보충해주시면 좋을 것 같습니다.

읽어주셔서 감사합니다 :)