How to read .txt file from drawable or any other folder in android using FileInputStream?
I have a .txt file in my drawable folder. How can i read the file and set it in Textview?
How to read .txt file from drawable or any other folder in android using FileInputStream?
I have a .txt file in my drawable folder. How can i read the file and set it in Textview?
Try This with help of this u can read file from drawable folder
String data = "";
StringBuffer sbuffer = new StringBuffer();
InputStream is = this.getResources().openRawResource(+
R.drawable.filetoread);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is != null) {
try {
while ((data = reader.readLine()) != null) {
sbuffer.append(data + "\n");
}
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
data = sbuffer.toString(),
}
Why would you want to keep your .txt in the drawable/ folder? It's better that you keep it in the assets folder instead and read it as described in this SO.
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt")));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
//process line
...
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}