JAVA CODE
Write a program which has a 3D array and prints the integers from the array. Then then convert the output into a file using filewriter
import java.io.FileWriter; import java.io.IOException; public class Array3DToFile { public static void main(String[] args) { int arr[][][] = { { {1,2,3,4}, {5,6,7,8} }, { {8,7,6,5}, {4,3,2,1} } }; for(int i = 0;i<arr.length;i++){ for(int j = 0;j<arr[i].length;j++){ for(int k = 0;k<arr[i][j].length;k++){ System.out.print(arr[i][j][k]+"\t"); } System.out.println(); } System.out.println(); } FileWriter fw= null; try { fw = new FileWriter("output.txt"); for(int i = 0;i<arr.length;i++){ for(int j = 0;j<arr[i].length;j++){ for(int k = 0;k<arr[i][j].length;k++){ fw.write(""+arr[i][j][k]+"\t"); } fw.write("\n"); } fw.write("\n"); } fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1
Get Answers For Free
Most questions answered within 1 hours.