Thursday, April 17, 2014

Andrews small problem, is really very, very small.


I have on the SD card's own data folder mydata, the following two subfolders: valid and invalid, the valid below many folders book1, book2, bookn, each book below is a lot of files.

I need to move book1 valid from invalid, I ask by modifying the file names can achieve the purpose of it? I do not want to copy each file inside.

I have yet to test this issue, with experience to give me an opinion, okay?
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
I think it is not feasible.
First copy, then delete it.
Give you a few functions, refer to it:
 / ** 
* Copy the folder
*
* @ Param sourceDir
* @ Param targetDir
* /
public static void copyFolder (String sourceDir, String targetDir) {
/ / Create the target directory
(New File (targetDir)) mkdirs ();.
/ / Get the current source folder under the file or directory
. File [] file = (new File (sourceDir)) listFiles ();
if (file! = null && file.length> 0) {
for (int i = 0; i if (file [i]. isFile ()) {
/ / Source file
File sourceFile = file [i];
/ / Target file
File targetFile = new File (new File (targetDir) getAbsolutePath () + File.separator + file [i] getName ()..);
copyFile (sourceFile, targetFile);
}
if (file [i]. isDirectory ()) {
/ / Prepare to copy the source folder
String dir1 = sourceDir + "/" + file [i] getName ();.
/ / Prepare to copy the target folder
String dir2 = targetDir + "/" + file [i] getName ();.
copyFolder (dir1, dir2);
}
}
}
}


 / ** 
* Copy the file
*
* @ 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;
}


 / ** 
* Remove all files (folders)
*
* @ Param file
* @ Return
* /
public static boolean deleteAllFile (File file) {
if (file == null | |! file.exists ()) {
return false;
}
/ / If it is a directory
if (file.isDirectory ()) {
/ / Get all the files in the directory
File [] fileArray = file.listFiles ();
/ / Traverse file
for (int i = 0; i deleteAllFile (fileArray [i]);
}
}
return file.delete ();
}


 / ** 
* Rename the folder
*
* @ 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);
}

Reply:
Try chanting, I seem to remember that you can directly modify the path name
Reply:
 
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 allFilePath = new Vector ();

@ 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 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 filePath) {
Vector dest = getAllDestPath (filePath);
byte [] buf = new byte [4 * 1024];
int c = -1;
int size = filePath.size ();
boolean mkFlag = false;
for (int i = 0; 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 getAllDestPath (Vector filePath) {
Vector dest = new Vector ();
int size = filePath.size ();
for (int i = 0; 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);
}
}
}


If you rename the method is unsuccessful

Can refer me to throw the code

No comments:

Post a Comment