在内文件写操作博客中存在着一些问题,如下图所示,那就是在我们按下back键,再次打开时应用不进行回显,而按下home键时再次打开则可以回显,这是因为我们在按下back键后activity结束,再次打开时是重新创建了一个activity,而home键则是使activity进入后台运行而没有进入finish()状态,现在我们通过对内部txt文件的读取信息来使界面能够进行回显。
新增的读代码
public void readAcount(){
File file=new File("data/data/com.example.instorage/info.txt");
FileInputStream fis;
try {
fis = new FileInputStream(file);
//把字节流转换为字符流
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
//读取txt中的用户名和密码
String line=br.readLine();
String[] s=line.split("##");
//ctrl+1可以直接将局部变量设置为全局变量
et_username.setText(s[0]);
et_password.setText(s[1]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
全部的读代码
package com.example.instorage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_username;
private EditText et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instorage);
readAcount();
}
public void readAcount(){
File file=new File("data/data/com.example.instorage/info.txt");
FileInputStream fis;
try {
fis = new FileInputStream(file);
//把字节流转换为字符流
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
//读取txt中的用户名和密码
String line=br.readLine();
String[] s=line.split("##");
//ctrl+1可以直接将局部变量设置为全局变量
et_username.setText(s[0]);
et_password.setText(s[1]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void login (View v){
et_username=(EditText) findViewById(R.id.et_username);
et_password=(EditText) findViewById(R.id.et_password);
String name=et_username.getText().toString();
String password=et_password.getText().toString();
CheckBox cb=(CheckBox) findViewById(R.id.cb);
//判断选框有没有被勾选,调用isChecked()
if(cb.isChecked()){
//这就是内部存储空间的路径
//先进行部署,之后通过 Window-show View-other-File Explorer-data/data下找到相应包名的文件
File file=new File("data/data/com.example.instorage/info.txt");
FileOutputStream os;
try {
os = new FileOutputStream(file);
String buffer=name+"##"+password;
os.write(buffer.getBytes());
os.close();//记得关闭文件
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
}
}
优化代码
1、全局变量一般放在onCreate方法中赋初值
2、当第一次使用时,并不存在txt文件,此时读方法就会出现问题,因此需要判断文件是否存在。
public class MainActivity extends Activity {
private EditText et_username;
private EditText et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instorage);
et_username=(EditText) findViewById(R.id.et_username);
et_password=(EditText) findViewById(R.id.et_password);
readAcount();
}
public void readAcount(){
File file=new File("data/data/com.example.instorage/info.txt");
if(file.exists()){
FileInputStream fis;
try {
fis = new FileInputStream(file);
//把字节流转换为字符流
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
//读取txt中的用户名和密码
String line=br.readLine();
String[] s=line.split("##");
//ctrl+1可以直接将局部变量设置为全局变量
et_username.setText(s[0]);
et_password.setText(s[1]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void login (View v){
String name=et_username.getText().toString();
String password=et_password.getText().toString();
CheckBox cb=(CheckBox) findViewById(R.id.cb);
//判断选框有没有被勾选,调用isChecked()
if(cb.isChecked()){
//这就是内部存储空间的路径
//先进行部署,之后通过 Window-show View-other-File Explorer-data/data下找到相应包名的文件
File file=new File("data/data/com.example.instorage/info.txt");
FileOutputStream os;
try {
os = new FileOutputStream(file);
String buffer=name+"##"+password;
os.write(buffer.getBytes());
os.close();//记得关闭文件
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
}
}
使用API在内部存储器中的读写
使用API在内部存储器中的读写仅需要修改一句话就可以了
1、将File file=new File("data/data/com.example.instorage/info.txt");
修改为
//获得的路径为data/data/com.example.instorage/files
File file=new File(getFilesDir(),"info.txt");
2、获得缓存路径,该路径下的内容你不要企图让系统帮你删除,应该自己设定个值删除,比如1MB,因此一些重要的文件还是不要记录在这个文件下。
File file=new File(getCacheDir(),"info.txt");