安卓小问题,真的很小很小。
我在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/";
private FileInputStream inStream = null;
private FileOutputStream outStream = null;
private Vector<String> allFilePath = new Vector<String>();
@Override
public int validate() {
getFilePath(fileManagerPath);
fileManagerFileSize = getFileManagerFileSizeInMB();
Log.i(TAG, "fileManagerFileSize = " + fileManagerFileSize);
emmcAvailableSize = getEmmcAvailableSizeInMB();
Log.i(TAG, "emmcAvailableSize = " + emmcAvailableSize);
validateState = fileManagerFileSize == 0 ? -1 : 0;
return validateState;
}
@Override
public int proceed() {
if (validateState == 0) {
DisplayOperation.getInstance().processMessage(DisplayOperation.BEGIN_COPY_FILES, -1, -1);
sleep(1000);
long diff = emmcAvailableSize - fileManagerFileSize;
if (diff < 10) {
proceedState = -2;
DisplayOperation.getInstance().setKeyValue(DisplayOperation.KEY_COPY_FILES, false);
long neededSpace = fileManagerFileSize - emmcAvailableSize + 10;
DisplayOperation.getInstance().processMessage(DisplayOperation.END_COPY_FILES, proceedState, (int)neededSpace);
return proceedState;
} else {
proceedState = doCopyFile(allFilePath);
DisplayOperation.getInstance().setKeyValue(DisplayOperation.KEY_COPY_FILES, proceedState == 0);
DisplayOperation.getInstance().processMessage(DisplayOperation.END_COPY_FILES, proceedState, -1);
sleep(1000);
return proceedState;
}
}
return proceedState;
}
@Override
public int finish() {
return 0;
}
/*
* This function is used to calculate the free space on emmc in MB
*return:the available size on emmc
*/
private long getEmmcAvailableSizeInMB() {
String path = Environment.getEMMCStorageDirectory().getAbsolutePath();
StatFs stat = new StatFs(path);
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return blockSize * availableBlocks / MEGA_BYTE;
}
/*
*This function is used to calculate all the files' size
*under the directory "/data/cust/fileManager"
*return:total files' size in /data/cust/fileManager in MB
*/
private long getFileManagerFileSizeInMB() {
long totalSize = 0;
int size = allFilePath.size();
for (int i = 0; i < size; i++) {
totalSize += new File(allFilePath.get(i)).length();
}
return totalSize / MEGA_BYTE;
}
/*
*This function is used to get all the files' absolute
*path and put them into a vector.to prepare to calculate
*the total size of the files under the given dirpath
*it should be called before getFileManagerFileSizeInMB
*/
private String getFilePath(String dirpath) {
File file = new File(dirpath);
String path;
if (file.exists()) {
if (file.isDirectory()) {
File[] tmp = file.listFiles();
for (File f : tmp) {
path = getFilePath(f.getAbsolutePath());
if (path != null) {
allFilePath.add(path);
}
}
} else {
return file.getPath();
}
}
return null;
}
/*
* This function do the copy job
* return : -1 if any error occurs
* 0 if all file copied successfully
*/
private int doCopyFile(Vector<String> filePath) {
Vector<String> dest = getAllDestPath(filePath);
byte[] buf = new byte[4 * 1024];
int c = -1;
int size = filePath.size();
boolean mkFlag = false;
for (int i = 0; i < size; i++) {
try {
Log.i(TAG, filePath.get(i));
inStream = new FileInputStream(new File(filePath.get(i)));
File f = new File(dest.get(i));
mkFlag = new File(f.getParent()).mkdirs();
if (!mkFlag) {
Log.e(TAG,"mkdirs not finished");
}
if (!f.exists()) {
outStream = new FileOutputStream(f);
while((c = inStream.read(buf)) > 0) {
outStream.write(buf, 0, c);
}
outStream.flush();
}
} catch(IOException e) {
Log.e(TAG, "IOException ", e);
return -1;
} finally {
try {
if (inStream != null) {
inStream.close();
}
if (outStream != null) {
outStream.close();
}
} catch (IOException e) {
Log.e(TAG, "IOException ", e);
return -1;
}
}
}
return 0;
}
/**
* This function is used to get the destination file path base on the files' path in /data/cust/fileManager
* if a "fileManager" file's path is /data/cust/fileManager/test/a.xml
* then it should be copied to /data/share/test/
* this function is to get /data/share/test/ and put it into a vector
* return:a vector with all the destination files' path
*/
private Vector<String> getAllDestPath(Vector<String> filePath) {
Vector<String> dest = new Vector<String>();
int size = filePath.size();
for(int i = 0; i < size; i++) {
dest.add(destPath + filePath.get(i).substring(fileManagerPath.length()));
}
return dest;
}
private void sleep(long mills) {
try {
Thread.sleep(mills);
} catch(InterruptedException e) {
Log.e(TAG, "InterruptedException", e);
}
}
}