用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);