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

前端 未结 5 845
日久生厌
日久生厌 2020-12-13 10:22

Is it possible to copy a folder present in sdcard to another folder present the same sdcard programmatically ??

If so, how to do that?

5条回答
  •  一个人的身影
    2020-12-13 11:12

    yes it is possible and im using below method in my code . Hope use full to you:-

    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();
        }
    
    }
    

提交回复
热议问题