In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Android how to achieve file storage", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Android how to achieve file storage" bar!
1. File storage case
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) } / / read data from file 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 () Store the file in a file 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 ();}}
Running result, Pass
2. SharePreferences storage case
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", "meritorious"); editor.putString ("type", "Movie"); 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:" + name + ", type:" + type);}}
Running result, Pass
3. Log in to the page to remember username and pwd
Activity_login.xml file
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) {/ / set the account and password to the text box 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 () / / if the account: admin and password: 123456, the login is considered successful if (username.equals ("admin") & & pwd.equals ("123456")) {mEditor = mSharedPs.edit () / / check whether the check box is selected 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, "user name or password entered incorrectly, please re-enter");});}}
Running result, Pass
Thank you for reading, the above is the content of "how to achieve file storage in Android". After the study of this article, I believe you have a deeper understanding of how to achieve file storage in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.