Page 1 of 3 123 LastLast
Results 1 to 20 of 42
  1. #1
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Reading A Binary File

    I have an assignment to develop a program that reads a binary file. The instructions say:

    "The structure of the file will be such that you will read records of variable length. Each record is preceded by a two-byte field (RDW) which gives the length of the record that follows."

    I am slightly confused by this, he did not give us a binary file to read and told us to create our own, We explained to him we did not now how to do this so what i did was create a text file with a list of names using notepad, opened it in WinHex and save it as a .bin file Is this the correct way to do this? also here is my code that reads the file. All it outputs is the number 97. I have included the text file that i opened in winhex and saved as a .bin

    InputStream in = new FileInputStream("names.bin");
    int next = in.read();
    if (next != -1)
    {
    System.out.println(next);
    }
    Attached Files Attached Files

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Reading A Binary File

    It makes sense that your program outputs a single number 97; you're reading only a single byte and your file is not empty (it starts with "anthony"); so number is not -1, it is 97 which is the unicode for a lower case 'a'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Reading A Binary File

    Okay so i think i figured it out. This outputs every byte.
    InputStream in = new FileInputStream("names.bin");
    boolean done = false;
    while (!done)
    {
    int next = in.read();
    if (next == -1)
    {
    done = true;
    }
    else
    {
    System.out.print(next);
    }
    }
    Quote Originally Posted by JosAH View Post
    It makes sense that your program outputs a single number 97; you're reading only a single byte and your file is not empty (it starts with "anthony"); so number is not -1, it is 97 which is the unicode for a lower case 'a'.

    kind regards,

    Jos
    Last edited by IceNine; 03-23-2013 at 06:08 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Reading A Binary File

    That's a start. The code reads the bytes and displays their int values.

    Your assignment has some byte manipulation requirements.
    If you have a hex editor, you can create a file in the required format for the input file. If the values between the | are bytes and ASCII char values are in 's, then the file's contents could look like this:
    |0|4|'t'|'h'|'i'|'s'|0|2|'i'|'s'|0|1|'a'|0|4|'W'|' o'|'r'|'d'|
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Reading A Binary File

    Thank you, i don't even really understand what the assignment requires he didn't really explain it well nor did he tell us how to make the actual binary file. My question is what exactly is the program supposed to output? The length of the record? so in your example of |0|4|'t'|'h'|'i'|'s'|0|2|'i'|'s'|0|1|'a'|0|4|'W'|' o'|'r'|'d' would the program output 04 02 01 04? Is it even supposed to output anything at all? All it says is develop a program that reads a binary file, then the instructions i put in the previous post. i made a bin file in winHex that follows your example it reads 04this02is01a04word

    Quote Originally Posted by Norm View Post
    That's a start. The code reads the bytes and displays their int values.

    Your assignment has some byte manipulation requirements.
    If you have a hex editor, you can create a file in the required format for the input file. If the values between the | are bytes and ASCII char values are in 's, then the file's contents could look like this:
    |0|4|'t'|'h'|'i'|'s'|0|2|'i'|'s'|0|1|'a'|0|4|'W'|' o'|'r'|'d'|
    Last edited by Norm; 03-23-2013 at 09:33 PM. Reason: fixed quote

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Reading A Binary File

    Quote Originally Posted by IceNine View Post
    I am slightly confused by this, he did not give us a binary file to read and told us to create our own, We explained to him we did not now how to do this so what i did was create a text file with a list of names using notepad.
    You may have already figured this out but all computer files are binary files regardless of what the file suffix is. The suffix only provides information as to how the binary information is to be interpreted. So changing the suffix doesn't (or at least shouldn't) change the contents.

    Caveat: It is common practice to refer to files as either text or binary. This serves to distinguish between files that either do or do not contain only character data.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Reading A Binary File

    Your assignment was to write a program that reads the described file and extracts the records from the file.

    Text files use endline characters (\r\n) to tell the reading program where the end of the record/line is.

    The "binary" file you are to read uses two bytes to hold the length of the record followed by the bytes for the record.
    In my example,
    the first record was 4 bytes long and had the characters: this
    the next record was 2 bytes long and had the characters: is
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Reading A Binary File

    Okay so this prints out whats inside the binary file. inside the binary file i have 07anthony06johnny05manny05tommy and thats what it prints out.


    Java Code:
      public static void main(String[] args) throws FileNotFoundException, IOException {
            RandomAccessFile f = new RandomAccessFile("names.bin", "rw");
            long position = f.getFilePointer();
            long fileLenght = f.length();
            boolean done = false;
            while (!done)
            {
             int next = f.read();
            if (next == -1)
            {
            done = true;
            } 
            else
            {
              String whatsHere = f.readLine();
              System.out.println(whatsHere);
            } 
            }
            
            }
    
        }




    Quote Originally Posted by Norm View Post
    Your assignment was to write a program that reads the described file and extracts the records from the file.

    Text files use endline characters (\r\n) to tell the reading program where the end of the record/line is.

    The "binary" file you are to read uses two bytes to hold the length of the record followed by the bytes for the record.
    In my example,
    the first record was 4 bytes long and had the characters: this
    the next record was 2 bytes long and had the characters: is

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Reading A Binary File

    thats what it prints out
    Where is what is printed out?

    The file contents you posted does NOT have two bytes of binary data (the length). The binary values 0 and 6 will NOT print any character. It could show as either a ? or a small box.

    You need to look at the ASCII character table to see what the int values of chars are.
    The int value of char '0' is 48. Or 0x30
    Java Code:
    System.out.println((int)'0'); // 48
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Reading A Binary File

    Please disregard thsi post and photos and look at the follwoing post.

    Quote Originally Posted by Norm View Post
    Where is what is printed out?

    The file contents you posted does NOT have two bytes of binary data (the length). The binary values 0 and 6 will NOT print any character. It could show as either a ? or a small box.

    You need to look at the ASCII character table to see what the int values of chars are.
    The int value of char '0' is 48. Or 0x30
    Java Code:
    System.out.println((int)'0'); // 48
    Attached Thumbnails Attached Thumbnails Reading A Binary File-binarypicture.png   Reading A Binary File-binarypicture.2png.png   Reading A Binary File-48example.png  
    Last edited by IceNine; 03-24-2013 at 05:34 PM.

  11. #11
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Reading A Binary File

    So i fixed the binary file (check the photo) and my program outputs anthony johnny tommy manny. Please verify if this is the correct way.
    Attached Thumbnails Attached Thumbnails Reading A Binary File-binary.png  

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Reading A Binary File

    The file shown in post#11 is close. But, it only has a single byte for the length of the record. I thought the specs said that there were two byes for the record length (allow 32K length(or 64K if unsigned)). A one byte length field would allow 127/255 byte record length.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Reading A Binary File

    Could this be done by simply changing the length of their names so the preceding value is two bytes in length. For example 23 Wolfgang Amadeus Mozart 20 Ludwig van Beethoven etc....

    Quote Originally Posted by Norm View Post
    The file shown in post#11 is close. But, it only has a single byte for the length of the record. I thought the specs said that there were two byes for the record length (allow 32K length(or 64K if unsigned)). A one byte length field would allow 127/255 byte record length.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Reading A Binary File

    The first two bytes contain the length of the following record in binary.
    For example the first two bytes in hex for a record of length 20 would be: 00|14|
    For example the first two bytes in hex for a record of length 256 would be: 01|00|
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Reading A Binary File

    okay so like in my picture below? Ludwig van Beehtoven is 20 in decimal so a 14 in hex and Wolfgang Amadeus Mozart is a 23 in decimal so a 17 in hex
    Attached Thumbnails Attached Thumbnails Reading A Binary File-composers.png  

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Reading A Binary File

    The file's contents posted in #15 still shows just one byte for the length field and not two bytes.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Reading A Binary File

    okay so i think i fixed it also i want thank you very much for your help so far.
    Attached Thumbnails Attached Thumbnails Reading A Binary File-composers.png  

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Reading A Binary File

    That looks like it. Now to write the logic to read the file and show its records.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    IceNine is offline Member
    Join Date
    Mar 2013
    Posts
    33
    Rep Power
    0

    Default Re: Reading A Binary File

    this outputs it correctly.

    Java Code:
    package binary;
    
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    /**
     *
     * @author Anthony
     */
    public class Binary {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws FileNotFoundException, IOException {
            RandomAccessFile f = new RandomAccessFile("composers.bin", "rw");
            long position = f.getFilePointer();
            long fileLenght = f.length();
            boolean done = false;
            while (!done)
            {
             int next = f.read();
            if (next == -1)
            {
            done = true;
            } 
            else
            {
              String whatsHere = f.readLine();
              System.out.println(whatsHere);
             
            } 
            }
            
            }
    
        }

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Reading A Binary File

    How does the readLine() method stop reading at the end of the record? I don't see where the code uses the 2 byte record length value.
    BTW the formatting needs some work. There should not be }s one above the other like in lines 34-37
    Nested statements should be indented 3-4 spaces.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 3 123 LastLast

Similar Threads

  1. Reading / Writing to a binary file
    By JeremyLin in forum New To Java
    Replies: 7
    Last Post: 06-14-2012, 12:12 AM
  2. Reading a binary file
    By sigge in forum New To Java
    Replies: 2
    Last Post: 01-08-2012, 02:44 PM
  3. reading a from a binary file
    By trishtren in forum New To Java
    Replies: 2
    Last Post: 05-11-2011, 06:49 PM
  4. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  5. reading a binary file with a RAF
    By jkurth in forum Advanced Java
    Replies: 2
    Last Post: 12-20-2007, 08:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •