I have a purpose that I have to create a 3D array of random size, write it to a binary file, then read the binary file back into the program and create another three-dimensional array that will be the same as the first one. I have problems reading back to the program, after several hours I can only get the first int or the last from the previous array. I did not go past the first 2D, but I just allocated some space to make the array work, but as soon as I get this, it should come quickly. The readData () method is the one that gives me problems. Thanks in advance.
import java.io.*; import java.util.*; public class homework1 { public homework1() { } // Allocates space for the 3-dimension array as specified and for each // array element, assigns a random number, and return the array public static int[][][] createData() { int[][][] data; //Random variables for array dimensions Random rand = new Random(); int x = rand.nextInt(5) + 1; rand = new Random(); int y = rand.nextInt(5) + 1; rand = new Random(); int z = rand.nextInt(5) + 1; data = new int[x][y][z]; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { for (int k = 0; k < z; k++) { rand = new Random(); int r = rand.nextInt(5) + 1; data[i][j][k] = r; } } } return data; } //Writes the 3-dimension array to file. public static int[][][] writeData(int[][][] array, String fileName) throws IOException { try { FileOutputStream out = new FileOutputStream(fileName); DataOutputStream outs = new DataOutputStream(out); for (int i = 0; i < array.length; i++) { //outs.writeInt(array[i].length); (maybe?) for (int j = 0; j < array[i].length; j++) { //outs.writeInt(array[i][j].length); (maybe?) for (int k = 0; k < array[i][j].length; k++) { outs.writeInt(array[i][j][k]); } } } outs.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } return array; } public static int[][][] readData(String fileName) throws FileNotFoundException, IOException { int[][][] array = new int[3][3][5]; try { FileInputStream in = new FileInputStream(fileName); DataInputStream ins = new DataInputStream(in); int readFrom = ins.readInt(); //read 4 binary byes and System.out.println("From file"); while (in.read() != -1) { // poop = ins.readInt(); System.out.println(readFrom); for (int i = 0; i < array.length; i++) { //outs.writeInt(array[i].length); (maybe?) for (int j = 0; j < array[i].length; j++) { //outs.writeInt(array[i][j].length); (maybe?) for (int k = 0; k < array[i][j].length; k++) { array[i][j][k] = readFrom; } } } System.out.flush(); readFrom=ins.readInt(); } //save them in an integer } catch (FileNotFoundException e) { e.printStackTrace(); } catch (EOFException ex){ ex.printStackTrace(); } System.out.println("Blank array that needs to be filled"); return array; } // Displays the array. public static void printData(int[][][] array) { for (int i = 0; i < array.length; i++) { System.out.println("Frame " + i + ":"); for (int j = 0; j < array[i].length; j++) { for (int k = 0; k < array[i][j].length; k++) { System.out.print("\t" + array[i][j][k] + " "); } System.out.print("\n"); } } } public static void main(String args[]) throws IOException { // throws FileNotFoundException, IOException { int data[][][]; data = createData(); printData(data); writeData(data, "data.out"); data = readData("data.out"); printData(data); }
}
source share