How do I read the file content from the Internal storage - Android App
Take a look this how to use storages in android http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
To read data from internal storage you need your app files folder and read content from here
String yourFilePath = context.getFilesDir() + "/" + "hello.txt";
File yourFile = new File( yourFilePath );
Also you can use this approach
FileInputStream fis = context.openFileInput("hello.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
Call To the following function with argument as you file path:
private String getFileContent(String targetFilePath){
File file = new File(targetFilePath);
try {
fileInputStream = new FileInputStream(file);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.e("",""+e.printStackTrace());
}
StringBuilder sb;
while(fileInputStream.available() > 0) {
if(null== sb) sb = new StringBuilder();
sb.append((char)fileInputStream.read());
}
String fileContent;
if(null!=sb){
fileContent= sb.toString();
// This is your fileContent in String.
}
try {
fileInputStream.close();
}
catch(Exception e){
// TODO Auto-generated catch block
Log.e("",""+e.printStackTrace());
}
return fileContent;
}
Read a file as a string full version (handling exceptions, using UTF-8, handling new line):
// Calling:
/*
Context context = getApplicationContext();
String filename = "log.txt";
String str = read_file(context, filename);
*/
public String read_file(Context context, String filename) {
try {
FileInputStream fis = context.openFileInput(filename);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
} catch (FileNotFoundException e) {
return "";
} catch (UnsupportedEncodingException e) {
return "";
} catch (IOException e) {
return "";
}
}
Note: you don't need to bother about file path only with file name.