我在SD卡上有自己的数据文件夹mydata,下面有两个子文件夹:valid和invalid,在valid下面有许多文件夹book1、book2、bookn,每个book下面是很多文件的。
我需要将book1从valid移到invalid,请问通过修改文件名能达到目的吗?我不希望要复制里面的每个文件。
我自己尚未测试这个问题,有经验者给我一个意见,好吗?
------解决方案--------------------
我觉得不可行。
先复制,再删除吧。
给你几个函数,参考下吧:
/**
* 复制文件夹
*
* @param sourceDir
* @param targetDir
*/
public static void copyFolder(String sourceDir, String targetDir) {
// 新建目标目录
(new File(targetDir)).mkdirs();
// 获取源文件夹当前下的文件或目录
File[] file = (new File(sourceDir)).listFiles();
if (file != null && file.length > 0) {
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
// 源文件
File sourceFile = file[i];
// 目标文件
File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
copyFile(sourceFile, targetFile);
}
if (file[i].isDirectory()) {
// 准备复制的源文件夹
String dir1 = sourceDir + "/" + file[i].getName();
// 准备复制的目标文件夹
String dir2 = targetDir + "/" + file[i].getName();
copyFolder(dir1, dir2);
}
}
}
}
/**
* 复制文件
*
* @param srcFile
* @param destFile
* @throws IOException
*/
public static boolean copyFile(File srcFile, File destFile) {
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(srcFile);
output = new FileOutputStream(destFile);
byte[] buffer = new byte[BUFFER_SIZE];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
} catch (Exception e) {
KLog.handleLog(e);
return false;
} finally {
if (output != null)
try {
output.close();
} catch (IOException ioe) {
}
if (input != null)
try {
input.close();
} catch (IOException ioe) {
}
}
return true;
}
/**
* 删除所有文件(夹)
*
* @param file
* @return
*/
public static boolean deleteAllFile(File file) {
if (file == null
------解决方案--------------------
!file.exists()) {
return false;
}
// 如果是一个目录
if (file.isDirectory()) {
// 取得目录下所有文件
File[] fileArray = file.listFiles();
// 遍历文件
for (int i = 0; i < fileArray.length; i++) {
deleteAllFile(fileArray[i]);
}
}
return file.delete();
}
/**
* 文件夹重命名
*
* @param srcFolder
* @param newFolderName
* @return
*/
public static boolean renameFolder(File srcFolder, String newFolderName) {
String newFolderPath = srcFolder.getParent() + "/" + newFolderName;
copyFolder(srcFolder.getAbsolutePath(), newFolderPath);
return deleteAllFile(srcFolder);
}
------解决方案--------------------
试一下呗,我记得直接修改路径名字好像可以
------解决方案--------------------
public class CopyFileManagerFiles{
private static final String TAG = "CopyFileManagerFiles";
private int validateState = -1;
private int proceedState = -1;
private long fileManagerFileSize = 0;
private long emmcAvailableSize = 0;
private static final int MEGA_BYTE = 1024 * 1024;
private static final String fileManagerPath = "/data/cust/fileManager/";
private static final String destPath = "/data/share/";