/* * 02100 + 02199 Programming * Exercise 28: Writing to a file * Author: Torben Gjaldbæk, c9718383 */ import java.io.*; /* * The main method of this class writes two arrays of integers to the file * 'data.txt'. */ public class IntArrayWriter { public static void main (String[] args) { // allocate arrays to be written int[] array1 = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; int[] array2 = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}; try { // create output streams FileWriter fileWriter = new FileWriter ("data.txt"); BufferedWriter buffer = new BufferedWriter(fileWriter); PrintWriter outFile = new PrintWriter(buffer); // strings to be written String string1 = ""; String string2 = ""; for (int i = 0; i < array1.length; i++) { string1 = string1 + array1[i] + " "; string2 = string2 + array2[i] + " "; } // write the strings outFile.println(string1); outFile.println(string2); // close output stream outFile.close(); // print OK message System.out.println("The two arrays of integers were written to the file 'data.txt'."); } catch (IOException exception) { System.out.println (exception); } } }