views:

78

answers:

1

I'm writing a simple budget app for myself, and I'm having trouble figuring out how to write to internal storage. I don't seem to be writing to the file properly, and I can't find any more in depth examples than the Data Storage article on developer.android.com

Basically, I'm trying to write a test float to the MyBalance file, then read it into balance. In my actual code I use try/catch statements around the file in/out operations, but I skipped them to make the code more readable.

float test = 55;
float balance;
byte[] buffer = null;
FileOutputStream fos = openFileOutput( "MyBalance", Context.MODE_PRIVATE );
fos.write(Float.floatToRawIntBits(balance));
fis.read(buffer); //null pointer
ByteBuffer b = ByteBuffer.wrap(buffer);
balance=b.getFloat();

That's the gist of it, anyone see what I'm doing wrong?

Edit: Thanks for the reply, I went ahead and converted to/from String like you suggested, but I still don't think the file is being created. I have an if statement that reads from it if it exists in onResume() and it isn't being run. Lemme post some of my code.

Here's how I'm writing the file, (setbal is an EditText and balanceview is a TextView):

 balance = Float.valueOf(setbal.getText().toString());
 balanceview.setText(setbal.getText());
 balstring = String.valueOf(balance);
 for (int i = 0; i < balstring.length(); ++i)
     try {
     fos.write((byte)balstring.charAt(i));
     } catch (IOException e) {
         e.printStackTrace();
     }

I check if the file exists in onResume() like so:

File file = new File("data/data/com.v1nsai.mibudget/balance.txt");

Is that where an internal file for that context would be stored?

+2  A: 

So this isn't exactly what you asked for, but this is how I have it working for Strings, and it may be helpful to you to see. (You could box the primatives and toString them of course if you wanted to use this code.)

Writing

FileOutputStream fos = context.openFileOutput("savedstate.txt", 0);

for (int i = 0; i < out.length(); ++i)
    fos.write((byte)out.charAt(i));

Reading

StringBuilder inb = new StringBuilder();

FileInputStream fis = this.mContext.openFileInput("savedstate.txt");

int ch;
while((ch = fis.read()) != -1)
    inb.append((char)ch);

Update

One thought that springs to mind is that you may not want to trust using a File object with a hand typed full path to the file. Instead, just use the FileInputStream with the context object and a relative path like in my code, then see if you get a String back of some length or something like that, or an exception that the file doesn't exist.

If you are really curious of where the file is created, or want to see it with your own eyes, I believe you can browse to it on your phone through the file manager in DDMS.

One last thing, I would suggest moving the try/catch block outside of your writing loop. Since it is an identical task being repeated, there is no need for the overhead of that approach, though it is typically good practice to minimize the size of your try/catch blocks.

Ok really one last thing, if you want to use the File object with the full path, you might want to have the path be the following:

File file = new File("/data/data/com.v1nsai.mibudget/balance.txt");

The beginning slash may make all the difference.

Guzba