java - Java复制文件的方法

标签 java android compilation

我正在使用 Android Studio。是否有可能的命令会复制 DCIM 文件夹中的图片并将其移动到 SD 卡的根目录?我已经研究过,但一无所获。我已经反编译了其他应用程序,但一无所获。您可能会说我是普通程序员的初学者。我刚开始为这个项目使用 Java。

即使没有我也想知道所以我可以停止在网上搜索答案:P

谢谢,欢迎所有评论,如果需要会发布更多信息! :)

最佳答案

使用这个函数

public void copyFile(File sourceFile, File destFile)
            throws IOException {

        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;
        FileInputStream is = null;
        FileOutputStream os = null;
        try {
            is = new FileInputStream(sourceFile);
            os = new FileOutputStream(destFile);
            source = is.getChannel();
            destination = os.getChannel();

            long count = 0;
            long size = source.size();
            while ((count += destination.transferFrom(source, count, size
                    - count)) < size)
                ;
        } catch (Exception ex) {
        } finally {
            if (source != null) {
                source.close();
            }
            if (is != null) {
                is.close();
            }
            if (destination != null) {
                destination.close();
            }
            if (os != null) {
                os.close();
            }
        }
    }

关于java - Java复制文件的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31848454/

相关文章:

java - 解析复杂的 Json Java 多节点

java - 从 Java ProcessBuilder 运行 OpenMPI 进程时,ompi_evesel->dispatch() 失败

android - Android 的全屏来电者

java - 在 libgdx 对话框中绘制形状和图像

c++ - 编译后在 OpenCV 中包含额外的库

使用不同的CFLAGS为不同的目标编译通用的C文件

java - appcompat primarycolour 实现

java - SQLite Android 运行时错误

android - PopupWindow 中的 Horizo​​ntalScrollView

c - 将目标文件与在具有不同对齐限制的体系结构上编译的结构访问链接