user3324600
user3324600

Reputation: 37

Internal storage project

hello everyone out there. here is my project. since I am a beginner at Java programming I am trying different types of storage, right now internal storage. I have a screen that accepts a username and password, when saved it saves a text file, david.txt . I go to the second screen push load and the information is pulled from the text file and populates the Username and Password.

this is the code for Main Activity

public static final String TAG = "MainActivity";

EditText userName, password;

//... standard stuff

    userName = (EditText) findViewById(R.id.userName);
    password = (EditText) findViewById(R.id.password);
}

public void save (View view)
{
    String text1 = userName.getText().toString(); // example: David
    String text2 = password.getText().toString(); // Example: Vilma123
    File file=null;
    text1=text1+" "; // Adds space between username and password
    FileOutputStream fileOutputStream=null;

        try {
        file=getFilesDir(); //Gets directory of stored file
        fileOutputStream = openFileOutput("david", Context.MODE_PRIVATE);
        fileOutputStream.write(text1.getBytes());
        fileOutputStream.write(text2.getBytes());

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        Log.d(TAG, e.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d(TAG, e.toString());
    }
    finally{
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.d(TAG, e.toString());
        }
    }



    Toast.makeText(this, "Saved successfully"+file+" /david.txt", Toast.LENGTH_LONG).show();
}

this works great, on the screen it I push "Go to B" button which activates the "load" method in the SecondActivity page: see below

EditText userName, password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    userName = (EditText) findViewById(R.id.editText2);
    password = (EditText) findViewById(R.id.editText4);

}
public void load (View view)
{   
        Log.d("david", "starting fileInputStream");

        try {
            FileInputStream fileInputStream =      openFileInput("david.txt");
            int read = -1; //-1 indicates file is empty
            StringBuffer buffer = new StringBuffer();

            while ((read = fileInputStream.read())!=-1);
            {
                buffer.append((char)read);
            }

                Log.d("david", buffer.toString());

                String user = buffer.substring(0, buffer.indexOf(""));

                String pass = buffer.substring(buffer.indexOf("")+1);

                userName.setText(user);
                password.setText(pass);

        fileInputStream.close();


    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   


    Toast.makeText(this, "Load successful", Toast.LENGTH_SHORT).show();
}

ok this is where it get fun. when I push the Load button it is supposed to load the username and password which i typed in the last screen but it is not loading. This log file has a 0 value

Log.d("david", buffer.toString());

but the .txt file has the info I placed. Could some one please help me on this one. I show no errors and all Toasts display as they should.

Upvotes: 0

Views: 81

Answers (2)

Sush
Sush

Reputation: 3874

public class Preference
{

  // check for username length
   public static String getUserName(Context context)
   {
    return PreferenceManager.getDefaultSharedPreferences(context).getString(
            "username", "");
   }

  public static boolean setUserName(Context context,String username)
  {
    return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).edit()
            .putString("username", username).commit();
  } 

// check for password length 
  public static String getPassword(Context context)
  {
    return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).getString(
            "password", "");
  }


  public static boolean setPassword(Context context,String password)
  {
    return PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).edit()
            .putString("password", password).commit();
  } 


} 

Upvotes: 0

user2060383
user2060383

Reputation: 989

You are doing a small mistake which is while storing data you are using file name david and while retriving data you are using file name david.txt

In MainActivity, change your following line,

fileOutputStream = openFileOutput("david", Context.MODE_PRIVATE);

with this line,

fileOutputStream = openFileOutput("david.txt", Context.MODE_PRIVATE);

Upvotes: 1

Related Questions