本文介绍了Java - 由块读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何按字节读取文件但无法找到如何以字节块的形式读取它的示例。我有一个字节数组,我想读取512字节的文件并通过套接字发送它们。

I know how to read a file by bytes but cannot find a example how to read it in chunks of bytes. I have a byte array, and i want to read the file by 512bytes and send them over a socket.

我试过通过读取文件的总字节数然后减去512字节,直到我得到一个小于512字节的块并发信号通知EOF和传输结束。

I have tried by reading total bytes of file and then subtracting 512 bytes until i got a chunk that was less than 512 bytes and signaled EOF and end of transfer.

我正在尝试实现TFTP,其中数据以512字节发送chunk。

I am trying to implement a TFTP, where data is sent in 512 byte chunks.

无论如何都要感谢一个例子。

Anyhow would be thankful for a example.

推荐答案

你...一次读取512个字节。

You ... read 512 bytes at a time.

char[] myBuffer = new char[512];
int bytesRead = 0;
BufferedReader in = new BufferedReader(new FileReader("foo.txt"));
while ((bytesRead = in.read(myBuffer,0,512)) != -1)
{
    ...
}

这篇关于Java - 由块读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 07:50