Android : java.lang.IllegalArgumentException File contains a path separator


Error Stack:
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 openFileInput("/Dir/data.txt") method with 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 occured while reading text file!!");

 }









Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap