I've been fooling around with the android platform, messing around with different ways of storing data. Right now I am using the Context
methods openFileInput()
and openFileOutput()
.
I created a file called default just as the documentation for these two methods told me. Here's some example code (these examples are replicas of what I did, I am aware that the filename and variables are named differently):
openFileOutput()...
Context cont = /* defined somewhere */;
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = cont.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.flush();
fos.close();
openFileInput()...
FileInputStream fis = cont.openFileInput("hello_file");
byte[] buffer = new byte[(int) fis.getChannel().size()];
fis.read(buffer);
String str= "";
for(byte b:buffer) str+=(char)b;
fis.close();
In these code snippets, "hello world" should be written to the file "hello_file", and should be what's in str
. The problem I'm having with my code is that no matter what I write to my file, the FileInputReader
picks up nothing.
I scrolled through the permissions listed in the android documentation, but I could not find anything about internal storage (and I am pretty sure you don't need a permission for something like this).
The bottom line is, I don't understand why the FileInputWriter
isn't writing anything or why the FileInputReader
isn't reading anything (I don't know which it is) when the code runs fine and without error.