Android 檔案操作工具類

來源:互聯網
上載者:User

Android 檔案操作工具類

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;//使用者資料讀寫函數//存在SD卡時資料從SD卡讀寫不存在時通過DATA目錄讀寫public class 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/";}}/** * 在SD卡上建立檔案 */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); //刪除完裡面所有內容                String filePath = folderPath;                filePath = filePath.toString();                java.io.File myFilePath = new java.io.File(filePath);                myFilePath.delete(); //刪除空檔案夾       }        catch (Exception e) {                System.out.println("刪除檔案夾操作出錯");                e.printStackTrace();       }}/** * 刪除檔案夾裡面的所有檔案 * @param path String 檔案夾路徑 如 c:/fqf */public void delAllFile(String path) {        File file = new File(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]);//先刪除檔案夾裡面的檔案                        delFolder(path+"/"+ tempList[i]);//再刪除空檔案夾                }        }} /** * 在SD卡上建立目錄 */public File creatSDDir(String dirName){File dir = new File(SDPATH + dirName);dir.mkdir();return dir;}/** * 判斷SD卡上的檔案夾是否存在 */public boolean isFileExist(String fileName){File file = new File(SDPATH + fileName);return file.exists();}public long file_time(String fileName){File file = new File(SDPATH + fileName);return file.lastModified();}/** * 將一個InputStream裡面的資料寫入到SD卡中 */public File write2SDFromInput(String path, String fileName,InputStream input){File file = null;OutputStream output = null;try{creatSDDir(path);file = creatSDFile(path + fileName);output = new FileOutputStream(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()) { // 判斷檔案是否存在if (file.isFile()) { // 判斷是否是檔案file.delete(); // delete()方法 你應該知道 是刪除的意思;}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);OutputStream fosto = new FileOutputStream(toFile);byte bt[] = new byte[1024];int c;while ((c = fosfrom.read(bt)) > 0) {fosto.write(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;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.