Java 二进制 I/O 问题

发布于 2024-10-15 11:31:48 字数 2102 浏览 0 评论 0原文

好吧,我正在尝试将一堆数独谜题写入一个文件。这是我用来生成谜题的代码。 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 是随机数。它从文件中读取对象数组,然后将每个对象转换为 int[][] 进行打印。打印只是为了查看数组内容的测试。

Alright, so I'm trying to write a bunch of Sudoku puzzles to a file. This is the code I have to generate the puzzles. gen is the class that generates the puzzles, gridArray is an object array that will hold the puzzles (The puzzles are generated as two-dimensional int arrays). The System.out.print just prints the grid to ensure that the puzzles are valid. The problem I have is stated below the code block:

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) {
    }
}

The output writes the entire object array to file. The problem I have is that every time I read the array from the file, it always returns a single puzzle. I have the read program printing out the entire content of the array and it's the same puzzle, although the length of the array is correct.

I made this part print out the arrays as they were saved, and those were correct (No repeats). I'm running out of things to test.. I even tried using different collections classes to save, and I came up with the same result. Can anyone see what's wrong with my code?

Here is the read portion of my code:

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

Where r is a Random. It reads the Object array from the file then it casts each Object as an int[][] to print out. The print is just a test to see the contents of the array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美不过初阳 2024-10-22 11:31:48

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

可能发生的情况示例:

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

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

Based on the code shown, it would appear that you are returning the same int[][] instance for every call to gen.generate(). If this method is reusing the int[][] that was created from a previous invocation, then you will be, in actuality, be adding the same int[][] to the array everytime. The subsequent calls to the generate method will result in modifying the arrays you added to your list of arrays.

Example of what could be happening:

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

If your method is similar to above, then you will have the problem you documented. You should create a new instance of the int[][] array everytime you call the method or change the way you are writing the data to the file.

机场等船 2024-10-22 11:31:48

为了进行测试,请尝试更改此设置:

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 关键字并实际返回来自 generate 方法的 new int[9][9] 而不是重用它返回的任何引用。

For the sake of testing, try changing this:

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

To this:

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

If things suddenly work better, the results point towards what ILMTitan mentioned in his comment -- you would need to use the new keyword and actually return a new int[9][9] from your generate method rather than reusing whatever reference it's returning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文