Re: Do any java.io classes support inserting text into a file?
- From: Tom Anderson <twic@xxxxxxxxxxxxxxx>
- Date: Wed, 20 Aug 2008 15:55:50 +0100
On Tue, 19 Aug 2008, Arne Vajhøj wrote:
Tom Anderson wrote:On Tue, 19 Aug 2008, Arne Vajhøj wrote:Danger_Duck wrote:On Aug 19, 10:47 am, Eric Sosman <Eric.Sos...@xxxxxxx> wrote:Danger_Duck wrote:So I need to insert a string at the top of a file.
Oh, drat! You forgot "Adelia," which belongs at the top of the list -- but since you knew Donizetti was prolific and the list would be long, you started right at the top edge of the paper and there's no space above the existing first entry. Now ponder what sort of "simple way" would allow you to insert "Adelia" in its proper place without recopying.
Heh, ok. I was thinking that the file was stored as an array of characters rather than a piece of paper though, and there might be some way to move the pointer that points to the first element of the array back by the number of characters I have to prepend. Then I could copy the characters in and all would be well.
piece of paper = disk block
Let us say that your file system uses disk blocks of 4096 bytes.
And you need to insert something at the beginning. If what you insert
just happen to be a multipla of 4096 then you could allocate some
new blocks, write you data and update the file meta data to include
the new blocks. But if it is not a multipla of 40956, then it can not
be done for the same reasons as the piece of paper.
You simply can not do it.
No, with existing filesystems, you're quite right, you can't.
But you could imagine a filesystem which did make efficient inserts possible. The trick would be to allow partially-filled blocks inside a file, so that if you want to insert or prepend less than a block's worth (or some non-integer multiple of a block's worth) of data, you could partially fill a block, then splice it into the middle of the file.
It could be done.
But I don't think anyone would.
Probably not!
Because there are really not much usage for it.
Hmm. No, you're probably right. It might be useful if you were very limited in RAM, but if you have a decent amount of memory, then cacheing makes the rewrite-the-whole-file approach sufficiently efficient.
If there was an application which manipulated large files, needed to make random, variable-sized insertions into them, and needed to run fast, then such a filesystem would be useful. However, i suspect that such applications don't exist, because the lack of efficient inserts in existing filesystems leads them to be written not to use files like that. For instance, a bulletin board system, which needs to maintain a record for each board: with efficient inserts, you could use a single file for each board, but without them, you just use a folder for the board, and a file per post.
For something more flexible than traditional sequential files various index-sequential file systems exist.
I would expect those to be able to solve almost all problems that the partial disk block approach would.
Aren't such files based on fixed-length records? That means they wouldn't be much use for the text-processing use case the OP described.
tom
--
But for [Flavor Flav's] "YEAAAAAAAAAAAAAH BOYYYYYYYYYY"s alone he should
be given Rap Legend status. -- Nate Patrin, ILX
- Follow-Ups:
- Re: Do any java.io classes support inserting text into a file?
- From: Eric Sosman
- Re: Do any java.io classes support inserting text into a file?
- References:
- Do any java.io classes support inserting text into a file?
- From: Danger_Duck
- Re: Do any java.io classes support inserting text into a file?
- From: Eric Sosman
- Re: Do any java.io classes support inserting text into a file?
- From: Danger_Duck
- Re: Do any java.io classes support inserting text into a file?
- From: Arne Vajhøj
- Re: Do any java.io classes support inserting text into a file?
- From: Tom Anderson
- Re: Do any java.io classes support inserting text into a file?
- From: Arne Vajhøj
- Do any java.io classes support inserting text into a file?
- Prev by Date: Re: The mac for Java programmers
- Next by Date: ZERG WINS!!!!!!!!!!!!!!!
- Previous by thread: Re: Do any java.io classes support inserting text into a file?
- Next by thread: Re: Do any java.io classes support inserting text into a file?
- Index(es):
Relevant Pages
|
Loading