KV
12/01/2019, 2:27 PMfileContent = readFileFromRawDirectory(R.raw.demoFile) . ---> This is the file inside the /raw folder
private fun readFileFromRawDirectory(resourceId: Int): String {
val iStream: InputStream = context?.resources.openRawResource(resourceId)
var byteStream: ByteArrayOutputStream? = null
try {
val buffer = ByteArray(iStream.available())
iStream.read(buffer)
byteStream = ByteArrayOutputStream()
byteStream.write(buffer)
byteStream.close()
iStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
return byteStream.toString()
}
Now I want to add the data inside the same demoFile.txt
This below code is for writing the string inside the demoFile.txt
private fun writeDataIntoFile(searchText: String) {
val file = context?.filesDir?.absolutePath + "/demoFile.txt"
try {
val fileout: FileOutputStream? = context?.openFileOutput(file, MODE_PRIVATE)
val outputWriter = OutputStreamWriter(fileout)
outputWriter.write(searchText)
outputWriter.close()
fileout?.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
rkeazor
12/01/2019, 2:59 PMKV
12/01/2019, 3:05 PMrkeazor
12/01/2019, 3:21 PMKV
12/01/2019, 3:21 PMrkeazor
12/01/2019, 3:21 PM