Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading to a file, using FileWriter  RSS feed

 
Jon Anders Roee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im making a guestbook with java/jsp, im saving the messages in a file, using filewriter. When I read the file, the newest messages appers at the end of the file. How can I print the newest messages First?
Here is a some lines from my code:

if(ok){
String filnavn = application.getRealPath(reFilnavn);
String utskrift =
"<FONT Face=Verdana Size=2>" +
"<TABLE WIDTH=500 ><TR><TD><b>Dato:</b> " + dato.toString() + "</TD></TR>" +
"<TR><TD><b>Navn:</b> " + navn + "</TD></TR>" +
"<TR><TD><b>Epost: </b> <a href=mailto:" + epost +" >"+epost+"</A></TD></TR>" +
"<TR><TD><b>Hjemmeside: </b> <a href=" + url +" target=new>"+url+"</A></TD></TR>" +
"<TR><TD><b>Hilsen:</b><br>" + innhold + "</TD></TR><br><BR></TABLE>";

FileWriter skriveforbTilFil = new FileWriter(filnavn,true); //True betyr, skriv etter det som ligger p� fil ifra f�r
PrintWriter skriver = new PrintWriter(new BufferedWriter(skriveforbTilFil));
skriver.print(utskrift);
skriver.close();

}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Files just don't work that way on any OS I know. You certainly could add new records to the front of the file, but generally only by creating a new file, adding the new record, copying in the records from the old file, deleting the old file, and renaming the new file to the old name. It's a lot of slow work -- and ultimately pointless, too. You don't really care how the records are stored: you care how they are displayed. It would be much easier to write a JSP to display the guest book last-record-first than it would be to store them in that order.
 
Kalpith Ramamoorthi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Friedman-Hill is right! Why bother setting up your textfile! Displaying is more important...Ya I wanted to do the same thing until some guy told me to display it differently! I was using a JLabel and I was reading from a textfile and printing it I needed to put it in order so I understand... But for all the people like me we got to do it the hard but efficient way!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!