小米開源檔案管理工具MiCodeFileExplorer-源碼研究(4)-檔案操作工具類FileOperationHelper

來源:互聯網
上載者:User

標籤:檔案管理工具   fileexplorer   小米   開源   android   

檔案操作是非常通用的,注釋都寫在原始碼中了,不多說~
需要特別說明的是,任務的非同步執行和IOperationProgressListener。
拷貝和刪除等操作,是比較費時的,採用了非同步執行的方式~


Android非同步執行,我也是初次瞭解,在CSDN上找了一篇文章,後續寫個單獨的例子,單獨寫1篇介紹。
http://blog.csdn.net/xufenghappy6/article/details/7343899
非同步執行+事件通知 是一種比較流行的模式,比同步等待很多時候要好。


另外,特別需要說明的是,Java應用程式中、Android、Windows開發、Linux Shell都會有檔案File的概念,他們本質是一樣的。
檔案的核心概念基本一致,都是用的作業系統的檔案概念,不同作業系統之間的區別也不大。
建立、刪除、重新命名、複製、粘貼,輸入-執行-輸出,也都一樣。


package net.micode.fileexplorer.util;import java.io.File;import java.io.FilenameFilter;import java.util.ArrayList;import net.micode.fileexplorer.model.FileInfo;import android.os.AsyncTask;import android.os.Environment;import android.text.TextUtils;import android.util.Log;/**檔案操作工具類,執行檔案的建立、移動、粘貼、重新命名、刪除等*/public class FileOperationHelper {private static final String LOG_TAG = "FileOperation";    //內部檔案集合,用來臨時儲存複製、移動等操作,使用者選擇的檔案集合private ArrayList<FileInfo> mCurFileNameList = new ArrayList<FileInfo>();private boolean mMoving;private IOperationProgressListener mOperationListener;private FilenameFilter mFilter;public interface IOperationProgressListener {void onFinish();void onFileChanged(String path);}public FileOperationHelper(IOperationProgressListener l) {mOperationListener = l;}public void setFilenameFilter(FilenameFilter f) {mFilter = f;}//根據路徑和檔案名稱,建立檔案public boolean CreateFolder(String path, String name) {Log.v(LOG_TAG, "CreateFolder >>> " + path + "," + name);File f = new File(Util.makePath(path, name));if (f.exists())return false;return f.mkdir();}//拷貝若干個檔案,把檔案集合拷貝到“當前檔案集合中mCurFileNameList”,可以供“粘貼操作”使用public void Copy(ArrayList<FileInfo> files) {copyFileList(files);}//粘貼,把當前檔案集合中“mCurFileNameList”的檔案,拷貝到目標路徑下public boolean Paste(String path) {if (mCurFileNameList.size() == 0)return false;final String _path = path;//非同步執行某個任務asnycExecute(new Runnable() {@Overridepublic void run() {for (FileInfo f : mCurFileNameList) {CopyFile(f, _path);}//通知操作變化mOperationListener.onFileChanged(Environment.getExternalStorageDirectory().getAbsolutePath());                //粘貼之後,需要清空mCurFileNameListclear();}});return true;}//是否可以“粘貼”,mCurFileNameList有元素public boolean canPaste() {return mCurFileNameList.size() != 0;}//開始移動,標記“正在移動”,拷貝檔案集合public void StartMove(ArrayList<FileInfo> files) {if (mMoving)return;mMoving = true;copyFileList(files);}//移動狀態public boolean isMoveState() {return mMoving;}//能否移動,假設path為“C:/a/b”,f.filePath為“C:、/a/b/c/d.png”,不能移動//TODO 感覺不太靠譜啊,為啥不能移動到檔案的上級目錄呢?public boolean canMove(String path) {for (FileInfo f : mCurFileNameList) {if (!f.IsDir)continue;if (Util.containsPath(f.filePath, path))return false;}return true;}//清空當前檔案集合public void clear() {synchronized (mCurFileNameList) {mCurFileNameList.clear();}}//停止移動,移動檔案是非同步執行,結束後有事件通知public boolean EndMove(String path) {if (!mMoving)return false;mMoving = false;if (TextUtils.isEmpty(path))return false;final String _path = path;asnycExecute(new Runnable() {@Overridepublic void run() {for (FileInfo f : mCurFileNameList) {MoveFile(f, _path);}mOperationListener.onFileChanged(Environment.getExternalStorageDirectory().getAbsolutePath());clear();}});return true;}public ArrayList<FileInfo> getFileList() {return mCurFileNameList;}//非同步執行某個任務//android的類AsyncTask對線程間通訊進行了封裝,提供了簡易的編程方式來使後台線程和UI線程進行通訊:後台線程執行非同步任務,並把操作結果通知UI線程。//可以參考http://blog.csdn.net/xufenghappy6/article/details/7343899private void asnycExecute(Runnable r) {final Runnable _r = r;new AsyncTask() {@Overrideprotected Object doInBackground(Object... params) {synchronized (mCurFileNameList) {_r.run();}if (mOperationListener != null) {mOperationListener.onFinish();}return null;}}.execute();}//某個路徑是否被選中public boolean isFileSelected(String path) {synchronized (mCurFileNameList) {for (FileInfo f : mCurFileNameList) {if (f.filePath.equalsIgnoreCase(path))return true;}}return false;}//檔案重新命名public boolean Rename(FileInfo f, String newName) {if (f == null || newName == null) {Log.e(LOG_TAG, "Rename: null parameter");return false;}File file = new File(f.filePath);String newPath = Util.makePath(Util.getPathFromFilepath(f.filePath),newName);final boolean needScan = file.isFile();try {boolean ret = file.renameTo(new File(newPath));if (ret) {if (needScan) {mOperationListener.onFileChanged(f.filePath);}mOperationListener.onFileChanged(newPath);}return ret;} catch (SecurityException e) {Log.e(LOG_TAG, "Fail to rename file," + e.toString());}return false;}//刪除若干檔案,先copy檔案集合,再非同步執行刪除操作,刪除完成後,有通知public boolean Delete(ArrayList<FileInfo> files) {copyFileList(files);asnycExecute(new Runnable() {@Overridepublic void run() {for (FileInfo f : mCurFileNameList) {DeleteFile(f);}mOperationListener.onFileChanged(Environment.getExternalStorageDirectory().getAbsolutePath());clear();}});return true;}//刪除1個檔案protected void DeleteFile(FileInfo f) {if (f == null) {Log.e(LOG_TAG, "DeleteFile: null parameter");return;}File file = new File(f.filePath);boolean directory = file.isDirectory();if (directory) {for (File child : file.listFiles(mFilter)) {if (Util.isNormalFile(child.getAbsolutePath())) {DeleteFile(Util.GetFileInfo(child, mFilter, true));}}}file.delete();Log.v(LOG_TAG, "DeleteFile >>> " + f.filePath);}//執行1個檔案的拷貝,如果檔案是目錄,拷貝整個目錄,可能有遞迴Copyprivate void CopyFile(FileInfo f, String dest) {if (f == null || dest == null) {Log.e(LOG_TAG, "CopyFile: null parameter");return;}File file = new File(f.filePath);if (file.isDirectory()) {// directory exists in destination, rename itString destPath = Util.makePath(dest, f.fileName);File destFile = new File(destPath);int i = 1;while (destFile.exists()) {destPath = Util.makePath(dest, f.fileName + " " + i++);destFile = new File(destPath);}for (File child : file.listFiles(mFilter)) {if (!child.isHidden()&& Util.isNormalFile(child.getAbsolutePath())) {CopyFile(Util.GetFileInfo(child, mFilter, Settings.instance().getShowDotAndHiddenFiles()), destPath);}}} else {String destFile = Util.copyFile(f.filePath, dest);}Log.v(LOG_TAG, "CopyFile >>> " + f.filePath + "," + dest);}//移動檔案,通過重新命名的方式,移動的private boolean MoveFile(FileInfo f, String dest) {Log.v(LOG_TAG, "MoveFile >>> " + f.filePath + "," + dest);if (f == null || dest == null) {Log.e(LOG_TAG, "CopyFile: null parameter");return false;}File file = new File(f.filePath);String newPath = Util.makePath(dest, f.fileName);try {return file.renameTo(new File(newPath));} catch (SecurityException e) {Log.e(LOG_TAG, "Fail to move file," + e.toString());}return false;}//把檔案集合copy到mCurFileNameList中,同步~private void copyFileList(ArrayList<FileInfo> files) {synchronized (mCurFileNameList) {mCurFileNameList.clear();for (FileInfo f : files) {mCurFileNameList.add(f);}}}}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

小米開源檔案管理工具MiCodeFileExplorer-源碼研究(4)-檔案操作工具類FileOperationHelper

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.