🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #8217

Re: How do you modify the content of a File

#8217
From: Joi Ellis
Date: Sat, 14 Oct 2000 00:00
42 lines
2731 bytes
Pascal AUDANT <paco@orbitek.com> wrote:
> Greetings everyone,
>     I am editing a file in a Java application. I want to be able to
> write to the file but also remove stuff from it (correct it). The only
> way I see right now to remove stuff from a file is to delete the entire
> file and to re-write it whole except the stuff I wanted remove. (it's a
> text file so the 'stuff' in question are actually String objects...)
> Is there a better way to do this ?
> thanx in advance,
> Pascal

Are you asking about general methods of doing this, or Java-specific
methods?  I've done this sort of thing in perl and pascal, you can do it
in C, I haven't looked to see if Java has the I/O type necessary for it.
I would be disappointed if Java didn't have it.

In general, on the platforms I have experience with (Dos, OS/2, Windows,
various Unixes) it's possible to open any file for random I/O, seek to
a particular spot in the file, and begin writing data.  

If your text file consists of fixed-size records, it's pretty easy to manage.
The difficulty comes when you wish to shorten or lengthen a line of text
in the middle of a file of varying length lines.  Doing this is so much
trouble that it's easier to just rewrite the whole file.  

Usually when I'm doing something like this, it's because I'm writing a file
which contains references to its own size, but I don't yet know how big it 
will be. I write X blanks where the size belongs, then go back to those blanks
later and overwrite them with a zero-padded number of the same length.

Some applications do things like write a flag value in the first character
of a line that is used only to denote that line as "deleted."  That line
can be reused later to store anything of the same or smaller size.  Larger
items are appended to the end of the file.  
-- 
Joi Ellis                    Software Engineer
Aravox Technologies          joi@aravox.com, gyles19@visi.com

No matter what we think of Linux versus FreeBSD, etc., the one thing I
really like about Linux is that it has Microsoft worried.  Anything
that kicks a monopoly in the pants has got to be good for something.
           - Chris Johnson
Technical Details
Message-ID: <8saca4$82c11@bornews.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <39E8AD29.EC6876A8@orbitek.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,a0888bdc77105000
X-Google-Attributes: gid111d45,public
From: Joi Ellis <joi@joi.aravox.com>
Subject: Re: How do you modify the content of a File
Date: 2000/10/14
Message-ID: <8saca4$82c11@bornews.borland.com>
X-Deja-AN: 681471577
References: <39E8AD29.EC6876A8@orbitek.com>
Organization: Another Netscape Collabra Server User
User-Agent: tin/1.4.2-20000205 ("Possession") (UNIX) (Linux/2.2.14-5.0 (i686))
Newsgroups: borland.public.jbuilder.java.swing