Created
May 10, 2024 19:11
-
-
Save ikhlaqmalik13/9d39e93c321d70a120f1754c6ec89a84 to your computer and use it in GitHub Desktop.
Read the contents of Raw .doc file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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