用api在内部存储中读写文件
用到的两个方法
- 用getFileDir()获取当前应用的绝对路径,并在其中建立一个files文件夹,info.txt文件就被新建在这
个文件夹中:File file=new File(getFileDir(),"info.txt");
- 用getCacheDir()获取当前应用的缓存文件夹,在其中建立一个info.txt文件,进行读写的操作:
File
file=new File(getCacheDir(),"info.txt"); - 下面是一个简单对文件操作的示例:
界面UI有两个文本框,一个Checkbox,一个button按钮
//button的点击事件,点击时在对应的位置创建文件,这里是把两个文本框的内容写入文件中
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;
//**有另外一种方式写文件输出流,这里第一个参数表示路径下files文件夹下的文件,第二个参数
//表示模式 MODE_PRIVATE或者0 是默认设置, MODE_APPEND 是对已经存在的文件进行追加操作;
//MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE表示对文件的读写权限进行设定**
//FileOutputStream fos=openFileOutput("info.txt",MODE_PRIVATE);
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();
}
读取数据,并回显示到文本输入框上
//这里读取数据的方法
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()){
//MEDIA_UNKNOWN:不能识别sd卡
//MEDIA_REMOVED:没有sd卡
//MEDIA_UNMOUNTED:sd卡存在但是没有挂载
//MEDIA_CHECKING:sd卡正在准备
//MEDIA_MOUNTED:sd卡已经挂载,可用
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//返回一个File对象,其路径是sd卡的真实路径
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
// File file = new File("sdcard/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();
}
}
else{
Toast.makeText(this, "sd卡不可用哟亲么么哒", 0).show();
}
}
//创建并显示吐司对话框
Toast.makeText(this, "登录成功", 0).show();
}
进行写数据时,和这个类似,就不做过多赘述。
获取手机空间大小
//获取外部空间大小
File path = Environment.getExternalStorageDirectory();
//File pathFile=Environment.getDataDirectory();//获取内部空间大小
StatFs stat = new StatFs(path.getPath());//这个对象用来获得当前手机内存或手机sd卡使用情况
long blockSize;
long totalBlocks;
long availableBlocks;
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
availableBlocks = stat.getAvailableBlocksLong();
return availableBlocks * blockSize;
SharedPreferences
SharedPreference适合保存一些零散的数据,保存数据时,不需要 指定文件的类型,会自动生成一个.xml文件来保存数据。
示例用法:
//**点击触发事件**
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()){
//使用sharedPreference来保存用户名和密码
//路径在data/data/com.itheima.sharedpreference/share_prefs,info生成后是一个.xml文件
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
//拿到sp的编辑器
Editor ed = sp.edit();
ed.putString("name", name);
ed.putString("pass", pass);
//提交
ed.commit();
}
//创建并显示吐司对话框
Toast.makeText(this, "登录成功", 0).show();
}
//**回显数据**
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
String name = sp.getString("name", "");
String pass = sp.getString("pass", "");
et_name.setText(name);
et_pass.setText(pass);
总结:在Android中操作读写文件,基本会用到
/*第一种方式*/FileOutputStream fos=openFileOutput("info.txt",MODE_PRIVATE);
/*第二种方式*/SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);