Search code examples
javaandroidinputstreambufferedreader

How to read a file that i created inside my the same package?


This is a chunk of data I'd like to access by a method. I'm doing the following to read my file:

String fileName = "file.txt" 
InputStream inputStream = new FileInputStream(fileName);
BufferedReader bufferedReader = new BufferedReader(new       InputStreamReader(inputStream));

My file.txt is in the same package, but I still get FileNotFoundException. I didn't use a path url to point to the file because I thought since this it going to be an android application, hard-coding the path might not work when deployed... Please correct me if I am wrong. Thanks bunch!


Solution

  • This shows how to do that. https://stackoverflow.com/a/14377185/2801237

    Also the 'package' your class is in has nothing to do with the 'path' where the file is being executed from. (two different concepts, 'package' = folder hierarchy of java source code files), 'path' = location on a filesystem of a specific file, your APK is being 'executed' in a particular place, and the location it writes a file is associated with that (I actually don't know where 'offhand' it writes by default, because I always get cache dir, or sd card root, etc.)