diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 609e114..ec87427 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,11 +20,6 @@ - - - - - - - - - + + + + +// bArr[i] = ch.code.toByte() +// } + + return alphabets.toByteArray() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/androidconcepts/fileStorage/learning1/FileLearning1.kt b/app/src/main/java/com/example/androidconcepts/fileStorage/learning1/FileLearning1.kt new file mode 100644 index 0000000..4bb8d8b --- /dev/null +++ b/app/src/main/java/com/example/androidconcepts/fileStorage/learning1/FileLearning1.kt @@ -0,0 +1,108 @@ +package com.example.androidconcepts.fileStorage.learning1 + +import android.content.Context +import android.content.Context.MODE_PRIVATE +import android.util.Log +import androidx.core.text.isDigitsOnly +import java.io.File + +class FileLearning1 constructor(private val context: Context) { + + private val myDigitsFileName = "myCreatedFile" + + fun start() { + logBasicDetails() + + Log.d("FILE","-------------------------") + + incrementAppLaunchCounterInFile() + } + + private fun incrementAppLaunchCounterInFile() { + checkIfFileExistsElseCreate() + + Log.d("FILE","-------------------------") + + var readData = readAndGetDataFromFile(myDigitsFileName) + + if (!readData.isDigitsOnly()) + readData = "0" + + val newCount = readData.toInt() + 1 + + writeNewDataToFile(newCount) + + Log.d("FILE", "data : $readData") + } + + private fun checkIfFileExistsElseCreate() { + // though in input/output stream we directly pass the name to access from filesDir, + // here we need to explicitly mention the path. + val file = File(context.filesDir.path + File.separator + myDigitsFileName) + + if (!file.exists()) { + Log.d("FILE", "created new file path : " + file.path) + } else { + Log.d("FILE", "already exists : " + file.path) + } + } + + private fun readAndGetDataFromFile(filesDirName: String): String { + var readData = "" + + // any file accessed like this (by passing direct name) will be accessed from files directory of the app. + context.openFileInput(filesDirName) + .use { + while (true) { + val data = it.read() + if (data == -1) break + readData += data.toChar().toString() + } + } + + return readData + } + + private fun writeNewDataToFile(newCount: Int) { + // any file accessed like this (by passing direct name) will be accessed from files directory of the app. + context.openFileOutput(myDigitsFileName, MODE_PRIVATE).use { + it.write(newCount.toString().toByteArray()) + } + } + + private fun logBasicDetails() { + + context.fileList().forEach { + val file = File(context.filesDir.path + File.separator + it) + Log.d("FILE", "file details : " + file.path) + } + + Log.d("FILE","-------------------------") + + printDetails( + prefix = "cacheDir", + file = context.cacheDir + ) // cacheDir path /data/user/0/com.example.androidconcepts/cache name : cache isDir : true + + printDetails( + prefix = "filesDir", + file = context.filesDir + ) // filesDir path /data/user/0/com.example.androidconcepts/files name : files isDir : true + + if (context.externalCacheDir != null) + printDetails( + prefix = "externalDir", + file = context.externalCacheDir!! + ) // externalDir path /storage/emulated/0/Android/data/com.example.androidconcepts/cache name : cache isDir : true + } + + private fun printDetails(prefix: String, file: File): String { + val string = + "$prefix path " + file.path + " name : " + file.name + " isDir : " + file.isDirectory + + Log.d("FILE", string) + + return string + } + +} \ No newline at end of file diff --git a/app/src/test/java/com/example/androidconcepts/dataTypes/bytes/ByteLearning1Test.kt b/app/src/test/java/com/example/androidconcepts/dataTypes/bytes/ByteLearning1Test.kt new file mode 100644 index 0000000..3f5ded4 --- /dev/null +++ b/app/src/test/java/com/example/androidconcepts/dataTypes/bytes/ByteLearning1Test.kt @@ -0,0 +1,55 @@ +package com.example.androidconcepts.dataTypes.bytes + +import org.junit.Before +import org.junit.Test + +class ByteLearning1Test { + + private lateinit var SUT: ByteLearning1 + + @Test + fun simpleTest() { + + val result = SUT.execute() + + result.forEachIndexed { i, ch -> + println("BYTE DEBUG : " + ('a' + i) + " " + ch) + } + + /** + * + BYTE DEBUG : a 97 + BYTE DEBUG : b 98 + BYTE DEBUG : c 99 + BYTE DEBUG : d 100 + BYTE DEBUG : e 101 + BYTE DEBUG : f 102 + BYTE DEBUG : g 103 + BYTE DEBUG : h 104 + BYTE DEBUG : i 105 + BYTE DEBUG : j 106 + BYTE DEBUG : k 107 + BYTE DEBUG : l 108 + BYTE DEBUG : m 109 + BYTE DEBUG : n 110 + BYTE DEBUG : o 111 + BYTE DEBUG : p 112 + BYTE DEBUG : q 113 + BYTE DEBUG : r 114 + BYTE DEBUG : s 115 + BYTE DEBUG : t 116 + BYTE DEBUG : u 117 + BYTE DEBUG : v 118 + BYTE DEBUG : w 119 + BYTE DEBUG : x 120 + BYTE DEBUG : y 121 + BYTE DEBUG : z 122 + */ + } + + @Before + fun setup() { + SUT = ByteLearning1() + } + +} \ No newline at end of file