将SD卡文件夹中的文件复制到另一个SD卡文件夹中

时间:2011-04-19 10:38:51

标签: android copy directory android-sdcard android-file

是否可以将sdcard中的文件夹复制到另一个以编程方式呈现相同SD卡的文件夹?

如果是的话,怎么做?

5 个答案:

答案 0 :(得分:39)

该示例的改进版本:

// If targetLocation does not exist, it will be created.
public void copyDirectory(File sourceLocation , File targetLocation)
throws IOException {

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists() && !targetLocation.mkdirs()) {
            throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath());
        }

        String[] children = sourceLocation.list();
        for (int i=0; i<children.length; i++) {
            copyDirectory(new File(sourceLocation, children[i]),
                    new File(targetLocation, children[i]));
        }
    } else {

        // make sure the directory we plan to store the recording in exists
        File directory = targetLocation.getParentFile();
        if (directory != null && !directory.exists() && !directory.mkdirs()) {
            throw new IOException("Cannot create dir " + directory.getAbsolutePath());
        }

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

如果传递的目标文件位于不存在的目录中,则获得更好的错误处理和更好的处理。

答案 1 :(得分:15)

参见示例here。 SD卡是外部存储设备,因此您可以通过getExternalStorageDirectory访问它。

答案 2 :(得分:4)

是的,我可以在我的代码中使用以下方法。希望充分利用你: -

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

}

答案 3 :(得分:1)

科特琳码

fun File.copyFileTo(file: File) {
    inputStream().use { input ->
        file.outputStream().use { output ->
            input.copyTo(output)
        }
    }
}

fun File.copyDirTo(dir: File) {
    if (!dir.exists()) {
        dir.mkdirs()
    }
    listFiles()?.forEach {
        if (it.isDirectory) {
            it.copyDirTo(File(dir, it.name))
        } else {
            it.copyFileTo(File(dir, it.name))
        }
    }
}

答案 4 :(得分:0)

要移动文件或目录,您可以使用File.renameTo(String path)功能

File oldFile = new File (oldFilePath);
oldFile.renameTo(newFilePath);