这里介绍安卓进行文件存储的方法
比如我们用QQ登录等软件时 哪怕第二次登录时账号密码是可以保存起来的
最终实现图
1.首先写一下布局文件 一共五个控件 2个Textview 两个Button 一个EditText
activity_main.xml新加代码如下
<TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请您输入要保存的内容:" /> <TextView android:id="@+id/showText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/adon" android:layout_below="@+id/adon" android:layout_marginTop="31dp" /> <Button android:id="@+id/showButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/addText" android:layout_below="@+id/addText" android:layout_marginTop="62dp" android:text="show" /> <Button android:id="@+id/adon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/addText" android:layout_below="@+id/showButton" android:text="save" /> <EditText android:id="@+id/addText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="15dp" android:ems="10" android:hint="请您在此处输入文件内容!" >
2.接下来修改MainActivity.java
MainActivity.java代码 import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.widget.Toast; public class MainActivity extends Activity { private EditText editText; private TextView showTextView; // 要保存的文件名 private String fileName = "chenzheng_java.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取页面中的组件 editText = (EditText) findViewById(R.id.addText); showTextView = (TextView) findViewById(R.id.showText); Button adon = (Button) this.findViewById(R.id.adon); Button showButton = (Button) this.findViewById(R.id.showButton); // 绑定单击事件 adon.setOnClickListener(listener); showButton.setOnClickListener(listener); } // 声明监听器 private View.OnClickListener listener = new OnClickListener() { public void onClick(View v) { Button view = (Button) v; switch (view.getId()) { case R.id.adon: save(); break; case R.id.showButton: read(); break; } } }; private void save() { String content = editText.getText().toString(); try { FileOutputStream outputStream = openFileOutput(fileName, Activity.MODE_PRIVATE); outputStream.write(content.getBytes()); outputStream.flush(); outputStream.close(); Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void read() { try { FileInputStream inputStream = this.openFileInput(fileName); byte[] bytes = new byte[1024]; ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); while (inputStream.read(bytes) != -1) { arrayOutputStream.write(bytes, 0, bytes.length); } inputStream.close(); arrayOutputStream.close(); String content = new String(arrayOutputStream.toByteArray()); showTextView.setText(content); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
源码地址:www.github.com/Ninse/Android