0

I'm trying to write a file on users mobile following this example

Storage Options [data-storage]

I want to create that file the first time users run my app.

After the first time users run my app, I want FIRST read the file, and THEN write something in it (if i need).

Following the example above i'm using FileInputStream stream= openFileInput(FILENAME),

Is there a way to know if the file i put in FileInputStream exists by checking the fIleInpuStream itself?

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
MDP
  • 4,177
  • 21
  • 63
  • 119
  • see http://stackoverflow.com/questions/8867334/check-if-a-file-exists-before-calling-openfileinput – njzk2 Jan 14 '13 at 16:56
  • i would say that if you can read from it, you can safely assume it exists, but you couldn't open it if it didn't exist, meaning you'd have to catch a FileNotFoundException – njzk2 Jan 14 '13 at 16:58
  • on the side note, why don't you use preferences to store your application specific data? It is a standard way of "remembering" application information rather than using a file in SD card. Also what happens, when the user changes/removes his sd card? Your application will think that it is getting launched for the first time. Is it what you are expecting? – Durairaj Packirisamy Jan 14 '13 at 17:05
  • @DurairajP : openFileInput open a private file for this context. It is not on the SDCard (as it is private). It uses the internal storage, as the Storage Options article states. – njzk2 Jan 15 '13 at 09:08

2 Answers2

1

Thank everybody for your help. Maybe the best way to do what i want to do was suggested by @Durairaj P.

I used Preferences.

But i'm still wondering if it's suitable and appropriate for what i want to do. I want to keep track of the points that users earn while playing my game; when users re-open my app, i have to show all the points they earned since they installed my app. I'm just wondering if Preferences are suitable and appropriatefor this, or if i should use something else.

Anyway i post my code, it might help someone

public class managePreferences{
Context context;    

    managePreferences(Context context){
        this.context = context;
    }


    public String readPreference(String fieldName, String defaultValue){    
        SharedPreferences prefs = context.getSharedPreferences("MY_PREFERENCES", Context.MODE_PRIVATE);
        String value = prefs.getString(fieldName, defaultValue);
        return value ;
    }


    public void writePreference(String fieldName, String value){
        SharedPreferences prefs = context.getSharedPreferences("MY_PREFERENCES", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(fieldName, value);
        editor.commit();

    }

}
MDP
  • 4,177
  • 21
  • 63
  • 119
-1

I would use

context.fileList();

and test if my file is somewhere. (quickest way is to make a List of it and use contains()) :

Arrays.asList(context.fileList()).contains(FILENAME);
njzk2
  • 38,969
  • 7
  • 69
  • 107