如何使一个文件的副本在Android的? [英] How to make a copy of a file in android?

查看:111
本文介绍了如何使一个文件的副本在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想保存某个文件具有不同名称的副本(这是我从用户获得)

In my app I want to save a copy of a certain file with a different name (which I get from user)

我真的需要打开该文件的内容,并将其写入到另一个文件?

Do I really need to open the contents of the file and write it to another file?

什么是这样做的最佳方法是什么?

What is the best way to do so?

推荐答案

要复制的文件并将其保存到你的目的地的路径,你可以使用下面的方法。

To copy a file and save it to your destination path you can use the method below.

public void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

这篇关于如何使一个文件的副本在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆