这篇文章主要讲解了“Android如何实现文件存储”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android如何实现文件存储”吧!
1、文件存储案例
public class TestActivity extends AppCompatActivity { private EditText mFileEdit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); initView(); } private void initView() { mFileEdit = findViewById(R.id.fileEdit); String inputText = load(); if (!TextUtils.isEmpty(inputText)) { mFileEdit.setText(inputText); mFileEdit.setSelection(inputText.length()); Toast.makeText(this, "Restoring succeeded", Toast.LENGTH_SHORT).show(); } } @Override protected void onDestroy() { super.onDestroy(); String inputText = mFileEdit.getText().toString(); save(inputText); } // 从文件中读取数据 public void save(String inputText) { FileOutputStream outputStream = null; BufferedWriter writer = null; try { outputStream = openFileOutput("data", Context.MODE_PRIVATE); writer = new BufferedWriter(new OutputStreamWriter(outputStream)); writer.write(inputText); } catch (IOException e) { e.printStackTrace(); } finally { try { if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); } } } // 将文件存储到文件中 public String load() { FileInputStream inputStream = null; BufferedReader reader = null; StringBuilder builder = new StringBuilder(); try { inputStream = openFileInput("data"); reader = new BufferedReader(new InputStreamReader(inputStream)); String line = ""; while ((line = reader.readLine()) != null) { builder.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return builder.toString(); } }
运行结果,Pass
2、SharePreferences存储案例
public class SharePfsActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "SharePfsActivity"; private Button mSharedData; private Button mRestoreData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shared_pfs); initView(); } private void initView() { mSharedData = findViewById(R.id.sharedBtn); mSharedData.setOnClickListener(this); mRestoreData = findViewById(R.id.restoreBtn); mRestoreData.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.sharedBtn: sharedData(); break; case R.id.restoreBtn: restoreData(); break; default: break; } } private void sharedData() { SharedPreferences.Editor editor = getSharedPreferences("shareData", MODE_PRIVATE).edit(); editor.putString("name", "功勋"); editor.putString("type", "电影"); editor.apply(); } private void restoreData() { SharedPreferences preferences = getSharedPreferences("shareData", MODE_PRIVATE); String name = preferences.getString("name", ""); String type = preferences.getString("type", ""); Log.d(TAG, "名称:" + name + ",类型:" + type); } }
运行结果,Pass
3、登录页面,实现记住username和pwd功能
activity_login.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <EditText android:id="@+id/username" android:layout_width="240dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" /> <EditText android:id="@+id/pwd" android:layout_width="240dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Remeber password" /> </LinearLayout> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout>
LoginActivity .class
public class LoginActivity extends AppCompatActivity { private static final String TAG = "LoginActivity"; private Button mLogin; private CheckBox mRemember; private EditText mUsername; private EditText mPwd; private SharedPreferences mSharedPs; private SharedPreferences.Editor mEditor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); } private void initView() { mSharedPs = PreferenceManager.getDefaultSharedPreferences(this); mUsername = findViewById(R.id.username); mPwd = findViewById(R.id.pwd); mRemember = findViewById(R.id.remember); mLogin = findViewById(R.id.login); boolean isRemember = mSharedPs.getBoolean("remember_pwd", false); if (isRemember) { // 将账号和密码都设置到文本框中 mUsername.setText(mSharedPs.getString("username", "")); mPwd.setText(mSharedPs.getString("pwd", "")); mRemember.setChecked(true); } mLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = mUsername.getText().toString(); String pwd = mPwd.getText().toString(); // 如果账号:admin,密码:123456,就认为登录成功 if (username.equals("admin") && pwd.equals("123456")) { mEditor = mSharedPs.edit(); // 检查复选框是否被选中 if (mRemember.isChecked()) { mEditor.putString("username", username); mEditor.putString("pwd", pwd); mEditor.putBoolean("remember_pwd", true); } else { mEditor.clear(); } mEditor.apply(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } else { Log.d(TAG, "用户名或密码输入错误,请重新输入"); } } }); } }
运行结果,Pass
感谢各位的阅读,以上就是“Android如何实现文件存储”的内容了,经过本文的学习后,相信大家对Android如何实现文件存储这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。