开发者

Writing 3D array into a binary file and reading file back into another 3D array Java

I have an assignment that I have to create a randomly sized 3D array, write it into a a binary file, then read the binary file back into the program and create another 3D array that's the same as the first. I'm having problems reading back into the program, after hours I can only get the first int, or the last, from the prior array. I haven't gotten into passing the first 2D's yet so I just allocated some space to ma开发者_如何学JAVAke the array work, but once I get this that should come quickly. The readData() method is the one giving 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);

}

}


As you have written it, you don't read from the file each time you loop in the innermost loop — and not either in the outer loops. So you read only once.

The ins.readInt() call should be in the innermost loop, because you need to read each table cell.


I believe you can attain this using Serialization.


Let's think. The readData() method positively MUST obtain information about the array size from somewhere, right? writeData() then MUST store it.

and, how many times do you call readInt() in the readData()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜