private void copyApkAndFiles() { File destDir = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath(), "Backup"); if (!destDir.exists()) { destDir.mkdirs(); } copyFolder(getActivity().getFilesDir().getParentFile().getAbsolutePath(), destDir.getAbsolutePath()); } /** * 复制文件夹及其中的文件 * * @param oldPath String 源文件夹路径 * @param newPath String 目的文件夹路径 */ public boolean copyFolder(String oldPath, String newPath) { try { File newFile = new File(newPath); if (!newFile.exists()) { if (!newFile.mkdirs()) { return false; } } File oldFile = new File(oldPath); String[] files = oldFile.list(); File temp; for (String file : files) { if (oldPath.endsWith(File.separator)) { temp = new File(oldPath + file); } else { temp = new File(oldPath + File.separator + file); } if (temp.isDirectory()) { copyFolder(oldPath + File.separator + file, newPath + File.separator + file); } else if (!temp.exists()) { return false; } else if (!temp.isFile()) { return false; } else if (!temp.canRead()) { return false; } else { FileInputStream fileInputStream = new FileInputStream(temp); FileOutputStream fileOutputStream = new FileOutputStream(newPath + File.separator + temp.getName()); byte[] buffer = new byte[1024]; int byteRead; while ((byteRead = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, byteRead); } fileInputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); } } return true; } catch (Exception e) { e.printStackTrace(); return false; } }
Android将私有目录下的文件拷贝至SD卡目录
最新推荐文章于 2023-06-05 18:21:43 发布