DBILITY

독거 가능성 100% 노후에 라면값이라도 하게 센스를 발휘합시다!😅
Please click on the ad so that I can pay for ramen in my old age!
点击一下广告,让老后吃个泡面钱吧!
老後にラーメン代だけでもするように広告を一回クリックしてください。

안드로이드 internal directory file read ,write 본문

android

안드로이드 internal directory file read ,write

DBILITY 2024. 3. 26. 17:18
반응형

유튜브를 보고 따라 했다.

저장 경로가 /data/user/0/com.example.myfilehandle/files/test.txt 로 나온다.

실제로 보니 data/user/0 = data/data 다 avd device explorer에서 찾아 봤다.

firesDir의 설명은 다음과 같다. 권한 요청이 필요없다고...

android.content.Context Returns the absolute path to the directory on the filesystem where files created with openFileOutput are stored.
The returned path may change over time if the calling app is moved to an adopted storage device, so only relative paths should be persisted.
No additional permissions are required for the calling app to read or write files under the returned path.
package com.example.myfilehandle

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.myfilehandle.databinding.ActivityMainBinding
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.File
import java.io.FileReader
import java.io.FileWriter

class MainActivity : AppCompatActivity() {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val internalDir = filesDir.absolutePath
        val filename = "test.txt"
        val content = "file contents write\nI'm stupid"
        //writeTextFile(internalDir,filename,content)
        val result = readTextFile("$internalDir/$filename")
        Log.d("result", result)
    }

    private fun readTextFile(fullPath: String): String {
        var tempText:String? = null
        val file = File(fullPath)
        Log.d("file.exists()",file.exists().toString())
        Log.d("fullPath",fullPath)
        return if (!file.exists()) "" else {
            val reader = FileReader(file)
            val bufferReader = BufferedReader(reader)
            var result = StringBuffer()
            while (true) {
                tempText = bufferReader.readLine()
                if (tempText == null) break
                result.append(tempText).append("\n")
            }
            bufferReader.close()
            result.toString()
        }
    }

    private fun writeTextFile(directory: String, filename: String, content: String) {
        try {
            val dir = File(directory)
            if (!dir.exists()) {
                dir.mkdirs()
            }
            val fullPath = "$directory/$filename"
            Log.d("write fullPath",fullPath)
            val writer = FileWriter(fullPath)
            val bufferWriter = BufferedWriter(writer)
            bufferWriter.write(content)
            bufferWriter.close()
        } catch (e:Exception) {
            e.printStackTrace()
        }
    }

}
반응형
Comments