简体   繁体   English

使用java.io.RandomAccessFile,如何编写文件并始终将内容添加到开头?

[英]Using java.io.RandomAccessFile, how do I write a file and keep adding content to the beginning?

How do I write to a file from the beginning using RandomAccessFile? 如何使用RandomAccessFile从头开始写入文件? I am writing into a file in 3mb byte chunks until it hits 100mb for benchmarking. 我正在以3mb字节的块大小写入文件,直到达到基准测试的100mb。

How do I write to a file from the beginning using RandomAccessFile? 如何使用RandomAccessFile从头开始写入文件?

You have to move the content already written. 您必须移动已经写入的内容。

Imagine the hard disk as a Lego base plate. 想象一下硬盘是Lego的底板。 You can start at one edge to put blocks one after the other. 您可以从一个边缘开始,将一个块另一个放置。 The blocks are the chunks of data you want to write and any consecutive line of blocks is a "file". 块是要写入的数据块,块的任何连续行都是“文件”。

However, if you want to put something at the beginning of the "file" you have to take of the blocks already there, put the new block at their position and put back the new locks behind them. 但是,如果您想在“文件”的开头放置一些内容,则必须使用已经存在的块,将新块放置在它们的位置,然后将新锁放回它们的后面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM