您的位置:首页 > 编程语言

常见文件操作代码(文件管理软件)

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 "";
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: