I was trying to open a file for reading.
When using: Scanner input = new Scanner(filename); the file could not be found
but when I used:
InputStream in = openFileInput(filename);
Scanner input = new Scanner(in);
It worked. Why was the first line of code wrong?
Files are stored on the device in a specific, application-dependent location, which is what I suppose
openFileInputadds at the beginning of the file name. The final result (location + file name) is constructed as follows:Note also that the documentation states that the
openFileInputparameter cannot contain path separators.To avoid hard-coding the location path, which could in principle even be different from device to device, you can obtain a
Fileobject pointing to the storage directory by callinggetFilesDir, and use it to read whatever file you would like to. For example:Note that constructing a
Scannerby passing aStringas a parameter would result in the scanner working on the content of the string, i.e. interpreting it as the actual content to scan instead of as the name of a file to open.