java - 检查文件的成功副本
问题描述
我正在尝试在 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 的情况下检查它是最有效的方法吗?文件传输不完整/损坏并且长度仍然相同的可能性有多大?
解决方案
推荐阅读
- python - 在 Flask 上将 pandas DataFrame 渲染为 html
- css - React-bootstrap 模式添加边距或填充
- css - 特殊网格或 flexbox 布局:在框中插入文本的问题
- python - 在 Anaconda 中导入模块 OpenCV 时出现问题
- python - 如何使用 Selenium 和 Python 通过 Headless Chrome 使用 Chrome 配置文件
- azure - 数据库连接 pg_hba.conf
- typescript - 使用 Firebase 实时数据库,在触发器中创建条目
- python - 我使用 ExcelWriter 编写了几个数据框到 excel 工作簿。但我不知道他们保存到哪里
- c++ - 除了运行时,有没有办法测量代码的性能?
- bi-publisher - 如何使用BI发布者添加每组帐号的金额