Article View: borland.public.jbuilder.java.language
Article #8462Re: Having problems with a sort...
From: Paul Linehan
Date: Fri, 23 Mar 2001 10:05
Date: Fri, 23 Mar 2001 10:05
81 lines
3174 bytes
3174 bytes
"Owen Green" <owen@norobots.unique.com> wrote:
> > Thanks for taking an interest. I would be grateful if you could figure
> > it out, it's been killing me for a while now.
> That was why in the first place
> the first string got pushed to the bottom. The, fairly inelegant, way I've
> got around this is to test after the while loop for the marker string being
> null, signifying that it went all the way through the file without finding a
> place to insert;
I tried your stuff and it didn't work, but I have finally come up with
a (very simple) solution...
The original problem really arose because I dug the algorithm out of a
book and it tacitly assumed that the line would be inserted somewhere
in an already existing file - it didn't take account of the fact that
I wanted to build one from scratch.
Thanks for your efforts on my behalf - and sorry for not replying
sooner, but I was (like others) having desperate problems with
timeouts on the Borland newsservers.
Paul...
insertStringInFile("recordCode.idx", indexLineAt, myDataString);
This calls
public static void insertStringInFile(String inFilename,
int lineno,
String lineToBeInserted)
throws IOException
{
// temp file
File outFile = new File("$$$$$$$$.tmp");
// input
File inFile = new File(inFilename);
FileInputStream fis = new FileInputStream(inFile);
BufferedReader in = new BufferedReader (new InputStreamReader(fis));
// output
FileOutputStream fos = new FileOutputStream(outFile);
PrintWriter out = new PrintWriter(fos);
String thisLine = "";
System.out.println("\nThe line number for insertion is " + lineno);
for(int i = 1; i < lineno; i++)
{
thisLine = in.readLine();
out.println(thisLine);
}
out.println(lineToBeInserted);
while ((thisLine = in.readLine()) != null)
{
out.println(thisLine);
}
out.flush();
out.close();
in.close();
inFile.delete();
outFile.renameTo(inFile);
}
Technical Details
Message-ID:
<3abb1dc9.860826583@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!news-xfer.nuri.net!enews.sgi.com!news-peer-west.sprintlink.net!news.sprintlink.net!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews!193.120.178.201References:
<3ab4c4e3.444852043@newsgroups.borland.com> <3ab62e22$1_2@dnews> <3ab63e8d.541533744@news.esat.net> <3ab6536e_1@dnews> <3ab88dce.692894920@newsgroups.borland.com> <3ab8c09f$1_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,91a554b109e4b985 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-23 04:08:05 PST From: paul@esat.net (Paul Linehan) Newsgroups: borland.public.jbuilder.java.language Subject: Re: Having problems with a sort... Date: Fri, 23 Mar 2001 10:05:20 GMT Message-ID: <3abb1dc9.860826583@newsgroups.borland.com> References: <3ab4c4e3.444852043@newsgroups.borland.com> <3ab62e22$1_2@dnews> <3ab63e8d.541533744@news.esat.net> <3ab6536e_1@dnews> <3ab88dce.692894920@newsgroups.borland.com> <3ab8c09f$1_2@dnews> X-Newsreader: Forte Free Agent 1.21/32.243 NNTP-Posting-Host: 193.120.178.201 X-Trace: 23 Mar 2001 02:06:51 -0800, 193.120.178.201 Lines: 82 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!news-xfer.nuri.net!enews.sgi.com!news-peer-west.sprintlink.net!news.sprintlink.net!newsfeed.direct.ca!look.ca!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews!193.120.178.201