首页 > 解决方案 > 检查文件的成功副本

问题描述

我正在尝试在 Android 中制作文件管理器应用程序。

我想为用户提供移动文件的选项。所以首先我复制文件,然后如果没有错误,我将删除文件。

这是我用来复制文件的代码

public static boolean copy(File copy, String directory, Context con) {
   static FileInputStream inStream = null;
   static OutputStream outStream = null;
   DocumentFile dir = getDocumentFileIfAllowedToWrite(new File(directory), con);
   String mime = "";
   DocumentFile copy1 = dir.createFile(mime, copy.getName());

   try {
      inStream = new FileInputStream(copy);
      outStream = con.getContentResolver().openOutputStream(copy1.getUri());
      byte[] buffer = new byte[16384];
      int bytesRead;
      while ((bytesRead = inStream.read(buffer)) != -1) {
         outStream.write(buffer, 0, bytesRead);
      }
   }
   catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   } finally {
      try {
         inStream.close();
         outStream.close();
         return true;
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   return false;
}

但是在我的一台设备中,文件只是被删除而无需复制。

所以我的想法是我将检查文件的SourceFile.length(长度 ) 和长度DestinationFile.length()是否相同。如果两者相同,那么我将删除 SourceFile。

在不检查文件的 MD5 的情况下检查它是最有效的方法吗?文件传输不完整/损坏并且长度仍然相同的可能性有多大?

标签: javaandroidfilecorruptioncopying

解决方案


推荐阅读