简体   繁体   中英

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. 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). Bytes 4 through 43 represent the first student (#0), 44 through 83 represent the second (#1) and so on. When a student transfers to another school, their 40 bytes is overwritten with all 0's (characters).

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. If there are any exceptions or if the file doesn't overwrite the student's data for some reason, I return 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. However, the console always prints false - the check isn't working for some reason. I'm on the verge of tearing my hair out over this. Please help!

You're confusing ASCII zeroes "0" with binary zeros. You're writing the former and testing for the latter. An ASCII "0" takes two bytes. Note that 'character' and 'byte' aren't the same in Java.

So I think I've finally figured out the issue: Like EJP stated, I was confusing the ASCII zeroes "0" with binary zeros. 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". 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.

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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