Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- GIT
- Java
- table
- SPC
- xPlatform
- Spring
- Eclipse
- mybatis
- 보조정렬
- Android
- IntelliJ
- R
- es6
- Kotlin
- SQL
- NPM
- 공정능력
- SSL
- Sqoop
- plugin
- tomcat
- JavaScript
- Express
- window
- mapreduce
- Python
- vaadin
- hadoop
- MSSQL
- react
Archives
- Today
- Total
DBILITY
안드로이드 internal directory file read ,write 본문
반응형
유튜브를 보고 따라 했다.
저장 경로가 /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()
}
}
}
반응형
'android > kotlin' 카테고리의 다른 글
android migration kapt to ksp (0) | 2024.03.29 |
---|---|
안드로이드 SQLiteOpenHelper 구현 (0) | 2024.03.28 |
안드로이드 CalendarView selected date (0) | 2024.03.26 |
안드로이드 could not find Fragment constructor (0) | 2024.03.25 |
안드로이드 TabLayout + ViewPager2 + Fragment swipe (0) | 2024.03.21 |
Comments