I am looking for an efficient way to create a file whose size is unknown but can range from mb's to gb's and fill its content randomly. I may write first 200 bytes than jump to the end and write last 200 bytes and move to the middle and write there. Is RandomAccessFile
efficient for doing this or are there any alternatives better suited for this kind of job?
views:
464answers:
3Yes, use RandomAccessFile
- that's what it's there for. You could potentially use a FileChannel
, but I'd go for RandomAccessFile
first - it's likely to be simpler.
Note that you won't be able to "insert in the middle" of the file after writing the end part: I don't know any file system which supports that. You'll need to know the complete size before you write the final section, basically. An alternative would be to write sequentially, remembering the final bit until the end.
If you can give us more information about what you need to do (and when you have information about the size) we may be able to help you more.
EDIT: To create the file with a specific size,you can seek()
past the end and then write data:
import java.io.*;
public class Test
{
// Just for the sake of a simple test program!
public static void main(String[] args) throws Exception
{
RandomAccessFile file = new RandomAccessFile("file.dat", "rw");
file.seek(100);
file.write(0);
file.close();
}
}
After this has exited, file.dat
will be 101 bytes long. (Note that normally you'd use try/finally etc.)
EDIT: As coobird mentioned in the comments, setLength()
will also extend the file - but it will also truncate the file if you give a length less than the current size. Given your comment about basically writing torrent data, I suspect that seeking and then writing is exactly the behaviour you want. When you receive a chunk, just seek to the right spot, write it out, and the file will extend itself when it needs to.