Suppose we want to use a different encoding than UTF-8
import java.math.*;
import java.io.*;
public class FibonacciLatin1 {
public static void main(String[] args) {
try {
FileOutputStream fout
= new FileOutputStream("fibonacci_8859_1.xml");
OutputStreamWriter out = new OutputStreamWriter(fout, "8859_1");
BigInteger low = BigInteger.ZERO;
BigInteger high = BigInteger.ONE;
out.write("<?xml version=\"1.0\" encoding=\"8859_1\"?>\r\n");
out.write("<Fibonacci_Numbers>\r\n");
for (int i = 0; i < 101; i++) {
out.write(" <fibonacci index=\"" + i + "\">");
out.write(low.toString());
out.write("</fibonacci>\r\n");
BigInteger temp = high;
high = high.add(low);
low = temp;
}
out.write("</Fibonacci_Numbers>");
out.close();
}
catch (IOException e) {
System.err.println(e);
}
}
}