注意权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
内部存储空间:
- RAM内存:运行内存,相当于电脑的内存
- ROM内存:存储内存,相当于电脑的硬盘
外部存储空间:
SD卡:相当于电脑的移动硬盘
package com.ldw.rwinrom;
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.EditText;
import android.widget.Toast;
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 block
e.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 block
e.printStackTrace();
}
}
Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
}
}