文件存储不对存储内容进行任何的格式化处理,全部数据都是原封不动保存到文件当中。java
Context类中提供了一个openFileOutput()方法,返回一个FileOutputStream对象,而后就能够用javaI/O流去写文件中了。文件都默认放在/data/data/
/files/目录下。 android
public void save(String data){ FileOutputStream out=null; BufferedWriter writer=null; try{ out=openFileOutput("data", Context.MODE_PRIVATE);//第一个参数是文件的名称,第二个参数是模式。MODE_PRIVATE表示覆盖,MODE_APPEND表示追加。 writer=new BufferedWriter(new OutputStreamWriter(out)); writer.write(data); }catch (IOException e){ e.printStackTrace(); }finally { try { if(writer!=null) writer.close(); }catch (IOException e){ e.printStackTrace(); } } }
Context类还提供了一个openFileInput(),返回的是一个FileInputStream。它接收一个参数,即要读取的文件名。而后系统会去/data/data/
/files/目录下寻找。 app
public String load(String name){ FileInputStream input=null; BufferedReader reader=null; StringBuilder content=new StringBuilder(); try { input=openFileInput(name); reader=new BufferedReader(new InputStreamReader(input)); String line=""; while((line=reader.readLine())!=null){ content.append(line); } }catch (IOException e){ e.printStackTrace(); }finally { try { if(reader!=null) reader.close(); }catch (IOException e){ e.printStackTrace(); } } return content.toString(); }
SharedPreferences是使用键值对的方式来存储数据的ide
1.获取SharedPreferences对象布局
此方法接收两个参数,第一个参数用于指定SharedPreferences文件的名称,若是直接的名称文件不存在,则会建立一个文件,SharedPreferences文件都是存放在/data/data/
/shared_prefs/目录下。第二个参数用于指定操做模式,目前只有MODE_WORLD_READABLE这一种模式可选,它是默认的操做模式,和直接传入0效果是相同的,表示只有当前的应用才能够对这个文件进行读写. ui
只接收一个操做模式参数,由于自动将当前活动的类名看成SharedPreferences的文件名this
这是一个静态方法,它接收一个Context参数,并自动使用当前应用程序的包名做为前缀来命名SharedPreferences文件。code
2.向SharedPreferences中存储数据xml
(1)调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象
(2)向SharedPreferences.Editor对象中添加数据,好比添加一个布尔型数据就使用putboolean()方法等
(3)调用apply()方法将添加的数据提交,从而完成数据存储操做对象
1.获取SharedPreferences对象
2.从SharedPreferences中读取数据
直接使用SharedPreferences对象的getxxx(键,默认值)方法获取对应类型值
1.新建LoginActivity活动
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:text="Account:" android:textSize="18dp" android:layout_gravity="center"/> <EditText android:id="@+id/account" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_gravity="center_vertical"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:text="Password:" android:textSize="18dp" android:layout_gravity="center_vertical" /> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:inputType="textPassword" android:layout_gravity="center_vertical"/> </LinearLayout> <CheckBox android:id="@+id/remeber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="remember password"/> <Button android:id="@+id/login" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login"/> </LinearLayout>
LoginActivity类:
ublic class LoginActivity extends AppCompatActivity { private SharedPreferences sharedPreferences; private SharedPreferences.Editor editor; private EditText account; private EditText password; private CheckBox isRemeber; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this); account=(EditText)findViewById(R.id.account); password=(EditText)findViewById(R.id.password); isRemeber=(CheckBox)findViewById(R.id.remeber); if(sharedPreferences.getBoolean("isRemeber",false)){//以前记住过密码 account.setText(sharedPreferences.getString("account","")); password.setText(sharedPreferences.getString("password","")); isRemeber.setChecked(true); } Button button=(Button)findViewById(R.id.login); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String accountstr=account.getText().toString(); String passwordstr=password.getText().toString(); if(accountstr.equals("admin")&&passwordstr.equals("123456")) { editor=sharedPreferences.edit(); if (isRemeber.isChecked()) {//选择记住密码 editor.putBoolean("isRemeber",true); editor.putString("account", accountstr);//保存帐户 editor.putString("password", passwordstr);//保存密码 }else{ editor.clear();//若是没选中说明清空 } editor.apply();//运行 Intent intent=new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); finish(); }else{ Toast.makeText(LoginActivity.this,"account or password is invald",Toast.LENGTH_SHORT).show(); } } }); } }