常见文件操作代码(文件管理软件)
发布日期: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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:使用javah快速生成本地方法及生成so动态库文件
下一篇:解决Layout weight不起作用

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年05月02日 22时16分28秒