Reading A Plain Text File
I know this topic probably has been covered before but I cant find an answer to the my problem. I have a file that contains some words I need to read. It works normally on my deskt
Solution 1:
You can reach a file from context in android.
Context Context;
AssetManager mngr = context.getAssets();
String line;
try {
BufferedReader br = new BufferedReader(new FileReader(mngr.open("words.txt")));
if (!br.ready()) {
thrownew IOException();
}
while ((line = br.readLine()) != null) {
words.add(line);
}
br.close();
} catch (IOException e) {
System.out.println(e);
}
Or try this:
String line;
try {
BufferedReader br = new BufferedReader(new FileReader(getApplicationContext().getAssets().open("words.txt")));
if (!br.ready()) {
thrownew IOException();
}
while ((line = br.readLine()) != null) {
words.add(line);
}
br.close();
} catch (IOException e) {
System.out.println(e);
}
Solution 2:
Assets are files on your development machine. They are not files on the device.
To get an InputStream
on an asset, use open()
on an AssetManager
. You can get an AssetManager
by calling getAssets()
on your Activity
, Service
, or other Context
.
Post a Comment for "Reading A Plain Text File"