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

我有一项任务,我必须创建一个随机大小的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);

}

}

解决方法

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

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

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...