在我的应用程序,我想保存某个文件具有不同名称的副本(这是我从用户获得)
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();
}