I am writing a simple budget application for myself, and it is difficult for me to understand how to write on the internal storage. It seems that I am not writing to the file correctly, and I cannot find more detailed examples than the "Data Warehouse" developer.android.com
Basically, I am trying to write a test float to a MyBalance file, and then read it into balance. In my actual code, I use try / catch statements around file I / O, 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);
ByteBuffer b = ByteBuffer.wrap(buffer);
balance=b.getFloat();
To judge this, does anyone see what I'm doing wrong?
Edit: Thanks for the answer, I went ahead and converted to / from String as you suggested, but I still don't think the file is being created. I have an if statement that reads it if it exists in onResume () and it does not start. Lemme will post some of my code.
This is how I write the file (setbal is EditText and balanceview is 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 () as follows:
File file = new File("data/data/com.v1nsai.mibudget/balance.txt");
This is where the internal file is stored for this context?
source
share