Android 關於檔案及資料夾的建立 、刪除、重新命名、複製拷貝、新增內容、修改內容、讀取內容
阿新 • • 發佈:2019-02-02
這幾天做了一個Android關於檔案及資料夾相關操作的軟體,自己就總結寫了一個關於檔案建立、遍歷、刪除、重新命名、複製拷貝、新增內容、修改內容、讀取內容的工具類,方便以後快速的對檔案進行一些簡單操作,使用時可直接複製到程式碼中作為一個工具類。
/** * author : smile * Created by PC on 2017/5/10. */ public class FileUtils { private static final String TAG = "FileUtils"; /** * 建立檔案 * * @param filePath 檔案地址 * @param fileName 檔名 * @return */ public static boolean createFile(String filePath, String fileName) { String strFilePath = filePath + fileName; File file = new File(filePath); if (!file.exists()) { /** 注意這裡是 mkdirs()方法 可以建立多個資料夾 */ file.mkdirs(); } File subfile = new File(strFilePath); if (!subfile.exists()) { try { boolean b = subfile.createNewFile(); return b; } catch (IOException e) { e.printStackTrace(); } } else { return true; } return false; } /** * 遍歷資料夾下的檔案 * * @param file 地址 */ public static List<File> getFile(File file) { List<File> list = new ArrayList<>(); File[] fileArray = file.listFiles(); if (fileArray == null) { return null; } else { for (File f : fileArray) { if (f.isFile()) { list.add(0, f); } else { getFile(f); } } } return list; } /** * 刪除檔案 * * @param filePath 檔案地址 * @return */ public static boolean deleteFiles(String filePath) { List<File> files = getFile(new File(filePath)); if (files.size() != 0) { for (int i = 0; i < files.size(); i++) { File file = files.get(i); /** 如果是檔案則刪除 如果都刪除可不必判斷 */ if (file.isFile()) { file.delete(); } } } return true; } /** * 向檔案中新增內容 * * @param strcontent 內容 * @param filePath 地址 * @param fileName 檔名 */ public static void writeToFile(String strcontent, String filePath, String fileName) { //生成資料夾之後,再生成檔案,不然會出錯 String strFilePath = filePath + fileName; // 每次寫入時,都換行寫 File subfile = new File(strFilePath); RandomAccessFile raf = null; try { /** 建構函式 第二個是讀寫方式 */ raf = new RandomAccessFile(subfile, "rw"); /** 將記錄指標移動到該檔案的最後 */ raf.seek(subfile.length()); /** 向檔案末尾追加內容 */ raf.write(strcontent.getBytes()); raf.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 修改檔案內容(覆蓋或者新增) * * @param path 檔案地址 * @param content 覆蓋內容 * @param append 指定了寫入的方式,是覆蓋寫還是追加寫(true=追加)(false=覆蓋) */ public static void modifyFile(String path, String content, boolean append) { try { FileWriter fileWriter = new FileWriter(path, append); BufferedWriter writer = new BufferedWriter(fileWriter); writer.append(content); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 讀取檔案內容 * * @param filePath 地址 * @param filename 名稱 * @return 返回內容 */ public static String getString(String filePath, String filename) { FileInputStream inputStream = null; try { inputStream = new FileInputStream(new File(filePath + filename)); } catch (FileNotFoundException e) { e.printStackTrace(); } InputStreamReader inputStreamReader = null; try { inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } BufferedReader reader = new BufferedReader(inputStreamReader); StringBuffer sb = new StringBuffer(""); String line; try { while ((line = reader.readLine()) != null) { sb.append(line); sb.append("\n"); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } /** * 重新命名檔案 * * @param oldPath 原來的檔案地址 * @param newPath 新的檔案地址 */ public static void renameFile(String oldPath, String newPath) { File oleFile = new File(oldPath); File newFile = new File(newPath); //執行重新命名 oleFile.renameTo(newFile); } /** * 複製檔案 * * @param fromFile 要複製的檔案目錄 * @param toFile 要貼上的檔案目錄 * @return 是否複製成功 */ public static boolean copy(String fromFile, String toFile) { //要複製的檔案目錄 File[] currentFiles; File root = new File(fromFile); //如同判斷SD卡是否存在或者檔案是否存在 //如果不存在則 return出去 if (!root.exists()) { return false; } //如果存在則獲取當前目錄下的全部檔案 填充陣列 currentFiles = root.listFiles(); //目標目錄 File targetDir = new File(toFile); //建立目錄 if (!targetDir.exists()) { targetDir.mkdirs(); } //遍歷要複製該目錄下的全部檔案 for (int i = 0; i < currentFiles.length; i++) { if (currentFiles[i].isDirectory())//如果當前項為子目錄 進行遞迴 { copy(currentFiles[i].getPath() + "/", toFile + currentFiles[i].getName() + "/"); } else//如果當前項為檔案則進行檔案拷貝 { CopySdcardFile(currentFiles[i].getPath(), toFile + currentFiles[i].getName()); } } return true; } //檔案拷貝 //要複製的目錄下的所有非子目錄(資料夾)檔案拷貝 public static boolean CopySdcardFile(String fromFile, String 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 true; } catch (Exception ex) { return false; } } }
對檔案操作不要忘記新增下面這個許可權:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
還有就是關於檔案路徑的一些知識點,也寫在這篇部落格裡面吧。
String path=Environment.getExternalStorageDirectory().getPath();
這個path一般都是SD卡下的根目錄。
String path1=getFilesDir().getPath();
這個path一般是 /data/data/包名/files 。
以上就是自己總結的關於檔案的一些簡單操作,樓主以後用到什麼還會繼續往上面新增。