Skip to content

Instantly share code, notes, and snippets.

@ikhlaqmalik13
Created May 10, 2024 19:11
Show Gist options
  • Save ikhlaqmalik13/9d39e93c321d70a120f1754c6ec89a84 to your computer and use it in GitHub Desktop.
Save ikhlaqmalik13/9d39e93c321d70a120f1754c6ec89a84 to your computer and use it in GitHub Desktop.
Read the contents of Raw .doc file
fun readRawDocFile(context: Context, rawResourceId: Int, encoding: String? = "UTF-8"): String? {
val stringBuilder = StringBuilder()
try {
// Open the input stream using the resource ID
val inputStream = context.resources.openRawResource(rawResourceId)
val reader = BufferedReader(InputStreamReader(inputStream, Charset.forName(encoding)))
// Read the lines and append them to the StringBuilder
var line: String?
while (reader.readLine().also { line = it } != null) {
stringBuilder.append(line).append("\n")
}
// Close the reader and input stream
reader.close()
inputStream.close()
} catch (e: IOException) {
e.printStackTrace()
}
return stringBuilder.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment