简体   繁体   English

RandomAccessFile Java

[英]RandomAccessFile java

RandomAccessFile has great advantages when i want to reach a certain byte in file in order to read/write to it. 当我想到达文件中的某个字节以对其进行读/写操作时,RandomAccessFile具有很大的优势。

on the other hand, i've noticed the following behavior: let's say i wanna save some data on the byte number 1,000,000,000 and it's the first time i'm writing to the file, it's making my file size hugh (1GB actually) for saving just one number and it's creation takes a few seconds (about 20 from my tests). 另一方面,我注意到了以下行为:假设我要在字节数1,000,000,000上保存一些数据,这是我第一次写入文件,这使我的文件大小很大(实际上为1GB),可以保存仅一个数字,创建过程需要几秒钟的时间(根据我的测试,大约需要20秒)。 i've also noticed that it initializes all the values that come before the byte 1,000,000,000 to 0 which means it iterates on all the preceeding bytes until it gets to the 1,000,000,000th byte. 我还注意到,它会将字节1,000,000,000之前的所有值初始化为0,这意味着它将对所有之前的字节进行迭代,直到到达第1,000,000,000个字节为止。

is there any way to prevent from that behavior and just to write to the specific byte i want to reach? 有什么办法可以防止这种行为,而只写我想到达的特定字节?

I don't know what exactly you are trying to achieve, but the answer is no, there is any way to do that. 我不知道您到底想达到什么目的,但是答案是否定的,有任何方法可以做到。 When you say that you are saving just on number, in fact this is what you(human) see, but not the computer. 当您说要节省的只是数量时,实际上这是您(人类)看到的,而不是计算机看到的。 To reach de 1,000,000,000 byte the computer have to know all the 1,000,000,000 bytes. 为了达到1,000,000,000字节,计算机必须知道所有1,000,000,000字节。

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

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