Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copying the entire folder with its contents from assets to internal app files/

Please, suggest me the best way of copying a folder from assets to /data/data/my_app_pkg/files.

The folder from assets (www) contains files and subfolders. which I want to completely copy to the files/ of my internal app path mentioned.

I am successfully able to copy a file from assets to internal app files/ path, but unable to do the same in case of copying folder, even assetmanager.list isn't helping me out, as it is copying only the files, but not the directories / subfolders.

Please kindly suggest me the better way to do what I want

like image 377
Narendra Singh Avatar asked Mar 19 '13 07:03

Narendra Singh


People also ask

How do I copy a folder and its contents?

Alternatively, right-click the folder, select Show more options and then Copy. In Windows 10 and earlier versions, right-click the folder and select Copy, or click Edit and then Copy. Navigate to the location where you want to place the folder and all its contents.

How do I copy the contents of multiple folders?

If you want to copy the set of files to multiple folders, hold down the CONTROL key while dragging and dropping the file(s). This ensures that the files in the drop stack are not removed once the copy process has completed. This makes it easy to copy the same set of files (from different folders) to multiple locations.

How do I copy the contents of a folder in Linux?

In order to copy a directory on Linux, you have to execute the “cp” command with the “-R” option for recursive and specify the source and destination directories to be copied. As an example, let's say that you want to copy the “/etc” directory into a backup folder named “/etc_backup”.


1 Answers

Hope use full to you below code:-

Copy files from a folder of SD card into another folder of SD card

Assets

            AssetManager am = con.getAssets("folder/file_name.xml");


 public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File targetLocation)
    throws IOException {

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

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

        copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]),
                new File(targetLocation, children[i]));
    }
} else {

    InputStream in = new FileInputStream(sourceLocation);

    OutputStream out = new FileOutputStream(targetLocation);

    // Copy the bits from instream to outstream
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

}
like image 141
duggu Avatar answered Sep 28 '22 19:09

duggu