項目中很多地方用到了文件IO操作,
最好對這些常用的操作進(jìn)行統(tǒng)一封裝,而非讓文件IO代碼散落在項目各模塊代碼中。
創(chuàng)建文件
需求:
-- 在指定目錄創(chuàng)建文件,
-- 先判斷目錄是否存在,如果不存在,則遞歸創(chuàng)建,
-- 再判斷文件是否存在,如果不存在,則創(chuàng)建
-- 最后,返回文件對象
方法傳參:目標(biāo)路徑、文件名稱
public File newFile(String filePath, String fileName){
if(filePath == null || filePath.length() == 0
|| fileName == null || fileName.length() == 0){
return null;
}
try {
//判斷目錄是否存在,如果不存在,遞歸創(chuàng)建目錄
File dir = new File(filePath);
if(!dir.exists()){
dir.mkdirs();
}
//組織文件路徑
StringBuilder sbFile = new StringBuilder(filePath);
if(!filePath.endsWith("/")){
sbFile.append("/");
}
sbFile.append(fileName);
//創(chuàng)建文件并返回文件對象
File file = new File(sbFile.toString());
if(!file.exists()){
file.createNewFile();
}
return file;
}catch (Exception ex){
ex.printStackTrace();
}
return null;
}
刪除文件或目錄
需求:
-- 可刪除單個文件,也可刪除整個目錄,
-- 先判斷File類型是文件還是目錄類型,
-- 如果是文件,則直接刪除
-- 如果是目錄,則獲得目錄信息,遞歸刪除文件及文件夾
方法傳參:目標(biāo)路徑(文件或目錄路徑)
public void removeFile(String filePath) {
if(filePath == null || filePath.length() == 0){
return;
}
try {
File file = new File(filePath);
if(file.exists()){
removeFile(file);
}
}catch (Exception ex){
ex.printStackTrace();
}
}
private void removeFile(File file){
//如果是文件直接刪除
if(file.isFile()){
file.delete();
return;
}
//如果是目錄,遞歸判斷,如果是空目錄,直接刪除,如果是文件,遍歷刪除
if(file.isDirectory()){
File[] childFile = file.listFiles();
if(childFile == null || childFile.length == 0){
file.delete();
return;
}
for(File f : childFile){
removeFile(f);
}
file.delete();
}
}
獲得文件或目錄大?。╯ize)
需求:
-- 可獲得單個文件大小,也可獲得目錄大小,
-- 先判斷File類型是文件還是目錄類型,
-- 如果是文件,則直接獲取大小并返回
-- 如果是目錄,遞歸獲得各文件大小累加,然后返回
方法傳參:目標(biāo)路徑(文件或目錄路徑)
float size = 0;
public float getFileSize(String filePath) {
if(filePath == null || filePath.length() == 0){
return 0;
}
try {
File file = new File(filePath);
if(file.exists()){
size = 0;
return getSize(file);
}
}catch (Exception ex){
ex.printStackTrace();
}
return 0;
}
private float getSize(File file) {
try {
//如果是目錄則遞歸計算其內(nèi)容的總大小
if (file.isDirectory()) {
File[] children = file.listFiles();
for (File f : children) {
size += getSize(f);
}
return size;
}
//如果是文件則直接返回其大小
else {
return (float) file.length();
}
}catch (Exception ex){
ex.printStackTrace();
}
return size;
}
拷貝文件
需求:
-- 將文件拷貝到指定目錄
-- 如果目錄不存在,則遞歸創(chuàng)建
-- 拷貝文件
方法傳參:文件路徑、拷貝目標(biāo)路徑
public void copyFile(String filePath, String newDirPath) {
if(filePath == null || filePath.length() == 0){
return;
}
try {
File file = new File(filePath);
if(!file.exists()){
return;
}
//判斷目錄是否存在,如果不存在,則創(chuàng)建
File newDir = new File(newDirPath);
if(!newDir.exists()){
newDir.mkdirs();
}
//創(chuàng)建目標(biāo)文件
File newFile = newFile(context, newDirPath, file.getName());
InputStream is = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] buffer = new byte[4096];
int byteCount = 0;
while ((byteCount = is.read(buffer)) != -1) {
fos.write(buffer, 0, byteCount);
}
fos.flush();
is.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
拷貝目錄
需求:
-- 將整個目錄拷貝到指定目錄
-- 如果目錄不存在,則遞歸創(chuàng)建
-- 遞歸拷貝文件
方法傳參:源目錄路徑、拷貝目標(biāo)路徑
public void copyDir(String dirPath, String newDirPath) {
if(dirPath == null || dirPath.length() == 0
|| newDirPath==null || newDirPath.length() == 0){
return;
}
try {
File file = new File(dirPath);
if(!file.exists() && !file.isDirectory()){
return;
}
File[] childFile = file.listFiles();
if(childFile == null || childFile.length == 0){
return;
}
File newFile = new File(newDirPath);
newFile.mkdirs();
for (File fileTemp : childFile) {
if(fileTemp.isDirectory()){
copyDir(fileTemp.getPath(), newDirPath + "/" + fileTemp.getName());
}else {
copyFile(fileTemp.getPath(), newDirPath);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
剪切文件
需求:
-- 將文件剪切到指定目錄
-- 如果目錄不存在,則遞歸創(chuàng)建
-- 拷貝文件,刪除源文件
方法傳參:源文件路徑、拷貝目標(biāo)路徑
public void moveFile(String filePath, String newDirPath) {
if(filePath == null || filePath.length() == 0
|| newDirPath==null || newDirPath.length() == 0){
return;
}
try {
//拷貝文件
copyFile(filePath, newDirPath);
//刪除原文件
removeFile(filePath);
}catch (Exception e) {
e.printStackTrace();
}
}
剪切目錄
需求:
-- 將目錄剪切到指定目錄
-- 如果目錄不存在,則遞歸創(chuàng)建
-- 遞歸拷貝文件,刪除源目錄
方法傳參:源目錄路徑、拷貝目標(biāo)路徑
public void moveDir(String dirPath, String newDirPath) {
if(dirPath == null || dirPath.length() == 0
|| newDirPath==null || newDirPath.length() == 0){
return;
}
try {
//拷貝目錄
copyDir(dirPath, newDirPath);
//刪除目錄
removeFile(dirPath);
}catch (Exception e) {
e.printStackTrace();
}
}
其他操作
文件路徑的分類使用,請參考http://www.itdecent.cn/p/0515df3b697d
以上代碼經(jīng)過測試,如果調(diào)用還有問題,請檢查APP權(quán)限或自行優(yōu)化。