Java - 按块读取文件? [英] Java - Read file by chunks?

查看:29
本文介绍了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 字节的块发送.

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 - 按块读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆