/**
* @author Himi
* @保存方式:Stream 数据流方式
* @注意1:默认情况下,使用openFileOutput 方法创建的文件只能被其调用的应用使用,
* 其他应用无法读取这个文件,如果需要在不同的应用中共享数据;
*
* @注意2:因为android os内部闪存有限,所以适合保存较少的数据,当然我们也有解决的方法,
* 就是把数据保存在SD开中,这样就可以了,后面我也会向大家讲解 !
*
* @提醒1 调用FileOutputStream 时指定的文件不存在,Android 会自动创建它。
* 另外,在默认情况下,写入的时候会覆盖原 文件内容,如果想把新写入的内
* 容附加到原文件内容后,则可以指定其mode为Context.MODE_APPEND。
*
* @提醒2 启动程序就初始化的时候一定要注意处理!代码中有注释!一定要仔细看!
*
* @提醒3 这里我给大家讲两种方式,一种是原生态file流来写入/读入,
* 另外一种是用Data流包装file流进行写入/读入 其实用data流来包装进行操作;
* 原因是:包装后支持了更多的写入/读入操作,比如:file流写入不支持
* writeUTF(String str); 但是用Data包装后就会支持。
*
* @操作模式: Context.MODE_PRIVATE:新内容覆盖原内容
* Context.MODE_APPEND:新内容追加到原内容后
* Context.MODE_WORLD_READABLE:允许其他应用程序读取
* Context.MODE_WORLD_WRITEABLE:允许其他应用程序写入,会覆盖原数据。
*/
public class MainActivity extends Activity implements OnClickListener {
private EditText et_login, et_password;
private Button btn_save;
private TextView tv_title;
private FileOutputStream fos;
private FileInputStream fis;
private DataOutputStream dos;
private DataInputStream dis;
@Override
public void onCreate(Bundle savedInstanceState) {
String temp = null;
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
btn_save = (Button) findViewById(R.id.button_save);
btn_save.setOnClickListener(this);
et_login = (EditText) findViewById(R.id.editText_Login);
et_password = (EditText) findViewById(R.id.editText_Password);
tv_title = (TextView) findViewById(R.id.tv_title);
try {
// openFileInput 不像 sharedPreference 中
// getSharedPreferences的方法那样找不到会返回默认值,
// 这里找不到数据文件就会报异常,所以finally里关闭流尤为重要!!!
if (this.openFileInput("save.himi") != null) {
// --------------单纯用file来读入的方式-----------------
// fis = this.openFileInput("save.himi");
// ByteArrayOutputStream byteArray = new
// ByteArrayOutputStream();
// byte[] buffer = new byte[1024];
// int len = 0;
// while ((len = fis.read(buffer)) > 0) {
// byteArray.write(buffer, 0, len);
// }
// temp = byteArray.toString();
// -------------- 用data流包装后的读入的方式------------
fis = this.openFileInput("save.himi");//备注1
dis = new DataInputStream(fis);
et_login.setText(dis.readUTF());
et_password.setText(dis.readUTF());
// 这里也是在刚启动程序的时候去读入存储的数据
// 读的时候要注意顺序; 例如我们写入数据的时候
//先写的字符串类型,我们也要先读取字符串类型,一一对应!
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 在finally中关闭流!因为如果找不到数据就会异常我们也能对其进行关闭操作 ;
try {
if (this.openFileInput("save.himi") != null) {
// 这里也要判断,因为找不到的情况下,两种流也不会实例化。
// 既然没有实例化,还去调用close关闭它,肯定"空指针"异常!!!
fis.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onClick(View v) {
if (Environment.getExternalStorageState() != null) {
// 这个方法在试探终端是否有sdcard!
Log.v("Himi", "有SD卡");
}
if (v == btn_save) {
if (et_login.getText().toString().equals(""))
tv_title.setText("请输入帐号!");
else if (et_password.getText().toString().equals(""))
tv_title.setText("请输入密码!");
else {
try {
// ------单纯用file来写入的方式--------------
//fos = new FileOutputStream(f);
// fos.write(et_login.getText().toString().getBytes());
// fos.write(et_password.getText().toString().getBytes());
// ------data包装后来写入的方式--------------
fos = this.openFileOutput("save.himi", MODE_PRIVATE);//备注2
dos = new DataOutputStream(fos);
dos.writeUTF(et_login.getText().toString());
dos.writeUTF(et_password.getText().toString());
tv_title.setText("保存成功!可重新打开此程序,测试是" +
"否已经保存数据!/n(或者在'File Explorer'" +
"窗口下-data-data-com.himi-files路径下" +
"是否存在了'save.himi')");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 在finally中关闭流 这样即使try中有异常我们也能对其进行关闭操作 ;
try {
dos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}