PDA

Click to See Complete Forum and Search --> : Writing a file in neat format


x-ice
Nov 29th, 2005, 03:26 PM
I am using the FileWriter() class to write a file. But i'm not getting what i want. Bill JonesJohn SmithMike BurnsDaniel MorganPeter HughesI dont want this, but i want this. Bill Jones
John Smith
Mike Burns
Daniel Morgan
Peter HughesHow can i get this? Can you get this using the FileWriter class as it doesn't appear that the class can do a carriage return when writing a file?

ComputerJy
Nov 29th, 2005, 11:26 PM
write a "\n" after each name

x-ice
Dec 1st, 2005, 03:17 AM
write a "\n" after each nameThat didn't work, can you show me what you mean with example code?

ComputerJy
Dec 2nd, 2005, 12:08 AM
import java.io.*;

public class Sample {
public static void main(String args[]) {
String names[] = {
"Bill Jones", "John Smith", "Mike Burns", "Daniel Morgan",
"Peter Hughe"};
FileWriter f = null;
try {
f = new FileWriter("C:\\a.txt");
for (int i = 0; i < names.length; i++) {
f.write(names[i] + " " + System.getProperty("line.separator"));
}
f.close();
}
catch (IOException ex) {
}
}
}