首页 > 解决方案 > Android-Java 检测 Output-Stream 何时完成写入字节

问题描述

我的代码工作正常,我只想在写入字节完成后展示一些吐司。我知道当我在 while 循环之后完成读取文件时可以添加 toast in.read(buf) == 0,因此 while 循环完成。我可以在一段时间后或在关闭您的流之后显示吐司。当我在 while 循环的末尾添加 toast 时,文件显示的时间与显示的 toast 的时间不同。它会在几分钟后显示出来。我怎么知道写入字节已完成或close()告诉它已完成?

File newfile;
FileInputStream in = new FileInputStream(new File(path));
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File dir = new File(root + "/" + "Pictures");
if (!dir.exists()) {
    dir.mkdirs();
}
newfile = new File(dir, "status_" + System.currentTimeMillis() + ".mp4");
if (newfile.exists()) newfile.delete();
OutputStream out = new FileOutputStream(newfile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
}
in.close();
out.close();

标签: javaandroidfilefile-handling

解决方案


他们是没有办法追踪OutputStream已经写完字节的,只是你可以在之后添加toast什么close()的。


推荐阅读