Tag:android Technical file operation
Package Cn.ibabyzone.library;import Java.io.file;import Java.io.fileinputstream;import java.io.FileOutputStream; Import Java.io.ioexception;import java.io.inputstream;import java.io.outputstream;import android.app.Activity; Import android.os.environment;import android.util.log;//User data read/write function//when there is an SD card, the data is read and written to the public class from the SD card when it is not available. Fileutils{private string sdpath;private int FILESIZE = 1;public string Getsdpath () {return sdpath;} Public FileUtils (Activity thisactivity) {if (Android.os.Environment.getExternalStorageState (). Equals ( Android.os.Environment.MEDIA_MOUNTED)) {Sdpath = Environment.getexternalstoragedirectory () + "/ibabyzone/";} Else{sdpath= Thisactivity.getfilesdir (). toString () + "/ibabyzone/";}} /** * Create files on SD card */public file Creatsdfile (String fileName) throws Ioexception{file file = new file (Sdpath + fileName); file . CreateNewFile (); return file;} public void Delfolder (String folderpath) {try {delallfile (folderpath);//Delete all contents String FilePath = FolderPath; FilePath = Filepath.tostring (); Java.io.File Myfilepath = new Java.io.File (FilePath); Myfilepath.delete (); Delete empty folder} catch (Exception e) {System.out.println ("Error deleting folder operation"); E.printstacktrace (); }}/** * Delete all files in folder * @param path String folder path such as C:/fqf */public void Delallfile (String path) {File File = new Fi Le (path); if (!file.exists ()) {return; } if (!file.isdirectory ()) {return; } string[] Templist = File.list (); File temp = null; for (int i = 0; i < templist.length; i++) {if (Path.endswith (File.separator)) { temp = new File (path + templist[i]); } else {temp = new File (path + file.separator + templist[i]); } if (Temp.isfile ()) {temp.delete (); } if (Temp.isdirectory ()) {Delallfile (path+ "/" + templist[i]);//delete the files in the folder first Delfolder (path+ "/" + templist[i]);//delete Empty Folder}}}/** * Create directory on SD card */public File Creatsddir (String dirName) {File dir = new file (Sdpath + dirName);d ir.mkdir (); return dir;} /** * Determine if the folder on the SD card exists */public Boolean isfileexist (String fileName) {File File = new file (Sdpath + fileName); return File.exi STS ();} Public long File_time (String fileName) {File File = new file (Sdpath + fileName); return file.lastmodified ();} /** * Writes data from a inputstream to an SD card */public file Write2sdfrominput (string path, string Filename,inputstream input) {file File = Null;outputstream output = null;try{creatsddir (path); file = creatsdfile (path + fileName); output = new Fileoutputstr EAM (file); byte[] buffer = new Byte[filesize];while ((input.read (buffer))! =-1) {output.write (buffer);} Output.flush ();} catch (Exception e) {e.printstacktrace ();} Finally{try{output.close ();} catch (ExceptiOn e) {e.printstacktrace ();}} return file;} public static void DeleteFile (String filestr) {File File = new file (FILESTR), if (File.exists ()) {//To determine if the file exists if (FILE.ISFI Le ()) {//To determine if it is a file file.delete ();//Delete () method you should know the meaning of the deletion;} File.delete ();}} public void Delfile (string path) {string str = Sdpath+path; File File = new file (str); if (File.exists ()) {File.delete ();}} public int Copyfiletofile (file fromfile, file ToFile) {try {InputStream fosfrom = new FileInputStream (fromfile); outputstr EAM Fosto = new FileOutputStream (tofile), byte bt[] = new Byte[1024];int c;while ((c = Fosfrom.read (BT)) > 0) {fosto.wri Te (BT, 0, C);} Fosfrom.close (); Fosto.close (); return 0;} catch (Exception ex) {return-1;}} Public File copyfileromtosd (String romfile) {if (!android.os.environment.getexternalstoragestate (). Equals ( Android.os.Environment.MEDIA_MOUNTED)) {return null;} File ROM = new file (romfile); File SD = new file (environment.getexternalstoragedirectory () + "/ibabyzone/" +romfile), if (Copyfiletofile (rom,sd) = = 0) { RetuRN SD;} return null;}}
Android File Manipulation Tool class