How to copy my files from one directory to another directory?

后端 未结 3 1382
梦毁少年i
梦毁少年i 2021-02-15 13:18

I am working on Android. My requirement is that I have one directory with some files, later I downloaded some other files into another directory and my intention is to copy all

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-15 13:24

    You have to use also below code:

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

提交回复
热议问题