import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class FileUtils { private static LogUtil log = LogUtil.get(FileUtils.class.getSimpleName()); /** * 拷贝文件夹 * * @param srcDir 源文件夹 * @param destDir 目标文件夹 * @throws IOException 读写异常 */ public static void copyDirectory(File srcDir, File destDir) { if (srcDir == null || destDir == null) { return; } log.i("copyDirectory start srcDir=" + srcDir.getAbsolutePath() + " destDir=" + destDir.getAbsolutePath()); if (!srcDir.exists()) { log.i("copyDirectory IllegalArgumentException=Source directory does not exist."); } if (!destDir.exists() && !destDir.mkdir()) { log.i("copyDirectory IllegalArgumentException=Destination directory cannot be created."); createSeparator(destDir.getAbsolutePath()); } if (!srcDir.isDirectory()) { log.i("copyDirectory IllegalArgumentException=Source is not a directory."); } deleteDirectory(destDir); //遍历源文件夹数据 File[] files = srcDir.listFiles(); if (files != null && files.length != 0) { for (File file : files) { if (file.isDirectory()) { // 如果是文件夹重复调用 copyDirectory(file, new File(destDir, file.getName())); } else { // 是文件执行拷贝操作 FileInputStream inStream = null; FileOutputStream outStream = null; try { log.e("copyDirectory file =" + file.getAbsolutePath()); inStream = new FileInputStream(file); outStream = new FileOutputStream(new File(destDir, file.getName())); byte[] buffer = new byte[1024]; int length; while ((length = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, length); } log.e("copyDirectory end"); } catch (Exception e) { log.e("copyDirectory e=" + e.getMessage()); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { log.e("copyDirectory IOException1=" + e.getMessage()); } } if (outStream != null) { try { outStream.close(); } catch (IOException e) { log.e("copyDirectory IOException2=" + e.getMessage()); } } } } } log.d("copyDirectory end"); } else { log.d("copyDirectory files is null srcDir=" + srcDir.getAbsolutePath()); } } /** * 拷贝文件 * * @param srcFile 源文件 * @param destFile 目标文件 * @throws IOException 读写异常 */ public static void copyFile(File srcFile, File destFile) { if (srcFile == null || destFile == null) { return; } log.i("copyFile start srcFile=" + srcFile.getAbsolutePath() + " destFile=" + destFile.getAbsolutePath()); // 是文件执行拷贝操作 FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(srcFile); outStream = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int length; while ((length = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, length); } log.e("copyFile end"); } catch (Exception e) { log.e("copyFile e=" + e.getMessage()); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { log.e("copyFile IOException1=" + e.getMessage()); } } if (outStream != null) { try { outStream.close(); } catch (IOException e) { log.e("copyFile IOException2=" + e.getMessage()); } } } } /** * 读取文件内容 txt等 */ public static void readContent(String filePath) { BufferedReader buffReader = null; InputStream inStream = null; InputStreamReader inputReader = null; StringBuilder content = new StringBuilder(); try { inStream = new FileInputStream(filePath); if (inStream != null) { inputReader = new InputStreamReader(inStream, "UTF-8"); buffReader = new BufferedReader(inputReader); String line = buffReader.readLine(); //分行读取 while (line != null) { content.append(line + " "); line = buffReader.readLine(); } log.e("readContent content=" + content); inStream.close(); //关闭输入流 } } catch (FileNotFoundException e) { log.e("readContent e1=" + e.getMessage()); } catch (IOException e) { log.e("readContent e2=" + e.getMessage()); } finally { try { if (inStream != null) { inStream.close(); log.i("readContent---inStream close"); } } catch (IOException e) { log.e("readContent---inStream close e=" + e.getMessage()); } try { if (inputReader != null) { inputReader.close(); log.i("inputReader---inStream close"); } } catch (IOException e) { log.e("readContent---inputReader close e=" + e.getMessage()); } try { if (buffReader != null) { buffReader.close(); log.i("readContent---buffReader close"); } } catch (IOException e) { log.e("readContent---buffReader close e=" + e.getMessage()); } } } /** * 创建 */ public static String createSeparator(String path) { File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } if (path.endsWith("/")) { return path; } return path + '/'; } /** * 删除文件夹内容 */ public static void deleteDirectory(File directory) { if (directory.exists()) { File[] files = directory.listFiles(); if (null != files) { for (File file : files) { if (file.isDirectory()) { deleteDirectory(file); } else { file.delete(); } } } } } }
Android拷贝文件夹和文件方法
于 2024-06-18 13:48:05 首次发布