Writes a 3D array to a binary file and reads the file back to another 3D array

I have a task. I have to create a 3D array of random size, write it to the binary file, then read the binary file back to the program and create another 3D array the same as the first one I had a problem reading back the program. After a few hours, I could only get the first int or the last int from the previous array I haven't passed the first 2D yet, so I just allocated some space to make the array work, but once I get it, it should be fast The ReadData () method caused me a problem Thank you 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);

}

}

Solution

As you wrote, every time you loop in the innermost loop, it will not be read from the file - not in the outer loop So you only read it once

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>