写入文件/大量字节失败

tcbh2hod  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(509)

我正在通过套接字写入文件/大量字节。
但假设我在写字节。我这样做;

  1. //Connection.data is a dataoutputstream
  2. byte[] a = new byte[filelength];
  3. //load file into the array
  4. //write file
  5. for (int i = 0; i < a.length; i++) {
  6. Connection.data.writeByte(a[i]);
  7. }

接收:

  1. //dat is a datainputstream
  2. byte[] byteA = new byte[bytestoread]
  3. for (int i = 0; i < toread; i++) {
  4. byteA[i] = dat.readByte();
  5. }

我会记录传入的数据,如果文件是200000字节,它会停在199990字节左右,基本上,任何大小的字节[],它会停在最后一个字节,然后超时。如果你不明白,我会解释得更多。谢谢。

wsxa1bj1

wsxa1bj11#

写完后你需要打电话 flush 以确保实际发送字节。

  1. Connection.data.flush();

根据文件:
刷新此数据输出流。这将强制将任何缓冲输出字节写入流。
dataoutputstream的flush方法调用其底层输出流的flush方法。

相关问题