将3D数组写入二进制文件并将文件读回另一个3D数组Java

时间:2011-01-19 19:12:22

标签: java arrays io

我有一项任务,我必须创建一个随机大小的3D数组,将其写入二进制文件,然后将二进制文件读回程序并创建另一个与第一个相同的3D数组。我在回读程序时遇到了问题,几小时后我只能从前一个数组中得到第一个int或者最后一个。我还没有通过第一个2D,所以我只是分配了一些空间来使数组工作,但是一旦我得到它应该很快。 readData()方法给我带来了问题。提前谢谢。

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);

}

}

3 个答案:

答案 0 :(得分:1)

正如你所写的那样,每次在最里面的循环中循环时都不会从文件中读取 - 而不是在外部循环中。所以你只读过一次。

ins.readInt()调用应该在最里面的循环中,因为你需要读取每个表格单元格。

答案 1 :(得分:0)

我相信您可以使用序列化来实现这一目标。

答案 2 :(得分:0)

让我们思考一下。 readData()方法肯定必须从某个地方获取有关数组大小的信息,对吗? writeData()然后必须存储它。

并且,你在readData()中调用了readInt()多少次?