我已尝试对您的代码进行必要的最小更改以使其正常工作。st0le 在提供更简洁的流复制版本方面做得很好。
public class Test {
private static final String FORMAT = "UTF-8";
private static final int BUFFER_SIZE = 10; // for demonstration purposes.
public static void main(String[] args) throws Exception {
String string = "This is a test of the public broadcast system";
int clen = string.length();
ByteArrayInputStream in = new ByteArrayInputStream(string.getBytes(FORMAT));
OutputStream out = System.out;
int pos = 0, total_pos = 0;
byte[] buffer = new byte[BUFFER_SIZE];
while (pos != -1) {
pos = in.read(buffer, 0, BUFFER_SIZE);
if (pos > 0) {
total_pos += pos;
out.write(buffer, 0, pos);
setProgress((int) (total_pos * 100 / clen));
}
}
}
private static void setProgress(int i) {
}
}
- 当您将缓冲区写入输出流时,您忽略了pos的值。
- 您还需要重新检查pos的值,因为它可能刚刚读取了文件的末尾。在这种情况下,您不会增加total_pos(尽管您可能应该报告您已 100% 完成)
- 确保在适当的位置使用 close() 正确处理您的资源。
-edit- 使用数组作为缓冲区的一般原因是,输出流可以使用更大的数据集完成尽可能多的工作。
写入控制台可能没有太多延迟,但可能是正在写入的网络套接字或其他一些慢速设备。正如 JavaDoc 所述
OutputStream 的 write 方法在要写出的每个字节上调用一个参数的 write 方法。鼓励子类重写此方法并提供更有效的实现。
The benefit of using it when using a Buffered Input/Output Stream are probably minimal.