如何在Android中创建一个文件的副本?

在我的应用程序中,我想保存一个具有不同名称的特定文件的副本(我从用户那里获得)

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

最好的办法是什么?

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

public static void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); try { OutputStream out = new FileOutputStream(dst); try { // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { out.close(); } } finally { in.close(); } } 

在API 19+上,您可以使用Java自动资源管理:

 public static void copy(File src, File dst) throws IOException { try (InputStream in = new FileInputStream(src)) { try (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); } } } } 

或者,您可以使用FileChannel来复制文件。 复制大文件时,它可能比字节复制方法更快。 如果你的文件大于2GB,那么你不能使用它。

 public void copy(File src, File dst) throws IOException { FileInputStream inStream = new FileInputStream(src); FileOutputStream outStream = new FileOutputStream(dst); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close(); outStream.close(); } 

这些对我很好

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

答案可能为时已晚,但最方便的方法是使用

FileUtils

static void copyFile(File srcFile, File destFile)

这就是我所做的

`

 private String copy(String original, int copyNumber){ String copy_path = path + "_copy" + copyNumber; try { FileUtils.copyFile(new File(path), new File(copy_path)); return copy_path; } catch (IOException e) { e.printStackTrace(); } return null; } 

`

这是一个解决方案,如果在复制时发生错误,则会实际关闭输入/输出流。 该解决方案利用Apache Commons IO IOUtils方法来复制和处理关闭流。

  public void copyFile(File src, File dst) { InputStream in = null; OutputStream out = null; try { in = new FileInputStream(src); out = new FileOutputStream(dst); IOUtils.copy(in, out); } catch (IOException ioe) { Log.e(LOGTAG, "IOException occurred.", ioe); } finally { IOUtils.closeQuietly(out); IOUtils.closeQuietly(in); } } 

如果您拥有root权限,则可以使用以下代码进行复制:

 void copyFile_dd(){ try { Process su; su = Runtime.getRuntime().exec("su"); String cmd = "dd if=/mnt/sdcard/test.dat of=/mnt/sdcard/test1.dat \n"+ "exit\n"; su.getOutputStream().write(cmd.getBytes()); if ((su.waitFor() != 0)) { throw new SecurityException(); } } catch (Exception e) { e.printStackTrace(); //throw new SecurityException(); } } 

要么

 void copyFile_cat(){ try { Process su; su = Runtime.getRuntime().exec("su"); String cmd = "cat /mnt/sdcard/test.dat > /mnt/sdcard/test2.dat \n"+ "exit\n"; su.getOutputStream().write(cmd.getBytes()); if ((su.waitFor() != 0)) { throw new SecurityException(); } } catch (Exception e) { e.printStackTrace(); //throw new SecurityException(); } } 

Kotlin扩展它

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