Danny Rubis wrote:
> Hey!
>
> Tom, why go through all of this trouble. Just use RandomAccessFile and forget
> all of the stream stuff. He just wants to find the end of the file.
>
> out.seek(out.length()); //shoosh
>
> Just my two cents worth. This is all off topic for SERVLET-INTEREST.
>
This is still too much work. Checking the API docs on the file classes in the JDK
reveals that
FileWriter writer = new FileWriter("/path/to/logfile", true);
opens the underlying file in append mode. You can even wrap it in a PrintWriter so you
can use print() and println() calls instead of messing with line ending conventions
yourself:
PrintWriter writer =
new PrintWriter(new FileWriter("/path/to/logfile", true));
writer.println(".... stuff to be logged ....");
writer.close();
But you're right ... it's still off-topic :-).
>
> Danny Rubis
>
Craig McClanahan
>
> Tom Kochanowicz wrote:
>
> > Hi Steve,
> >
> > I believe you need to use the FileOutputStream in the below manner so that
> > it does not erase an existing file, for example:
> >
> > import file.io.*
> >
> > public class AltAppendFileOutputStream extends FileOutputStream{
> > protected FileInputStream file;
> >
> > public AltAppendFileOutputStream (String fileName) throws IOException{
> > super((new RandomAccessFile(fileName, "rw")).getFD());
> > file = new FileInputStream (getFD());
> > int n = file.available();
> > do{
> > n -= file.skip(n);
> > }
> > while (n > 0);
> > }
> > }
> >
> > Tom Kochanowicz
> >
> > -----Original Message-----
> > From: A mailing list for discussion about Sun Microsystem's Java Servlet
> > API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of
> > Steven Lau
> > Sent: Sunday, December 12, 1999 11:07 AM
> > To: [EMAIL PROTECTED]
> > Subject: logging to text file
> >
> > I'm doing some custom logging to a text file to make things easier and
> > cheaper than to logging to a database. The servlet would get the header
> > information, session Id and so on and attemps to log it to a file. The
> > problem I have is I don't know how to insert it to a file instead of
> > overwritting it. Instead of having multiple entries, it will open the file
> > and overwrite what's on it resulting in a file having only one log.
> >
> > Here is my code:
> > try {
> > String outputFileName = "F:" + File.separatorChar + "LLog.txt";
> > FileWriter fout = new FileWriter(outputFileName);
> > String newLine = System.getProperty("line.separator");
> > String data = session.getValue("userID") + ";" + session.getId() + ";" +
> > request.getRemoteAddr() + ";" + date + newLine;
> > fout.write(data);
> > fout.close();
> > } catch(java.io.IOException e) {
> > System.out.println("Cannot access LLog.txt");
> > }
> >
> > How would I modify it so it will insert the log at the end of the file and
> > not overwritting previous entries?
> >
> > Thanks!!!!
> >
> > ___________________________________________________________________________
> > To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> > of the message "signoff SERVLET-INTEREST".
> >
> > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > Resources: http://java.sun.com/products/servlet/external-resources.html
> > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> >
> > ___________________________________________________________________________
> > To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> > of the message "signoff SERVLET-INTEREST".
> >
> > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > Resources: http://java.sun.com/products/servlet/external-resources.html
> > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html