File operation is very common, the comments are written in the source code, not much to say ~
In particular, the asynchronous execution and ioperationprogresslistener of tasks are required.
Copy and delete operations, it is time-consuming, the use of asynchronous execution of the way ~
Android asynchronous execution, I was also the first to understand, in Csdn found an article, follow-up write a separate example, write 1 separate introduction.
http://blog.csdn.net/xufenghappy6/article/details/7343899
Asynchronous execution + Event notification is a popular pattern that is better than waiting many times for synchronization.
In addition, it is particularly necessary to note that Java applications, Android, Windows development, Linux Shell will have the concept of file files, they are essentially the same.
The core concept of the document is basically the same, it is the file concept of the operating system, and the difference between different operating systems is small.
Create, delete, rename, copy, paste, input-execute-output, all the same.
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;/** file Operation tool class, perform file creation, move, paste, rename, delete */public class Fileoperationhelper {private static final String Log_tag = "FileOperation"; Internal file collection, used to temporarily save the copy, move, and other operations, the user selected file collection private arraylist<fileinfo> mcurfilenamelist = new arraylist<fileinfo> ();p rivate 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;} Create File public boolean CreateFolder (string path, string name) {LOG.V (Log_tag, "createfolder >>>" + path +) based on path and file name "," + name); FiLe f = new File (Util.makepath (path, name)), if (F.exists ()) return False;return F.mkdir ();} Copy several files, copy the collection of files to "Mcurfilenamelist" in "Current file collection", and use public void copy (arraylist<fileinfo> files) for "paste operation" { Copyfilelist (files);} Paste, copy the file of "Mcurfilenamelist" in the current file collection to the target path under public boolean Paste (String path) {if (mcurfilenamelist.size () = = 0) return false;final String _path = path;//Asynchronously executes a task Asnycexecute (new Runnable () {@Overridepublic void Run () {for (FileInfo F:mcurf Ilenamelist) {CopyFile (f, _path);} Notification operation changes moperationlistener.onfilechanged (Environment.getexternalstoragedirectory (). GetAbsolutePath ()); After pasting, you need to empty mcurfilenamelistclear ();}); return true;} Whether you can "paste", Mcurfilenamelist has element public boolean canPaste () {return mcurfilenamelist.size ()! = 0;} Start moving, Mark "moving", copy file Collection public void Startmove (arraylist<fileinfo> files) {if (mmoving) return;mmoving = true; Copyfilelist (files);} Move state public Boolean ismovestate () {return mmoving;} Can move, assuming that path is "c:/a/b", F.filepath "C:,/a/b/c/d.png", cannot move//todO don't feel so good, why can't you move to the parent directory of the file? public boolean Canmove (String path) {for (FileInfo f:mcurfilenamelist) {if (!f.isdir) continue;if (Util.containspath (f.f Ilepath, Path)) return false;} return true;} Empties the current file collection public void Clear () {synchronized (mcurfilenamelist) {mcurfilenamelist.clear ();}} Stop moving, the move file is executed asynchronously, and after the end there is an event notification 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;} Asynchronous execution of a task//android class Asynctask wraps the inter-thread communication, providing a simple programmatic way to communicate with the background thread and the UI thread: The background thread executes the asynchronous task and notifies the UI thread of the result of the operation. You can refer to 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 ();} Whether a path is selected public boolean isfileselected (String path) {synchronized (mcurfilenamelist) {FileInfo f: Mcurfilenamelist) {if (F.filepath.equalsignorecase (path)) return true;}} return false;} File Rename 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;} Delete a number of files, copy the collection of files, and then perform the delete operation asynchronously, after the deletion is completed, there is a notification 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;} Delete 1 files 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 (m Filter) {if (Util.isnormalfile (Child.getabsolutepath ())) {DeleteFile (Util.getfileinfo (Child, Mfilter, True));}}} File.delete (); LOG.V (Log_tag, "DeleteFile >>>" + F.filepath);} Perform a copy of 1 files, if the file is a directory, copy the entire directory, there may be a recursive 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++);d estfile = 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);} Move the file, by renaming it, to move the private Boolean MoveFile (FileInfo F, String dest) {log.v (Log_tag, "MoveFile >>>" + F.filepat H + "," + 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;} Put the file collection COpy to Mcurfilenamelist, synchronize ~private void Copyfilelist (arraylist<fileinfo> files) {synchronized (mcurfilenamelist {mcurfilenamelist.clear (); for (FileInfo f:files) {mcurfilenamelist.add (f);}}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.