I have some several txt file already.I want to read this files in my application.i click button and choose name of txt file and read it. How i do it? pls help me.
Asked
Active
Viewed 401 times
-3
-
This question may be duplicate of http://stackoverflow.com/questions/12421814/how-to-read-text-file-in-android – snehal Jul 24 '13 at 07:57
2 Answers
0
You can put the file in the asset directory of the projet and use
AssetManager am = context.getAssets();
I think this link can help you : http://www.technotalkative.com/android-read-file-from-assets/

David N
- 509
- 4
- 12
0
You can use following code listing to read contents of a text file.
To get the path:
File file = app.getFilesDir();
String path = file.getAbsoluteFile().getAbsolutePath() + "<filename.extension>";
--------------------------------------------------------
public static String readAllContents(String path) {
String fileContents = null;
try {
InputStream inputStream = new FileInputStream(path);
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
fileContents = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("exception", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("exception", "Can not read file: " + e.toString());
}
return fileContents;
}

bhavik shah
- 573
- 5
- 12