将3D数组写入二进制文件并将文件读回另一个3D数组Java
发布时间:2020-12-15 02:23:20 所属栏目:Java 来源:网络整理
导读:我有一项任务,我必须创建一个随机大小的3D数组,将其写入二进制文件,然后将二进制文件读回程序并创建另一个与第一个相同的3D数组.我在回读程序时遇到了问题,几小时后我只能从前一个数组中得到第一个int或者最后一个.我还没有通过第一个2D,所以我只是分配了一
|
我有一项任务,我必须创建一个随机大小的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()调用应该在最里面的循环中,因为你需要读取每个表格单元格. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
热点阅读
