04-04 23:22:21.869: E/AndroidRuntime(7417): FATAL EXCEPTION: main
04-04 23:22:21.869: E/AndroidRuntime(7417): java.lang.IllegalStateException: Could not execute method of the activity
04-04 23:22:21.869: E/AndroidRuntime(7417): Caused by: java.lang.reflect.InvocationTargetException
04-04 23:22:21.869: E/AndroidRuntime(7417): Caused by: java.lang.IllegalArgumentException: File /Dir/data.txt contains a path separator
04-04 23:22:21.869: E/AndroidRuntime(7417): at android.app.ContextImpl.makeFilename(ContextImpl.java:1674)
04-04 23:22:21.869: E/AndroidRuntime(7417): at android.app.ContextImpl.openFileInput(ContextImpl.java:412)
04-04 23:22:21.869: E/AndroidRuntime(7417): at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
04-04 23:22:21.869: E/AndroidRuntime(7417): at com.code2care.readtextfile.MainActivity.getTextFileData(MainActivity.java:44)
04-04 23:22:21.869: E/AndroidRuntime(7417): at com.code2care.readtextfile.MainActivity.getTextFile(MainActivity.java:29)
04-04 23:22:21.869: E/AndroidRuntime(7417): ... 14 more
I got the above error message while trying to access the file from Internal Storage using the openFileInput("/Dir/data.txt") method with the subdirectory Dir. The reason is that you cannot access subdirectories using this method, just try something like,
FileInputStream fIS = new FileInputStream (new File("/Dir/data.txt"));
Code Snippet:try {
FileInputStream fIS = new FileInputStream (new File(fileName));
InputStreamReader isr = new InputStreamReader(fIS, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
text.append(line + '\n');
}
br.close();
fIS.close();
} catch (IOException e) {
Log.e("C2c", "Error occurred while reading text file!!");
}
This is not an AI-generated article but is demonstrated by a human.
Please support independent contributors like Code2care by donating a coffee.
Buy me a coffee!

Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!