Java二进制I / O问题

时间:2011-02-03 21:13:58

标签: java collections io

好吧,所以我正在尝试将一堆数独游戏写入文件。这是我生成谜题的代码。 gen是生成谜题的类,gridArray是一个可以容纳谜题的对象数组(谜题是作为二维int数组生成的)。 System.out.print只打印网格以确保谜题有效。我遇到的问题在代码块下面说明:

public void run(){
    ObjectOutputStream output;
    try {
        output = new ObjectOutputStream(new FileOutputStream(f));

        for(int i = 0; i < amount; i++){
            int[][] blahbot = gen.generate(difficulty);
            gridArray[i] = blahbot;
            System.out.println(" #" + (i + 1) + " ");
            for(int row = 0; row < 9; row++){
                for(int col = 0; col < 9; col++){
                    System.out.print(blahbot[row][col]);
                    if(col == 8){
                        System.out.println();
                    }
                }
            }
        }
        output.writeObject(gridArray);
        output.close();
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

输出将整个对象数组写入文件。我遇到的问题是,每次从文件中读取数组时,它总会返回一个谜题。我有读取程序打印出阵列的整个内容,虽然数组的长度是正确的,但它是同一个谜题。

我把这个部分打印出阵列,因为它们被保存了,那些是正确的(没有重复)。我用完了测试的东西..我甚至尝试使用不同的集合类来保存,我想出了相同的结果。任何人都可以看到我的代码出了什么问题吗?

以下是我的代码的阅读部分:

try{
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
        try{
            while (true){
                try {
                    gridArray = (Object[]) input.readObject();
                } catch (ClassNotFoundException e) {
                } 
            }
        }catch(EOFException e){
        }

        /*
         * Close the input stream
         */
        input.close();
    } catch(FileNotFoundException e){
    } catch (IOException e) {
    }

    for(Object array: gridArray){
        int[][] temp = (int[][])array;
        for(int row = 0; row < 9; row++){
            for(int col = 0; col < 9; col++){
                System.out.print(temp[row][col]);
                if(col == 8){
                    System.out.println();
                }
            }
        }

        System.out.println("\n");
    }

其中r是随机数。它从文件中读取Object数组,然后将每个Object转换为int [] []以打印出来。打印只是测试数组内容的一项测试。

2 个答案:

答案 0 :(得分:1)

为了测试,请尝试更改:

for(int i = 0; i < amount; i++){
    int[][] blahbot = gen.generate(difficulty);
    gridArray[i] = blahbot;
    System.out.println(" #" + (i + 1) + " ");
    for(int row = 0; row < 9; row++){
        for(int col = 0; col < 9; col++){
            System.out.print(blahbot[row][col]);
            if(col == 8){
                System.out.println();
            }
        }
    }
}
output.writeObject(gridArray);
output.close();

对此:

for(int i = 0; i < amount; i++){
    output.writeObject(gen.generate(difficulty));
    output.flush();
}
output.close();

如果事情突然变得更好,结果指向ILMTitan在评论中提到的内容 - 您需要使用new关键字并实际从new int[9][9]方法返回generate而不是重复使用它返回的任何参考。

答案 1 :(得分:1)

根据显示的代码,对于每次调用gen.generate(),您似乎都返回相同的int [] []实例。如果此方法重用从先前调用创建的int [] [],那么实际上,您每次都会向数组添加相同的int [] []。随后对generate方法的调用将导致修改添加到数组列表中的数组。

可能发生的事情的例子:

private int[][]data = new int[9][9];
public int[][] generate() {
   // add logic here to set values in data array
   return data;
}

如果您的方法与上述类似,那么您将遇到您记录的问题。每次调用方法时都应该创建int [] []数组的新实例,或者更改将数据写入文件的方式。