Reputation: 1555
I created a file on my pc, and I want my app to read from it.
How do I read from that file in my app?
Thanks
Upvotes: 0
Views: 49
Reputation: 237
Move your file to card and add path instead of "file.txt"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text);
it example best if you want to read text
Upvotes: 0
Reputation: 1007584
Put the file in assets/
(e.g., app/src/main/assets/
in a typical Android Studio project). Then use open()
on an AssetManager
to get an InputStream
on that content. You can get an AssetManager
from your Activity
or other Context
by calling getAssets()
.
Upvotes: 3