常见文件操作代码(文件管理软件)
发布日期:2021-06-30 18:38:48
浏览次数:3
分类:技术文章
本文共 4293 字,大约阅读时间需要 14 分钟。
private static String ANDROID_SECURE = "/mnt/sdcard/.android_secure"; /** * 复制文件 * * @param file * 要复制的文件(可能是文件夹) * @param dest * 要复制到的地点 */ public static void CopyFile(File file, String dest) { if (file == null || dest == null) { return; } if (file.isDirectory()) { // directory exists in destination, rename it String destPath = makePath(dest, file.getName()); File destFile = new File(destPath); int i = 1; while (destFile.exists()) { destPath = makePath(dest, file.getName() + " " + i++); destFile = new File(destPath); } File[] listFiles = file.listFiles(); // 空文件夹 if (listFiles.length == 0) { destFile.mkdir(); } for (File child : listFiles) { if (!child.isHidden() && isNormalFile(child.getAbsolutePath())) { CopyFile(child, destPath); } } } else { String destFile = reallyCopyFile(file.getAbsolutePath(), dest); } } /** * 剪切文件 * * @param file * 要移动的文件或文件夹 * @param dest * 目的地址 * @return */ public static boolean MoveFile(File file, String dest) { if (file == null || dest == null) { LogUtils.e("CopyFile: null parameter"); return false; } String destPath = makePath(dest, file.getName()); File destFile = new File(destPath); int i = 1; while (destFile.exists()) { destPath = makePath(dest, getNameFromFilename(file.getName()) + " " + (i++) + "." + getExtFromFilename(file.getName())); destFile = new File(destPath); } try { return file.renameTo(new File(destPath)); } catch (SecurityException e) { LogUtils.e("Fail to move file," + e.toString()); } return false; } /** * 删除文件 * * @param file */ public static void DeleteFile(File file) { if (file == null) { return; } boolean directory = file.isDirectory(); if (directory) { for (File child : file.listFiles()) { if (isNormalFile(child.getAbsolutePath())) { DeleteFile(child); } } } file.delete(); } /** * 判断是不是正常的文件 * * @param fullName * @return */ public static boolean isNormalFile(String fullName) { return !fullName.equals(ANDROID_SECURE); } /** * 得到新的路径 * * @param path1 * 目的地址 * @param path2 * 文件或文件夹的名字 * @return */ public static String makePath(String path1, String path2) { // 如果是以/结尾 if (path1.endsWith(File.separator)) return path1 + path2; return path1 + File.separator + path2; } /** * 真正的复制文件的操作 * * @param src * @param dest * @return */ private static String reallyCopyFile(String src, String dest) { File file = new File(src); if (!file.exists() || file.isDirectory()) { return null; } FileInputStream fi = null; FileOutputStream fo = null; try { fi = new FileInputStream(file); File destPlace = new File(dest); if (!destPlace.exists()) { if (!destPlace.mkdirs()) return null; } String destPath = makePath(dest, file.getName()); File destFile = new File(destPath); int i = 1; while (destFile.exists()) { String destName = getNameFromFilename(file.getName()) + " " + i++ + "." + getExtFromFilename(file.getName()); destPath = makePath(dest, destName); destFile = new File(destPath); } if (!destFile.createNewFile()) return null; fo = new FileOutputStream(destFile); int count = 102400; byte[] buffer = new byte[count]; int read = 0; while ((read = fi.read(buffer, 0, count)) != -1) { fo.write(buffer, 0, read); } // TODO: set access privilege return destPath; } catch (FileNotFoundException e) { LogUtils.e("copyFile: file not found, " + src); e.printStackTrace(); } catch (IOException e) { LogUtils.e("copyFile: " + e.toString()); } finally { try { if (fi != null) fi.close(); if (fo != null) fo.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * 从文件的全名得到文件名 * * @param filename * @return */ public static String getNameFromFilename(String filename) { int dotPosition = filename.lastIndexOf('.'); if (dotPosition != -1) { return filename.substring(0, dotPosition); } return ""; } /** * 从文件的全名得到文件的拓展名 * * @param filename * @return */ public static String getExtFromFilename(String filename) { int dotPosition = filename.lastIndexOf('.'); if (dotPosition != -1) { return filename.substring(dotPosition + 1, filename.length()); } return ""; }
转载地址:https://linqiarui.blog.csdn.net/article/details/50958954 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年05月02日 22时16分28秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Python实现决策树 Desision Tree & 可视化
2019-04-30
决策树 Decision tree
2019-04-30
nominal和ordinal & 数据处理中四种基本数据类型
2019-04-30
Python 实现 Cross-validation
2019-04-30
Grid SearchCV(网格搜索)& Python实现
2019-04-30
ROS相关知识
2019-04-30
单目深度估计 monodepth2模型 代码
2019-04-30
位图索引Bitmap indexes
2019-04-30
YOLO算法(二)—— Yolov2 & yolo9000
2019-04-30
YOLO算法(三)—— Yolov3 & Yolo系列网络优缺点
2019-04-30
Python的__future__模块
2019-04-30
计算机视觉中的cost-volume的概念具体指什么(代价体积)
2019-04-30
启发函数heuristic 与 A*
2019-04-30
Image Pyramid(图像金字塔)
2019-04-30