Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello , simple question : how to save ( register) a value in a file with android
Posted
Comments
[no name] 16-Aug-13 9:31am    
http://www.bing.com/search?q=how+to+save+%28+register%29+a+value+in+a+file+with+android

1 solution

Hello anel91,

You will have to use file io API's[^]. Following sample should get you started.
Java
private void save(String filename, String data)
{
    try
    {
        FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
        fos.write(data.getBytes());
        fos.close();
        toast("File successfully saved.");
    }
    catch (Exception ex)
    {
        toast("Error saving file: " + ex.getLocalizedMessage());
    }
}

You will also find this[^] particular tutorial very useful.

Regards,
 
Share this answer
 
Comments
anel91 16-Aug-13 9:40am    
thank you very much :)) where can i find the file then ???
pasztorpisti 16-Aug-13 11:12am    
In the private dir of the application. Only the app has access to that. There is also a public folder into which you can save files visible from outside (for example by using the filesystem browser of Eclipse/ADT). I write my applications to use the public dir when debugging and in the release app I switch to using the private folder. This way I can inspect the files from outside easily for debugging. http://chrisrisner.com/31-Days-of-Android--Day-23%E2%80%93Writing-and-Reading-Files.
pasztorpisti 16-Aug-13 11:14am    
+5. You can avoid some leakage with an additional finally block inside the try block of your exception handler:
try {
fos.write(data.getBytes());
} finally {
fos.close();
}
Prasad Khandekar 19-Aug-13 10:28am    
Thank's pasztorpisti.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900