常见文件操作代码(文件管理软件)
2016-03-22 22:01
253 查看
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 ""; }
相关文章推荐
- 什么才算是真正的编程能力?
- 基于webrtc的媒体库测试代码以及接口介绍
- 【poj 1426】Find The Multiple 题意&题解&代码(C++)
- mybasit ,maven,spring-mvc与spring 的整合
- matlab 使用的一点观察
- spring 注入学习
- 代码练习
- java URL转PDF文件
- C/C++知识点整理(2)
- Python @property
- Java 类区别
- php的一些小笔记--数组
- Spring配置概述
- ReadWriteLock使用
- 大家一起和snailren学java-(四)初始化与清理
- Java的Log系统介绍和切换(转)
- JavaWeb 后端 <十一> 之 DBUtils 框架 (基本使用 结果集 事务处理 对表读取)
- Yaf安装和配置
- Google 网上论坛(Google Online Forums)
- php安装及相关配置