gpt4 book ai didi

java - 如何保持文件打开以供阅读

转载 作者:行者123 更新时间:2023-12-03 01:52:57 24 4
gpt4 key购买 nike

我希望能够打开一个(非常大的)文件并在以后随时从任意点读取数据,而不将整个文件保留在内存中。

这可能吗?

我想打开一个文件的句柄,没有其他东西会读/写这个文件,并且能够在以后的某个时间点从任何给定的偏移量和长度中获取数据,而无需再次在磁盘中搜索该文件。

我原以为会有某种

1, open handle to file
2, create reader
3, skip reader to X
4, read for Y
5, reset reader to 0
6, return to step 3

起初,似乎 BufferedReader.mark 就是我想要的,但文档建议,如果我标记、跳过、读取、重置来 self 标记的地方,我跳过的地方,会保留在内存中吗?

来自文档,

Limit on the number of characters that may be read while still preserving the mark. An attempt to reset the stream after reading characters up to this limit or beyond may fail. A limit value larger than the size of the input buffer will cause a new buffer to be allocated whose size is no smaller than limit. Therefore large values should be used with care.

它说“读”,所以也许如果我跳过,它不会计入这个限制(这是有道理的),但我可能会读很多,所以看来这个新分配的输入缓冲区可能会变得相当大。我不希望这种事发生...

有更好的方法来解决这个问题吗?

最佳答案

您可能正在寻找 RandomAccessFile类。

关于java - 如何保持文件打开以供阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14993280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com