• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • 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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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