6.读写内部存储的文件

来源:互联网 发布:打车软件不合法 编辑:程序博客网 时间:2024/05/17 01:15

注意权限

public class MainActivity extends Activity {    private EditText et_name;private EditText et_pass;@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                et_name = (EditText) findViewById(R.id.et_name);    et_pass = (EditText) findViewById(R.id.et_pass);        read();    }        //读取文件    public void read(){     File file = new File("data/data/com.ldw.rwinrom/12.txt");     if(file.exists()){     try {     FileInputStream fis = new FileInputStream(file);//把字节流转换成字符流BufferedReader br = new BufferedReader(new InputStreamReader(fis));//读取txt里面的数据,readLine可以读取一行数据String text = br.readLine();String[] s = text.split("==");        et_name.setText(s[0]);    et_pass.setText(s[1]);    fis.close();    br.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}          }    }    //点击登陆写文件    public void login(View v){        CheckBox cb = (CheckBox) findViewById(R.id.cb);    String name = et_name.getText().toString();    String password = et_pass.getText().toString();        if(cb.isChecked()){    File file = new File("data/data/com.ldw.rwinrom/12.txt");        try {    FileOutputStream fos = new FileOutputStream(file);fos.write((name + "==" + password).getBytes());fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}    }        Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();        }



0 0
原创粉丝点击