Article View: borland.public.jbuilder.java.language
Article #8409Changing a character in a file...
From: Paul Linehan
Date: Mon, 19 Mar 2001 17:14
Date: Mon, 19 Mar 2001 17:14
59 lines
2434 bytes
2434 bytes
Hi all,
I have data as follows in a file
10000 Ams3vh45 02/01/1980 00:20 78-D-175677 Matthew Murray Ranelagh 0
10010 Onm6io99 05/01/1980 05:57 76-D-712944 Oonagh Moran Ballymon 0
10020 Nny5ev37 08/01/1980 11:35 76-D-837556 Claire Leinster Ranelagh 0
127 (+ carriage return) = 128 characters wide.
Now, what I want to do is to change the zero at the end of (say) the
second line into a 1.
So I tried this
public static void deleteRecordAtLine(int theLineNumber (= 2 in this case)) throws IOException
{
System.out.println("\nThe record being deleted is line number " + theLineNumber);
File myMainDataFile = new File("mainfile.dat");
RandomAccessFile mainDataFile = new RandomAccessFile(myMainDataFile, "rw");
mainDataFile.seek(theLineNumber * 128);
String recordToBeMarkedDeleted = mainDataFile.readLine();
StringBuffer strBuffer = new StringBuffer(recordToBeMarkedDeleted);
strBuffer.insert(125,'1');
recordToBeMarkedDeleted = strBuffer.toString();
mainDataFile.seek(theLineNumber * 128);
mainDataFile.writeChars(recordToBeMarkedDeleted);
mainDataFile.close();
}
But this just makes that line and the one beneath it into gibberish.
I am new to all this stuff about Strings, StringBuffers, arrays of
bytes, characters &c.
Anybody know any good references on this kind of thing and also could
anyone point out the flaw(s) in my code?
BTW, this is not the only thing I have tried.
Paul...
Technical Details
Message-ID:
<3ab63d02.541139347@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!193.120.178.201Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,4e502a0d6ce62c2d,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-23 03:42:07 PST From: paul@esat.net (Paul Linehan) Newsgroups: borland.public.jbuilder.java.language Subject: Changing a character in a file... Date: Mon, 19 Mar 2001 17:14:38 GMT Message-ID: <3ab63d02.541139347@newsgroups.borland.com> X-Newsreader: Forte Free Agent 1.21/32.243 NNTP-Posting-Host: 193.120.178.201 X-Trace: 19 Mar 2001 09:16:15 -0800, 193.120.178.201 Lines: 62 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!193.120.178.201