Reputation: 501
I've created an app that creates a file on the device that stores about 5 values (double). I think I'm writing the file successfully because I get to see a toast message on the device just after the file is written.
If I try and add a newline to the command, then I never see the toast message so the writing is failing.
Here is my WORKING code:
try{
String path = this.getFilesDir().getAbsolutePath();
file = new File(path + "/" + filename);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(filename, Context.MODE_PRIVATE));
for (int i = 0; i < priceArray.length; i++) {
outputStreamWriter.append(String.format("%.2f", priceArray[i]));
testText.append(String.format("%.2f", priceArray[i]));
}
Toast.makeText(this, "Prices saved successfully!", Toast.LENGTH_SHORT).show();
outputStreamWriter.close();
}catch (Exception e) {
e.printStackTrace();
}
What is the best way to implement entries on their own line in the file? The line testText.append(String.format("%.2f", priceArray[i])); in my code is there for me to see the values as I can't locate the file on the file system.
Upvotes: 0
Views: 285
Reputation: 1032
You should format correctly: outputStreamWriter.append(String.format("%.2f\n", priceArray[i]); Notice \n after %.2f.
Upvotes: 1