Hi all,

In my program I need to create 2 log files, success and error. But I need to create either one of them, depending on the out come of the program execution. In other words, if program fails for any reason, I need to create the error log file and log any errors/exceptions. On the other hand, if program succeeds, then I need to log the normal process of the program.

Basically, I don't want to create the two files. So, here's what I have done:

I created 2 StringBuffer. errorBuffer and successBuffer, and I append to them during my program execution. Say there's any error, in my catch block, I append to errorBuffer. At the end, I determine, whether there was any error using a boolean flag, then based on that, using PrintStream, I create the file (success or error) and wirte the content of error/success buffer to my printStream.

But, when I do so, all lines are printed in the file into one single line.

Is it StringBuffer behaviour? how can I write multiple lines to printStream, using StringBuffer

Here's what I mean:

public static void main(String[] arg) {
        DocActivityDetach da = new DocActivityDetach();
        //getCredentialFromProxyService();
        try {
            sm = da.getSessionMgr();            
            da.startDetaching ();
        } catch (Exception e) {
            da.errorFlag = true;
            errorBuffer.append(e.getMessage() + "\n"); <-- Notice \n Will it work?
            e.printStackTrace();
        } finally {
            int STATUS = 0;
            if (da.errorFlag) {
                STATUS = 2;
                da.writeToLog(errorLog, errorFile, errorBuffer);               
            } else {
                da.writeToLog(successLog, successFile, successBuffer);
            }            
            da.closeConnection ();
            System.exit(STATUS);
        }
    }
    
    private void writeToLog (PrintStream logFile, String fileName, StringBuffer buffer) {
        try {
            out = new FileOutputStream(fileName);
            logFile = new PrintStream (out);
            logFile.println(buffer.toString()); <---Writing to printStream 
            logFile.close();
        } catch (IOException ioe) {
            errorFlag = true;
            ioe.printStackTrace();
        }
    }

But when I open the file, all the information are written in on single line.

Can someone please comment.


Thanks,

Not really sure why you are getting that behavior. Your writeToLog() code wrote a StringBuffer with newline characters in it to multiple lines just fine here in a tiny test program.

Are you viewing the file with an editor that's okay with just linefeeds or does it expect carriage return and linefeed pairs?

I am opening the file with Windows Notepad. However, I haven't tried it with other editors.

When I use this. It works fine. It writes to to next line

PrintStream.println("First line\nThis should go to second line");

But not this: This will print both in the same line, at least when I viewed in Notepad.

StringBuffer sb =new StringBuffer ();
sb.append("First line\n");
sb.append("this should go to second line");
PrintStream.println(sb.toString());

I am not sure if it expects linefeed and carriage return.

Ok, yeah, Notepad needs a "\r\n" carriage return and line feed pair. Using just the newline shows up as a little square symbol. Wordpad will handle the newlines by themselves just fine. If you want to avoid having to mess with knowing which to use when, you can get the system-specific line separator from the system properties like so

String newLine = System.getProperty("line.separator");
StringBuffer sb =new StringBuffer ();
sb.append("First line");
sb.append(newLine);
sb.append("this should go to second line");
PrintStream.println(sb.toString());
Comments
Excellent explanation and good solution to the problem

Thanks, that did the trick.

Appreciate your help.

Thanks for your supprt, its really helped me to format my output string

I'm glad you got it helpful. If you want to ask question, start your own thread.

Thread Closed.

This question has already been answered. Start a new discussion instead.