Android中文件與文件夾的創(chuàng)建(file)
常用路徑獲取
在Android開發(fā)過程中铝穷,我們經(jīng)常會對文件系統(tǒng)進(jìn)行操作——存放、釋放我們應(yīng)用的數(shù)據(jù)恋追。Android系統(tǒng)中提供了各種功能的文件目錄凭迹,每個(gè)目錄都有相應(yīng)的特點(diǎn)和功能。
內(nèi)部存儲(Internal Storage)
(1)內(nèi)部存儲
內(nèi)部存儲是App的私有目錄苦囱,當(dāng)一個(gè)應(yīng)用卸載之后嗅绸,內(nèi)部存儲中的這些文件也被刪除。Shared Preferences和SQLite數(shù)據(jù)庫文件都是存儲在內(nèi)部存儲空間上的撕彤。
//路徑:(data/data/應(yīng)用包名/files)
context.getFilesDir();
Log.e(TAG,getApplicationContext().getFilesDir().getAbsolutePath());
不過經(jīng)實(shí)際測試(華為鱼鸠、小米手機(jī)等),getFileDir實(shí)際路徑為: /data/user/0/ 應(yīng)用包名/files
(2)緩存目錄
//路徑:(data/data/應(yīng)用包名/cache),
context.getCacheDir();
Log.e(TAG,getApplicationContext().getCacheDir().getAbsolutePath());
應(yīng)用程序的緩存目錄喉刘,該目錄內(nèi)的文件在設(shè)備內(nèi)存不足時(shí)會優(yōu)先被刪除掉瞧柔,所以存放在這里的文件是沒有任何保障的,可能會隨時(shí)丟掉睦裳。
不過經(jīng)實(shí)際測試(華為造锅、小米手機(jī)等),getCacheDir的手機(jī)路徑為: /data//data/user/0/應(yīng)用包名/cache
外部存儲(External Storage)
文件是可以被自由訪問廉邑,且文件的數(shù)據(jù)對其他應(yīng)用或者用戶來說都是有意義的哥蔚,當(dāng)應(yīng)用被卸載之后,其卸載前創(chuàng)建的文件仍然保留蛛蒙。
(1)公共文件(目錄)
//SD卡的根目錄糙箍,路徑:/storage/emulated/0
Environment.getExternalStorageDirectory()
Log.e(TAG, Environment.getExternalStorageDirectory().getAbsolutePath());
由于是外部存儲的原因即使是這種類型的文件也能被其他程序訪問,只不過一個(gè)應(yīng)用私有的文件對其他應(yīng)用其實(shí)是沒有訪問價(jià)值的(惡意程序除外)牵祟。
(2)私有文件(目錄)
外部存儲上深夯,應(yīng)用私有文件的價(jià)值在于卸載之后,這些文件也會被刪除诺苹。類似于內(nèi)部存儲咕晋。
//路徑:/storage/emulated/0/Android/data/應(yīng)用包名/files
getApplicationContext().getExternalFilesDir("") ;
Log.e(TAG, getApplicationContext().getExternalFilesDir("").getAbsolutePath());
擴(kuò)展: getExternalFilesDir是手機(jī)中設(shè)置 → 應(yīng)用 → 具體應(yīng)用詳情→ 清除數(shù)據(jù) 的操作對象
//路徑:/storage/emulated/0/Android/data/應(yīng)用包名/cache
getApplicationContext().getExternalCacheDir() ;
Log.e(TAG,getApplicationContext().getExternalCacheDir().getAbsolutePath());
擴(kuò)展: getExternalCacheDir是手機(jī)中設(shè)置 → 應(yīng)用 → 具體應(yīng)用詳情→ 清除緩存的操作對象
文件相關(guān)操作
文件夾的創(chuàng)建和刪除
文件的創(chuàng)建和刪除
在demo中有簡單的示例
demo
布局很簡單就不貼出來了
相關(guān)代碼
public class FileActivity extends BaseActivity {
private TextView textView;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private Button btn10;
@Override
public int getLayoutId() {
return R.layout.home_activity_file;
}
@Override
public void initData() {
}
@Override
public void initView() {
textView = (TextView) findViewById(R.id.textView);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btn10 = (Button) findViewById(R.id.btn10);
}
@Override
public void initListener() {
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btn10.setOnClickListener(this);
}
@Override
public void viewsClick(View view) {
int id = view.getId();
if (id == R.id.btn1) {
//判斷SD卡是否插入
if (Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
textView.setText("true");
} else {
textView.setText("false");
}
} else if (id == R.id.btn2) {
//內(nèi)部存儲目錄
File file = getApplicationContext().getFilesDir();
textView.setText(file.getPath());
} else if (id == R.id.btn3) {
//內(nèi)部緩存目錄
File file = getApplicationContext().getCacheDir();
textView.setText(file.getPath());
} else if (id == R.id.btn4) {
//SD卡的根目錄
File file = Environment.getExternalStorageDirectory();
textView.setText(file.getPath());
} else if (id == R.id.btn5) {
//外部存儲目錄
File file = getApplicationContext().getExternalFilesDir("");
textView.setText(file.getPath());
} else if (id == R.id.btn6) {
//外部緩存目錄
File file = getApplicationContext().getExternalCacheDir();
textView.setText(file.getPath());
} else if (id == R.id.btn7) {
//創(chuàng)建文件夾
createFolder(getApplicationContext().getExternalFilesDir(""), "image");
} else if (id == R.id.btn8) {
//刪除文件夾
deleteFolder(getApplicationContext().getExternalFilesDir(""), "image");
} else if (id == R.id.btn9) {
//在image文件夾中創(chuàng)建文件,如果沒有則創(chuàng)建文件夾并且創(chuàng)建文件
createFile(getApplicationContext().getExternalFilesDir("image"), "text" + ".xml");
} else if (id == R.id.btn10) {
//刪除文件
deleteFile(getApplicationContext().getExternalFilesDir("image"), "text" + ".xml");
}
}
/**
* 創(chuàng)建文件夾
*
* @param path 路徑
* @param name 文件夾名
*/
public void createFolder(File path, String name) {
File Folder = new File(path + "/" + name);
if (!Folder.exists())//判斷文件夾是否存在,不存在則創(chuàng)建文件夾收奔,已經(jīng)存在則跳過
{
Folder.mkdir();//創(chuàng)建文件夾
} else {
Log.i("", "文件夾已存在");
}
}
/**
* 刪除文件夾
*
* @param path 路徑
* @param name 文件夾名
*/
public void deleteFolder(File path, String name) {
File Folder = new File(path + "/" + name);
if (Folder.exists())//判斷文件夾是否存在
Folder.delete();
}
/**
* 創(chuàng)建文件
*
* @param path 路徑
* @param name 文件名,帶格式
*/
public void createFile(File path, String name) {
//新建一個(gè)File類型的成員變量掌呜,傳入文件名路徑。
File file = new File(path + "/" + name);
//判斷文件是否存在坪哄,存在就刪除
if (file.exists()) {
file.delete();
}
try {
//創(chuàng)建文件
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
Log.e("creatXMLFileException", e.getMessage());
}
}
/**
* 刪除文件
*
* @param path 路徑
* @param name 文件名,帶格式
*/
public void deleteFile(File path, String name) {
File file = new File(path + "/" + name);
//判斷文件是否存在质蕉,存在就刪除
if (file.exists()) {
file.delete();
}
}
}
不要在忘記在AndroidManifest.xml中添加權(quán)限
<!-- 允許在外部存儲器即SD卡上添加或刪除系統(tǒng)文件-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 允許在外部存儲器即SD卡上寫數(shù)據(jù)-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 允許在外部存儲器即SD卡上讀數(shù)據(jù)-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />