1

I have here a code which I will be using as a logging in my Java Server. This seems to be working fine in single line input. But when I entered multiple line. For example:

Hey there!
Hey More!

And when I looked at the textfile which the PrintWriter had created, it show me:

Hey there!Hey More!

What I want to see on my textfile is:

Hey there!
Hey More!

Here is my code: I don't know how will I append that. I'm thinking of \n but that doesnt work. Or is there something missing within my code. Please kindly check and you'll be a great help to me! Thanks!

try
            {
                clientSocket = serverSocket.accept(); // accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); // get the client message
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Calendar cal = Calendar.getInstance();
                StringBuilder str =new StringBuilder();
                StringBuilder abc =new StringBuilder();

                while ((message = bufferedReader.readLine()) != null) 

                {
                    abc.append("Message from " + message + " at " + dateFormat.format(cal.getTime())+"/n");
                    System.out.println("Message from " + message + " at " + dateFormat.format(cal.getTime()));
                    str.append(message+"\n");



                }
                PrintWriter out= new PrintWriter(new BufferedWriter(new FileWriter("C:/god.txt", true)));
                out.println(abc);
                out.close (); 
                JOptionPane.showMessageDialog(null, str);


                inputStreamReader.close();
                clientSocket.close();

            } catch (IOException ex)
            {
                System.out.println("Problem in message reading");
            }
3
  • You've posted "/n". Flip it. Also, println() adds a new line at the end of your String. Commented Sep 25, 2013 at 18:17
  • Or, even better, use one of the many excellent existing logging frameworks rather than rolling your own. Commented Sep 25, 2013 at 18:18
  • revert it to \n and still my problem comes out. It prints as a single line output eventhough i inputted multiple lines. Commented Sep 25, 2013 at 18:44

2 Answers 2

2

BufferedWriter has a newLine method. You should probably use the same

String line = new String();    
BufferedWriter writer= nnew BufferedWriter(new FileWriter("C:/god.txt", true));
    while ((message = bufferedReader.readLine()) != null) 
    {
      line = "Message from " + message + " at " + dateFormat.format(cal.getTime());
      System.out.println("Message from " + message + " at " + dateFormat.format(cal.getTime()));
      writer.write(line);
      writer.newLine();
     }
PrintWriter out= new PrintWriter(writer);
Sign up to request clarification or add additional context in comments.

3 Comments

What's wrong with PrintWriter.println()?!?!
How will I integrate it in my code? I will replace PrintWriter with BufferedWriter? How? Can you show me? Feel free to edit my code above.
edited the answer.. I feel you should do a newLine in loop instead of \n. @BoristheSpider, PrintWriter.println() should also work when you print line by line in a loop
1

Are you in windows? Using \n is not enough you need \n\r.

You can get a system independent line separator from:

System.getProperty("line.separator");

Or in Java 1.7:

System.lineSeparator();

in your code replace all "\n" with one of the above.

In a different note: As other people said, consider using a log framework.

2 Comments

Windows. How can I do the System.getProperty("line separator")? How can I put it in my code? Feel free to edit my code above.
Also for a better code use multiple appends instead of a + like: str.append(message).append(System.getProperty("line.separator"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.