2

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.

user1780932
  • 81
  • 1
  • 7
  • Pass context `Context ctx` and use `fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);` – Sachin Thapa Aug 16 '13 at 05:17
  • Just found another post on stackoverflow that has complete code http://stackoverflow.com/questions/3625837/android-what-is-wrong-with-openfileoutput – Sachin Thapa Aug 16 '13 at 05:19
  • lol i deleted it from the post as i was typing on accident – user1780932 Aug 16 '13 at 05:21
  • @Sachin sorry for the incomplete code, but I have it that way in my code, taking in the context elsewhere. There is no error when my code is run. The objects themselves all seem to work when I use their different methods. But fis reads nothing or fos writes nothing. – user1780932 Aug 16 '13 at 05:27
  • While writing, file created is empty or you dont event see the file ? – Sachin Thapa Aug 16 '13 at 05:32
  • the file exists, but nothing is written to the file when i attempt to write it, or at least when i attempt to read the file, it registers the file as empty. – user1780932 Aug 16 '13 at 05:37
  • Can you please add `fos.flush()` after writing contents to file. – Sachin Thapa Aug 16 '13 at 05:44
  • I had fos.flush() in my code before, (i put it back in and put it in the example i posted) but the problem still persists. – user1780932 Aug 16 '13 at 05:57
  • There're several reasons, why this might not work. Do you see the file with DDMS after `openFileOutput` and `close()`? What size has it? Then, when reading the file. `openFileInput()` should throw a `FileNotFoundException` if the file does not exist. If the code runs thru, it seems to find the file. You're using `MODE_PRIVATE` which means, the file is private to the application. Does the input part run in the same application as the writing part? On the reading side, You should read here till EOF is reached. What's the actual length reported by `fis.getChannel().size()`? – jboi Aug 16 '13 at 07:18
  • @jboi the file exists, i tested that using `fileList()` and the code runs through so it does find it. I also tried `while(fis.read() != -1) str += (char) read(); ` and same thing. The size of the file is 0 it says, so therefore the 'FileOutputStream' is probably the thing that's not working. It's all in one application as well. As far as I can see, it should work. In fact if i take my code and use it in regular java to work on another file, it works properly. So I don't know. Maybe it's a problem with the version I'm using or something on the other side? But I don't see how that could be. – user1780932 Aug 16 '13 at 07:44

1 Answers1

1

I write back as answer, because this is getting too much for a comment. I've tried your code - and well, it just works fine.

The only thing I can imagine is, that your device has a problem. In which case I would expect some exception...
What you still can do, is to copy my code and check the log. See if it works, or if you might get some exception. Then check how much internal memory you got in your device (is it real or emulated?)
If it is emulated it might be too small for even such a small file.

Here's my code, that I put into onResume()

    String FILENAME = "hello_file";
    String string = "hello world!";

    try {
        FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        Log.e("STACKOVERFLOW", e.getMessage(), e);
    }

    try {
        FileInputStream fis = 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();
        Log.i("STACKOVERFLOW", String.format("GOT: [%s]", str));
    } catch (IOException e) {
        Log.e("STACKOVERFLOW", e.getMessage(), e);
    }

The output:

08-16 08:31:38.748: I/STACKOVERFLOW(915): GOT: [hello world!]

Is there a category of: "Helpful, but not sovling the problem"?

jboi
  • 11,324
  • 4
  • 36
  • 43