Article View: borland.public.jbuilder.java.language
Article #8365Re: Write a line
From: Ronald Wildenberg
Date: Tue, 27 Mar 2001 07:17
Date: Tue, 27 Mar 2001 07:17
46 lines
2227 bytes
2227 bytes
Hi Ahmad,
I think that there is no other solution than just to read the complete
file and then writing it again with all insertions at the correct place.
If you know all insertions beforehand, you can read the file up to
an insertion point, add the line(s) to be inserted and read again
until the next insertion point, etc.
Something like:
int linePointer = 0;
int insertionPointer = 0;
int[] insertionPoints = {20, 30, 40};
String[] insertionLines = {"line20", "line30", "line40"};
RandomAccessFile inFile = new RandomAccessFile("inFile.txt", "r");
ArrayList outFileArray = new ArrayList();
while ((String line = inFile.readLine()) != null) {
if (linePointer == insertionPoint[insertionPointer]) {
outFileArray.add(insertionLines[insertionPointer]);
insertionPointer++;
}
outFileArray.add(line);
linePointer++;
}
I have not tried to compile this yet so it might not work but this is the
idea.
Ronald.
Ahmad Noori wrote:
> Hello,
>
> I have a txt file that I want to write to it one line at a time. I
> sometimes want to write in the middle of the file and sometimes at the
> beginning. Using write(string, int, int), will replace the whole file,
> readline() reads one line at a time, does anyone knowof a method that
> could write one line at a time.
>
> Thanks a lot
>
> Ahmad Noori
> SAIC
Technical Details
Message-ID:
<3AC03EA0.B45FE1E1@tryllian.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!zeus.visi.com!news-out.visi.com!hermes.visi.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews!62.58.162.210References:
<3ABF903A.54E8FC0@SAIC.COM>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,95abce2b73e81abc X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-27 00:02:05 PST Message-ID: <3AC03EA0.B45FE1E1@tryllian.com> Date: Tue, 27 Mar 2001 09:17:52 +0200 From: Ronald Wildenberg <ronald@tryllian.com> X-Mailer: Mozilla 4.75 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language Subject: Re: Write a line References: <3ABF903A.54E8FC0@SAIC.COM> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 62.58.162.210 X-Trace: 26 Mar 2001 23:15:49 -0800, 62.58.162.210 Lines: 47 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!zeus.visi.com!news-out.visi.com!hermes.visi.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews!62.58.162.210