saving to a file - revisited lol

Hi

so iv got this method and it works, it creates the file according to the name i give and it writes appropriate info to the file, but the prob is that info is written in one huge line, can someone tell me how to get it to change to the new line after each piece of info?

publicstaticvoid Save (String file)

{

try

{

File outData =new File(file);

BufferedWriter write =new BufferedWriter(new FileWriter(outData));

for(int i=0;i<noClients;i++)

{

write.write(c[i].toString());

}

write.close();

}

catch (IOException e)

{

System.out.println("Error:\n" + e.toString());

}

}

so that instead of info appearing like this... name:marcSalary:25000...etc, it appears like this...

name:marc

Salary:25000...etc

Cheers>

[1421 byte] By [Aussie_Nerda] at [2007-11-27 5:59:08]
# 1
Decorate your writer using a PrintWriter and use printwriter.println(c.toString()) instead...
Peetzorea at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 2
write.write(c[i].toString());write.newLine();
AnanSmritia at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 3
write.write(c[i].toString() + "\n");
j_shadinataa at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 4

Hi AnanSmriti and j_shadinata, i tried out both of ur suggestions and unfortunately they werent what i am looking for, so peetzore, could you explain a little to me about how to create the printwriter item? iv heard of it but am just not sure how its used, heres my method once again if it helps u with showing how to use the printwriter

public static void Save (String file)

{

try

{

File outData = new File(file);

BufferedWriter write = new BufferedWriter(new FileWriter(outData));

for(int i=0;i<noClients;i++)

{

write.write(c[i].toString());

}

write.close();

}

catch (IOException e)

{

System.out.println("Error:\n" + e.toString());

}

}

Cheers>

Aussie_Nerda at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 5
Post your sample file content
AnanSmritia at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 6

I assume the following format

private String[] c = {"name:marcSalary:25000", "name:GilchristSalary:500000"};

public void Save (String file)

{

try

{

File outData = new File(file);

BufferedWriter write = new BufferedWriter(new FileWriter(outData));

for(int i=0;i<c.length;i++)

{

String str = c[i].substring(0, c[i].indexOf("Salary"));

write.write(str);

write.newLine();

write.write(c[i].substring(c[i].indexOf("Salary")));

write.newLine();

}

write.close();

}

catch (IOException e)

{

System.out.println("Error:\n" + e.toString());

}

}

Customize according to your file contents>

AnanSmritia at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 7

ok, when i open the notepad file it looks like this:

name: marcResident: trueGross Salary (per week): 480.77Net Salary(per week): 425.96

but just then i pasted it in here and it turned out just like this

name: marc

Resident: true

Gross Salary (per week): 480.77

Net Salary (per week): 425.96

in the notepad file it has tiny little square things between segments, this may be whats creating the spaces when i pase it elsewhere perhaps?

Aussie_Nerda at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 8
c[i].toStringthis calls a user-defined method called toString inside an array of client objects ('c') the method builds a string that contains all info of 1 client and so hence the method call is c[indexnumber].toString
Aussie_Nerda at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 9
it must be a notepad problem, it opens correct in wordpad
Aussie_Nerda at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 10

public void Save (String file)

{

try

{

File outData = new File(file);

BufferedWriter write = new BufferedWriter(new FileWriter(outData));

for(int i=0;i<c.length-1;i++)

{

// name: marcResident: trueGross Salary (per week): 480.77Net Salary(per week): 425.96

String name = c[i].substring(0, c[i].indexOf("Resident"));

String res = c[i].substring(c[i].indexOf("Resident"), c[i].indexOf("Gross Salary (per week)"));

String gs = c[i].substring(c[i].indexOf("Gross Salary (per week)"), c[i].indexOf("Net Salary(per week)"));

String ns = c[i].substring(c[i].indexOf("Net Salary(per week)"));

write.write(name);

write.newLine();

write.write(res);

write.newLine();

write.write(gs);

write.newLine();

write.write(ns);

write.newLine();

}

write.close();

}

catch (IOException e)

{

System.out.println("Error:\n" + e.toString());

}

}

>

AnanSmritia at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 11

All of the following would work to create the printwriter:

PrintWriter write = new PrintWriter(outData);

PrintWriter write = new PrintWriter(new FileWriter(outData));

Writing to file with a newline:

write.println("some message");

Peetzorea at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...
# 12
> it must be a notepad problem, it opens correct in wordpadfirst, you should identify what 'separator char' in that file. some 'char' not displayed well in notepad, but fine with better editor. (its '\r', perhaps...)
j_shadinataa at 2007-7-12 16:35:11 > top of Java-index,Java Essentials,New To Java...