繁体   English   中英

如何以编程方式将文件复制到另一个目录?

[英]How to copy a file to another directory programmatically?

有一个image file一个内部directory 如何将此image file copy到刚刚创建的另一个directory中? 这两个directories在设备的同一个internal storage上:)

您可以使用这些功能。 如果您传入文件,第一个将复制包含所有子项或单个文件的整个目录。 第二个仅对文件有用,并为第一个中的每个文件调用。

另请注意,您需要有权限才能做到这一点

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

职能:

 public static void copyFileOrDirectory(String srcDir, String dstDir) {

        try {
            File src = new File(srcDir);
            File dst = new File(dstDir, src.getName());

            if (src.isDirectory()) {

                String files[] = src.list();
                int filesLength = files.length;
                for (int i = 0; i < filesLength; i++) {
                    String src1 = (new File(src, files[i]).getPath());
                    String dst1 = dst.getPath();
                    copyFileOrDirectory(src1, dst1);

                }
            } else {
                copyFile(src, dst);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.getParentFile().exists())
            destFile.getParentFile().mkdirs();

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

        FileChannel source = null;
        FileChannel destination = null;

        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }

如果要以编程方式复制图像,请使用以下代码。

     File sourceLocation= new File (sourcepath);
     File targetLocation= new File (targetpath);

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

** 使用 FileUtils 这是简单快速和最好的方法并从这里下载 Jar 文件**

公共无效移动文件(字符串源路径){

         File source_f = new File(sourcepath);
        
        String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhatsappStatus/yourfilename.mp4";
        File destination = new File(destinationPath);
        try
        {
            FileUtils.copyFile(source_f , destination);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    } 

转到 FileUtils Jar 的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM