How to store data in android? [duplicate]-Collection of common programming errors

I’m trying to store some data which I received from my server into a file in my android project. But I’m not able to do that.. I used the following code which I’ve shown below.. Please help me out someone. Thanks.

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

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

The error its giving is “Content cannot be resolved to a variable”. I imported android.content.. Then its giving an error for all the 3 lines called “Unhandled Exception type FileNotFoundException” But I have created that file.. The problem is that I’m not knowing in which folder of the project do I keep that file? Can someone please help me out?

  1. Two way of saving a data is allowed in Android that is by using SharedPreference or Sqlite DB File writing is not advisable.

    For your Question 1. Manifest Should have write permission.

    If not please send the code.. Its very basic

    My suggestion is SharedPreference for you.

  2. The most common mistake to take place here is that you have not given the permission to wite external storage in AndroidManifest.xml file.

    Please include this into your manifest file

  3. Use

    File root = context.getFilesDir();
    
    File file = new File(root, fileName);
    fos = new FileOutputStream(file);
    
  4. 1st check the Manifest Permission as suggested by Kumar if still not sorted your problem out then take a look at the following link to get Help