0

I'm trying to save some properties and print them out, but it doesn't work for some reason. Here's the code. My intention is to make a file that contains passwords and usernames, as pairs. And then later on I'm going to iterate through them. Is there an easier/better way to do this?

public void createUser(){

    FileOutputStream fileOutputStream;
    try{
        fileOutputStream = context.openFileOutput("Testfile", context.MODE_PRIVATE);
        properties.setProperty("username", "password");
        properties.store(fileOutputStream, "User created");
        fileOutputStream.close();

    } catch(IOException e) {
        Toast.makeText(context, "Error create", Toast.LENGTH_LONG).show();
    }

And then I'm trying to print them out:

public void printUser( TextView textView){

    File infoFile = new File(context.getFilesDir(), "TestFile");
    BufferedReader br;
    try{
        br = new BufferedReader(new java.io.FileReader(infoFile));
        properties.load(br);
        textView.setText(properties.getProperty("username"));

    } catch(IOException e) {
        Toast.makeText(context, "Error printing users", Toast.LENGTH_LONG).show();
    }
}

But nothing happens. I did basically the same in eclipse with JSwing and it worked well there.

rafaelc
  • 48,227
  • 12
  • 46
  • 72
Jullix993
  • 95
  • 9
  • Why not use [SharedPreferences](http://developer.android.com/reference/android/content/SharedPreferences.html)? – Igor Morais Mar 26 '15 at 18:35
  • @MoraisIgor I'm not familiar with that....I think I will stick with properties for now. – Jullix993 Mar 26 '15 at 18:48
  • Shared Preferences is the most common way to save simple data, see these examples: [here](http://stackoverflow.com/questions/23024831/android-shared-preferences-example), [here](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) and [here](http://www.tutorialspoint.com/android/android_shared_preferences.htm). – Igor Morais Mar 26 '15 at 19:08
  • Thanks, I'll look into that – Jullix993 Mar 26 '15 at 19:21
  • Look at [it](http://developer.android.com/guide/topics/data/data-storage.html) too. – Igor Morais Mar 26 '15 at 19:23

0 Answers0