What is wrong of my code?

import java.io.*;
public class Test{
      public static void main(String[] args) throws IOException{

            StringBuffer buffer = new StringBuffer();
            PrintWriter write = new PrintWriter(new FileWriter("abc.txt"));
            buffer.append('a');
            buffer.append('b');
            buffer.append('c');
            buffer.append('d');
            buffer.append('e');
            buffer.append('\n');
            buffer.append('f');
            buffer.append('g');
            write.print(buffer);
            write.close();
     }
}

Why the abc.txt file only has one line? (abcdefg)
Should it be:
abcde
fg
??? why ??? How can I fix it?
htam2000Asked:
Who is Participating?
 
bloodredsunCommented:
You should be using

public static String newline = System.getProperty("line.separator");

to make sure that your code works on all platfoms.
0
 
neonlinesProject managerCommented:
I tested ur code and it gives me the output what u r expecting

abcde
fg

I tested in jdk1.4.1 and jdk1.3

U please check again or let me know about ur jdk ver

0
 
suprapto45Commented:
Hi,

Do you get something like this "abcde*fg" where * is something weird codes? Well, it is okay. How do you open abc.txt file. If you use Notepad, sometimes, it shows on the same line but actually, the fg is already in the new line. Try to open it using Wordpad. You will see the difference.

Best Regards
Dave
0
 
bloodredsunCommented:
If you want to code it as you are you could try '\r\n' but I would advise doing it properly as above
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.