Writing contents of String array to a text file

I have written a program that writes elements stored in variables to a text file. It uses the BufferedWriter/FileWriter classes. I want to output an Array of type String onto the text file but it is not allowing me to output the Array. I am using eclipse to run the writer and it shows up an error for the command;

out.write(order.getExtraFillings());

extraFillings is declared as;

private String[] extraFillings

and getExtraFillings() is its getter method. The error i get is;

"The method write(int) in the type BufferedWriter is not applicable for the

arguments (String[])"

I understand that this means the write method does not accept an array string as an argument, but how do I then write the Array into the text file?

[809 byte] By [DontKnowJacka] at [2007-11-26 16:04:16]
# 1

I DontKnowJack if this is what you're looking for!

StringBuffer sb = new StringBuffer();

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

sb.append(extraFillings[i]);

sb.append(" "); //dou you want to separate the contents by an empty space?

}

out.write(sb.toString();

regards,

Manuel Leiria

manuel.leiriaa at 2007-7-8 22:26:12 > top of Java-index,Java Essentials,New To Java...
# 2

Here's one I prepared earlier...

class StringArrayUtils {

public static String join(String[] a, String FS) {

if (a==null) return "";

StringBuffer sb = new StringBuffer(a[0]);

for(int i=1; i<a.length; i++) {

sb.append(FS + a[i]);

}

return sb.toString();

}

public static String join(String[] a) {

return join(a, ", ");

}

public static void main(String[] args) {

String[] words = { "Hello", "World!" };

System.out.println(join(words));

}

}

It would be nice to genericise this to Object[] somehow... but I don't know how (yet).

Keith.>

corlettka at 2007-7-8 22:26:12 > top of Java-index,Java Essentials,New To Java...
# 3

If you are using at least Java 1.5:

out.write(Arrays.toString(order.getExtraFillings()));

where Arrays is java.util.Arrays.

If you are using Java 1.4 or below:

out.write(Arrays.asList(order.getExtraFillings()).toString());

Either of these should give a String that looks like the following:

[fillings0, fillings1, fillings2, fillings3]

If you don't like that format, you'll have to loop over the array yourself, as the other two posters' code suggested.

doremifasollatidoa at 2007-7-8 22:26:12 > top of Java-index,Java Essentials,New To Java...
# 4

> It would be nice to genericise this to Object[]

> somehow... but I don't know how (yet).

Initialize the StringBuffer as:

StringBuffer sb = new StringBuffer(a[0].toString());

And I don't think the rest will need to change at all. The FS + a[ i ] will automatically cause toString to be called on the other elements of 'a'.

doremifasollatidoa at 2007-7-8 22:26:12 > top of Java-index,Java Essentials,New To Java...
# 5

Cool! this works (in 1.6)... Thanx doremifasollatido.

class StringArrayUtils {

public static String join(Object[] a, String FS) {

if (a==null) return "";

StringBuffer sb = new StringBuffer(""+a[0]);

for(int i=1; i<a.length; i++) {

sb.append(FS+a[i]);

}

return sb.toString();

}

public static String join(Object[] a) {

return join(a, ", ");

}

public static void main(String[] args)

{

String[] words = { "Hello", "World!" };

System.out.println(join(words));

Integer[] integers = { 1, 2, 3 };

System.out.println(join(integers));

}

}

>

corlettka at 2007-7-8 22:26:12 > top of Java-index,Java Essentials,New To Java...
# 6

> Cool! this works (in 1.6)... Thanx doremifasollatido.

You're welcome. It should work in 1.5, too. And, if you create the Integers yourself (use a constructor, instead of using autoboxing), it would work in 1.4 and below, too.

For 1.5 or 1.6, I would suspect that StringBuilder should be used instead of StringBuffer.

> StringBuffer sb = new StringBuffer(""+a[0]);

Eww! I like a[0].toString() better. Or String.valueOf(a[0]). But, definitely not appending to an empty String.

doremifasollatidoa at 2007-7-8 22:26:12 > top of Java-index,Java Essentials,New To Java...