• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

writing string line by line when creating a file

Hi,

I am trying to create a file with certain string by each line. System out prints correctly, but when I see a created file it is all one line. How do I handle this?

thx,

for(int i=0; i < listData.size(); i++){
				listValue += listData.elementAt(i).toString()+"\n";			
			}
			System.out.println("listValue: " + listValue);
			fh.writeToFile(selectedFile, listValue);
 
 
...
...
public void writeToFile(File selectedFile, String value){
		try {
			File flt = new File(selectedFile.toString());
			FileWriter wrt = new FileWriter(flt);
 
			
 
			wrt.append(value);
			wrt.flush();

Open in new window

0
dkim18
Asked:
dkim18
  • 3
1 Solution
 
objectsCommented:
use a PrintWriter and println()

public void writeToFile(File selectedFile, String value){
                try {
                        File flt = new File(selectedFile.toString());
                        PrintWriter wrt = new PrintWriter(new FileWriter(flt));
 
                       
 
                        wrt.println(value);
                        wrt.flush();

0
 
objectsCommented:
sorry misread your code, you could be using the wrong line terminator for your os

0
 
objectsCommented:
try using:

String newline = System.getProperty("line.separator");
for(int i=0; i < listData.size(); i++){
                                listValue += listData.elementAt(i).toString()+newLine;


or write the lines directly (using println()) as you read them                    

0
 
dkim18Author Commented:
thanks!!
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now