In my application there are some configuration data, such as ipaddress, portno, title and etc. I want to keep these data in phone privately. I decided to write data in below format
IPAddress=127.0.0.1
Port=1234
Title=MyNewApplication
i am confused with file streams. I also want to update values without using a temperory file. Please provide a solution to this.
I tried with below code
public class Mtx {
public static final String PREFS_NAME = "MyPrefsFile";
public static void ConfWrite(String type, String value) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(null);
SharedPreferences.Editor editor = settings.edit();
editor.putString("IPAddress", "127.0.0.1");
editor.putInt("port", 1234);
editor.putString("Title", "MyNewApplication");
// Commit the edits!
editor.commit();
}
public static void ConfRead( String type, String value ) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(null);
String ipAddress = settings.getString("IPAddress", "");
int port = settings.getInt("port", 0);
String title = settings.getString("Title", "");
Log.e("", title);
}
}
Use
SharedPreferencesinstead.Simple to use.
Details can be found here.
http://developer.android.com/guide/topics/data/data-storage.html