简体   繁体   English

从RandomAccessFile读取特定字节并进行测试以查看它们是否等于0

[英]Reading specific bytes from RandomAccessFile and testing to see if they equal 0

first time poster here. 第一次海报在这里。 Thank you in advance for viewing my question. 预先感谢您查看我的问题。 I'm having a ton of trouble with a homework problem in which I have to read a specific range of bytes from a RandomAccessFile, then check that range of bytes to see if they all equal 0. I've looked all over for something that pertains to this, but nothing I've found quite hits the spot. 我在做作业时遇到了很多麻烦,必须从RandomAccessFile中读取特定范围的字节,然后检查该字节范围,以查看它们是否都等于0。与此有关,但我发现没有什么很吸引人的。 Any help provided will be appreciated. 提供的任何帮助将不胜感激。

The problem tells us that there is a certain file that contains data for hypothetical students in a school. 该问题告诉我们,存在一个特定文件,其中包含学校中假设学生的数据。 Each of these students is represented in 40 bytes of code, but the first four bytes of our file must be an integer with the total number of students in the school(let's say there are 75). 这些学生中的每个人都用40字节的代码表示,但是文件的前四个字节必须是一个整数,该整数表示学校的学生总数(假设有75个)。 Bytes 4 through 43 represent the first student (#0), 44 through 83 represent the second (#1) and so on. 字节4到43代表第一个学生(#0),字节44到83代表第二个学生(#1),依此类推。 When a student transfers to another school, their 40 bytes is overwritten with all 0's (characters). 当学生转移到另一所学校时,他们的40个字节被全0(字符)覆盖。

I've written a method called "transferStudent" that takes a String which represents the file name and the integer which represents the number of students. 我编写了一种名为“ transferStudent”的方法,该方法采用一个代表文件名的String和一个代表学生人数的整数。 If there are any exceptions or if the file doesn't overwrite the student's data for some reason, I return false; 如果有任何例外情况,或者文件由于某种原因未覆盖学生的数据,则返回false;否则,返回false。

Here is my work thus far: 到目前为止,这是我的工作:

public static Boolean transferStudent(String fileName, int studentNum) {

    RandomAccessFile file = new RandomAccessFile(fileName, "rw");
    file.writeInt(75);
    try {
        if (studentNum == 0) {
            file.seek(4);
            file.writeBytes("0000000000000000000000000000000000000000"); // 40 zero characters
            file.seek(4);
            for (int i = 0; i < 40; i++) {
                if (file.read() == 0) {
                    return true;
                }
            }
            return false;
        }
        else if (studentNum > 0) {
            file.seek(4 + (studentNum * 40));
            file.writeBytes("0000000000000000000000000000000000000000"); // 40 more zeroes
            file.seek(4);
            for (int i = (4 + (studentNum * 40)); i < (44 + (studentNum * 40)); i++) {
                if (file.read() == 0) {
                    return true;
                }
            }
            return false;
        }
        else {
            return false;
        }
    }
    catch (Exception e) {
        return false;
    }
}

Whenever I view the binary file that's been created, there are indeed 0's in the range that corresponds with the studentNum. 每当我查看已创建的二进制文件时,在与studentNum对应的范围内确实存在0。 However, the console always prints false - the check isn't working for some reason. 但是,控制台始终显示false-由于某些原因,检查无法正常进行。 I'm on the verge of tearing my hair out over this. 我正要为此扯头发。 Please help! 请帮忙!

You're confusing ASCII zeroes "0" with binary zeros. 您将ASCII零“ 0”与二进制零混淆了。 You're writing the former and testing for the latter. 您正在编写前者,并为后者进行测试。 An ASCII "0" takes two bytes. ASCII“ 0”占用两个字节。 Note that 'character' and 'byte' aren't the same in Java. 请注意,“字符”和“字节”在Java中是不同的。

So I think I've finally figured out the issue: Like EJP stated, I was confusing the ASCII zeroes "0" with binary zeros. 因此,我想我终于找到了问题所在:就像EJP所说的那样,我将ASCII零“ 0”与二进制零混淆了。 As stated, ASCII zeroes take up two bytes of information- This was, and really still is, confusing to me: I view the file that is written, but it appears that only one byte of information is used to write each "0". 如前所述,ASCII零占用了两个字节的信息-这确实使我感到困惑:我查看了写入的文件,但似乎只有一个字节的信息用于写入每个“ 0”。 I'll have to do more research on this topic. 我将不得不对此主题进行更多研究。 Aside from that though, there was another issue with my code- every time I ran the program, the file would receive the zero-characters written to it. 除此之外,我的代码还有另一个问题-每次我运行该程序时,文件都会收到写入的零字符。 There was no issue with that, but there was a second issue with the check - I wasn't doing anything to further advance the file pointer when using the loop for a check. 没问题,但是检查还有第二个问题-在使用循环进行检查时,我没有做任何事情来进一步推进文件指针。

So there were two things that needed to be done to fix my code: 因此,需要做两件事来修复我的代码:

Firstly, I had to find a way to advance the file pointer so that each spot in my RandomAccessFile was being read correctly. 首先,我必须找到一种方法来前进文件指针,以便正确读取我的RandomAccessFile中的每个位置。

Secondly, I had to check for the appropriate value when initiating my check: This value should have been "48", which is the ASCII value for the character "0". 其次,我必须在启动检查时检查适当的值:该值应该为“ 48”,这是字符“ 0”的ASCII值。

Here is my new code: 这是我的新代码:

public static boolean transferStudent(String fileName, int studentNum) throws IOException {

    RandomAccessFile file = new RandomAccessFile(fileName, "rw");
    boolean trueOrFalse = false;
    file.writeInt(75);
    try {
        if (studentNum == 0) {
            file.seek(4);
            file.writeBytes("0000000000000000000000000000000000000000"); // 40 zero characters
            file.seek(4);
            for (int i = 0; i < 40; i++) {
                file.seek(4 + i); // Here is where the file pointer is advanced in the for-loop - very crucial
                if (file.read() == 48) { // Here is where the file is checked for the appropriate value - the ASCII value for "0"
                    trueOrFalse = true;
                }
            }
            return trueOrFalse;
        }
        else if (studentNum > 0) {
            file.seek(4 + (studentNum * 40));
            file.writeBytes("0000000000000000000000000000000000000000"); // 40 more zeroes
            file.seek(4 + (studentNum * 40));
            for (int i = 0; i < 40; i++) { // The same happens here as above
                file.seek((4 + (studentNum * 40)) + i); // ... and here also
                if (file.read() == 48) {
                    trueOrFalse = true;
                }
            }
            return trueOrFalse;
        }
        else {
            return trueOrFalse;
        }
    }
    catch (Exception e) {
        return false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM