0

Folder copy is wrong by recursive function under DEBUG mode on Android!!!

public class FileBox {
    public static boolean copySrc2Tar(File sourceLocation, File targetLocation) {

//        File sourceLocation = new File(src.getPath()); //ShallowCopy?
//        File targetLocation = new File(tar.getPath()); //DeepCopy?

        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i = 0; i < sourceLocation.listFiles().length; i++) {

                copySrc2Tar(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]));
            }
        } else {
            InputStream in = null;
            OutputStream out = null;
            try {

                in = new FileInputStream(sourceLocation);
                out = new FileOutputStream(targetLocation);

                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        return false;
                    }
                }
                if(in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                        return false;
                    }
                }
            }
        }

        return true; 
    }
}




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.acty_mnhnflfldr);

    InputStream in = null;
    OutputStream out = null;
    File curPath;

    if(getExternalStorageState().equals(MEDIA_MOUNTED) || getExternalStorageState().equals(MEDIA_MOUNTED_READ_ONLY))
        curPath = Environment.getExternalStorageDirectory();
    else
        curPath = Environment.getRootDirectory();

    File ff = new File(curPath +File.separator +"AFolder");
    File tt = new File(curPath +File.separator +"ZFolder");

    FileBox.copySrc2Tar(ff, tt, true);
}

My folder hierarchy like below:

AFolder ={FolderB, textB0.txt, textB1.txt}

FolderB ={FolderC, textC0.txt}

Then run the above code, Working well under normal mode on Android Studio. 'AFolder' and its all the elements copied into 'ZFolder'. Its result is...

ZFolder ={FolderB, textB0.txt, textB1.txt}

FolderB ={FolderC, textC0.txt}

But

Under "DEBUG" mode, 'ZFolder' is not copied perfectly. Always some elements are missing.

I can NOT find out. What is problem on the above code?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • That code should never work. Its wrong. Please just google for how to copy a directory. Pretty standard stuff. – greenapps Nov 08 '17 at 17:27
  • Why did not you put a Toast in every catch block? Now the user of your app never knows what happens. – greenapps Nov 08 '17 at 17:28
  • I've referenced http://stackoverflow.com/questions/4178168/how-to-programmatically-move-copy-and-delete-files-and-directories-on-sd – rtblgst rtblgst Nov 09 '17 at 03:03
  • This code is minimized minimized .. minimized . ... while what point is problem I was finding out around one week. So finally copySrc2Tar() is a origin of problem. Of course I've read the other articles which related to the FILE & copy. But in my code I can NOT find out Why! – rtblgst rtblgst Nov 09 '17 at 03:08
  • Error under DEBUG: java.io.FileNotFoundException: /storage/emulated/0/AFolder/FolderB/textC0.txt: open failed: ENOENT (No such file or directory) – rtblgst rtblgst Nov 09 '17 at 03:22
  • Again: your code is wrong. You can find many working examples on the internet and on this site. – greenapps Nov 09 '17 at 07:51

0 Answers0