Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

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();

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

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkim18
dkim18

ASKER

thanks!!