activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.lnjr.store01.MainActivity" android:orientation="vertical">
<Button
android:text="store to File"
android:onClick="storetoFile"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="read From File"
android:onClick="readFromFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:text="get sd"
android:onClick="getSd"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="store to sharePreferences"
android:onClick="storeToSp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="read from sharePreferences"
android:onClick="readFromSp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.main
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void storetoFile(View view)throws Exception {
File file=this.getFileStreamPath("abc.txt");
OutputStream out=new FileOutputStream(file);
Log.i("file Path",file.getAbsolutePath());
out.write(97);
out.close();
}
public void readFromFile(View view) throws Exception{
File file=this.getFileStreamPath("abc.txt");
InputStream in=new FileInputStream(file);
Log.i("this result",in.read()+"");
in.close();
}
public void getSd(View view) {
File sd= Environment.getExternalStorageDirectory();
File sdd=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
Log.i("sdd card",sdd.getAbsolutePath());
}
public void storeToSp(View view) {
SharedPreferences sharedPreference= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor=sharedPreference.edit();
editor.putString("User","sll");
editor.putString("psd","123");
editor.putInt("age",100);
editor.commit();
}
public void readFromSp(View view) {
SharedPreferences sharedPreference=PreferenceManager.getDefaultSharedPreferences(this);
String s=sharedPreference.getString("User","null");
Log.i("User name",s);
}
}