标签:
public void readAccount(){
// File file = new File(getFilesDir(), "info.txt");
File file = new File(getCacheDir(), "info.txt");
if(file.exists()){
try {
FileInputStream fis = new FileInputStream(file);
//把字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//读取txt文件里的用户名和密码
String text = br.readLine();
String[] s = text.split("##");
et_name.setText(s[0]);
et_pass.setText(s[1]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void login(View v){
String name = et_name.getText().toString();
String pass = et_pass.getText().toString();
CheckBox cb = (CheckBox) findViewById(R.id.cb);
//判断选框是否被勾选
if(cb.isChecked()){
//返回一个File对象,其路径是data/data/com.itheima.apirwinrom/files
// File file = new File(getFilesDir(), "info.txt");
//返回值也是一个File对象,其路径是data/data/com.itheima.apirwinrom/cache
File file = new File(getCacheDir(), "info.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write((name + "##" + pass).getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//创建并显示吐司对话框
Toast.makeText(this, "登录成功", 0).show();
}
最简单的打开sd卡的方式
File file = new File("sdcard/info.txt");
写sd卡需要权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
读sd卡,在4.0之前不需要权限,4.0之后可以设置为需要
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
使用api获得sd卡的真实路径,部分手机品牌会更改sd卡的路径
判断sd卡是否准备就绪
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//返回一个File对象,其路径是sd卡的真实路径
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
查找“可用空间”得到
<string name="memory_available" msgid="418542433817289474">"可用空间"</string>
查找"memory_available",得到
<Preference android:key="memory_sd_avail"
style="?android:attr/preferenceInformationStyle"
android:title="@string/memory_available"
android:summary="00"/>
查找"memory_sd_avail",得到
//这个字符串就是sd卡剩余容量
formatSize(availableBlocks * blockSize) + readOnly
//这两个参数相乘,得到sd卡以字节为单位的剩余容量
availableBlocks * blockSize
存储设备会被分为若干个区块,每个区块有固定的大小
SharedPreferences.Editor editor = getSharedPreferences("data",
MODE_PRIVATE).edit();editor.putString("name", "Tom");
editor.putInt("age", 28);
editor.putBoolean("married", false);
editor.commit();
用SharedPreference存储账号密码
往SharedPreference里写数据,另一种写法
/路径在data/data/包/share_pre
//拿到一个SharedPreference对象
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
//拿到编辑器
Editor ed = sp.edit();
//写数据
ed.putBoolean("name", name);
ed.commit();
从SharedPreference里取数据
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
//从SharedPreference里取数据
String name = sp.getBoolean("name", "");
把整个xml文件所有节点append到sb对象里
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?>");
sb.append("<messages>");
for (Message sms : smsList) {
sb.append("<sms>");
sb.append("<body>");
sb.append(sms.getBody());
sb.append("</body>");
sb.append("<date>");
sb.append(sms.getDate());
sb.append("</date>");
sb.append("<type>");
sb.append(sms.getType());
sb.append("</type>");
sb.append("<address>");
sb.append(sms.getAddress());
sb.append("</address>");
sb.append("</sms>");
}
sb.append("</messages>");
File file = new File("sdcard/sms.xml");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/4911076.html