?
Solved

What is wrong of my code?

Posted on 2005-04-16
4
Medium Priority
?
238 Views
Last Modified: 2010-05-18
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?
0
Comment
Question by:htam2000
  • 2
4 Comments
 
LVL 3

Expert Comment

by:neonlines
ID: 13796715
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
 
LVL 29

Accepted Solution

by:
bloodredsun earned 1000 total points
ID: 13796724
You should be using

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

to make sure that your code works on all platfoms.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13796726
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
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13796727
If you want to code it as you are you could try '\r\n' but I would advise doing it properly as above
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question