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

    Default Re: Reading A Binary File

    This code below outputs 20 Ludwig van Beehtoven, and 23 wolfgang amadeus mozart which i believe to be the correct output. now i just need to make it more effiecient code.
    Java Code:
        public static void main(String[] args) throws FileNotFoundException, IOException 
        {
            RandomAccessFile f = new RandomAccessFile("composers.bin", "rw");
            long position = f.getFilePointer();
            long fileLength = f.length();
    
            String one = f.readUTF();
            
            f.getFilePointer();
            
            f.seek(position + 1);
            
            int next = (int) f.readByte();
            
            System.out.println(next +  " " + one); 
            
            f.seek(position + 24); 
            
            int next1 = (int) f.readByte();
            
          f.getFilePointer();
          
            f.seek(position + 23);
            
            String two = f.readUTF();
            
            System.out.println(next1 + " " + two);
        }
    
    }
    Quote Originally Posted by Norm View Post
    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.
    Last edited by IceNine; 03-25-2013 at 07:44 PM.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Reading A Binary File

    Looks like readUTF() is a good tool for reading the records.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Reading A Binary File

    Well i got the code working using a loop which made it much simpler thanks again for all your help.

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

    Default Re: Reading A Binary File

    So my professor added another part to the assignment where he wants the records in alphabetical order but i have run into a snag. He wants us to use this binary file (in photo) Reading A Binary File-binarypicture2.png

    Now My code from yesterday works and outputs the records and there length except for some reason it outputs
    11
    14 Dildock, Dopey
    22 Brennermann, Bojangles
    18 Huggenkizz, Amanda
    11 Pinz, Bobby

    I cant seem to figure out why its not outputting Smith, John after the 11 (i realize there not in alpha order yet im going to do that after i get through this problem.)

    Heers my code
    Java Code:
      RandomAccessFile f = new RandomAccessFile("BinaryInputFile2.bin", "rw");
            long fileLength = f.length();
            if (fileLength == 0)
            {
                System.out.println("The file is blank");
            }
            else
            {
             for (int b = 5; b < fileLength; b++)
            {
              String recordName = f.readUTF();
              f.seek(b);
              int recordLength = (int) f.readByte();
              System.out.println(recordLength + " " + recordName);
              long position2 = f.getFilePointer();
              f.seek(position2 + recordLength);
              b = recordLength + (int) position2;   
            }       
            }

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Reading A Binary File

    What are the first 4 bytes of the file?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Reading A Binary File

    the 1st 4 bytes are the length of the entire file thats what my professor said...which i just noticed is wrong because a 59 would be 89 bytes when the file length is actual 90 bytes

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Reading A Binary File

    Does your code read the 4 bytes before reading the records?
    What is the hardcoded 5 in the for loop for?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Reading A Binary File

    no, it does not read the 4 bytes before reading the record, and the 5 is hard coded so it begins at position 5 to get the record length.

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Reading A Binary File

    The code should read the 4 bytes and use that value to check that the record lengths are correct.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Reading A Binary File

    What would that have to do with reading each individual record? wouldnt i just have to position the pointer over 3 anyway to being reading each record?

    Quote Originally Posted by Norm View Post
    The code should read the 4 bytes and use that value to check that the record lengths are correct.

  11. #31
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Reading A Binary File

    If you read all the bytes using one method or another, there would be no need to position.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Reading A Binary File

    okay well, why is it that even when i attempt to hardcode it to read where the string "Smith, John" is in the file it reads "" each time?
    Quote Originally Posted by Norm View Post
    If you read all the bytes using one method or another, there would be no need to position.

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Reading A Binary File

    Try changing the value to see if 3 or 4 or 6 gets better results.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Reading A Binary File

    Okay, so i got them to print out right. Now i need to sort them in alpha order by last name, so i figured i would add them to an array list an sort them but he wants the output so that the record comes before the name. If i add them to an array list using names.add(recordLength + " " + recordName); and then sort that it will sort it by the length of there record, is there a way to sort them by using there name instead of the record that precedes them?

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Reading A Binary File

    What method are you using to sort them? Does it have an arg that takes a Comparator object that you can write to select the substring that you want to sort on?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Reading A Binary File

    I was just using .sort(), im not very familiar with comparator so i dont really know how it works but are you saying that i can get the substring of each element in the array and sort them that way?

    Quote Originally Posted by Norm View Post
    What method are you using to sort them? Does it have an arg that takes a Comparator object that you can write to select the substring that you want to sort on?

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Reading A Binary File

    Since the Strings you are working with are to be sorted on an interior substring, you'll need a way to compare those interior substrings.

    Why are there numbers on the Strings? Is that required? If so the instructor must have given you the tools to do the sort.
    Making a Comparator for the sort() method is a way to do it.

    I was just using .sort()
    What class is .sort() in?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Reading A Binary File

    There are numbers that precede the stings because its supposed to tell you the length of each record before it gives you the records name, heres what the assignment states below. And the instructor really hasn't taught us much in terms of sorting the only sorting we know is .sort() which really dosnt help at all on this assignment. Oh and the .sort() is in the Collections class.

    Each logical record is variable in length and has a 2 byte Record Descriptor Word (hex value of the length of the record). Using this RDW, you can retrieve the data in the record, and reposition the pointer to the beginning of the next logical record. If you know the size of the entire file (the first 4 bytes), you can keep track of when you're running out of file.

    Submit in a single text file: 1) source code and 2) printout of the data, with the length of each record... 1 record per line as follows:

    8 Jane DOE
    10 John SMITH

    2 Records listed

    The output list should be in alpha order by last name. If the file is empty, your report should indicate there are no records.


    Quote Originally Posted by Norm View Post
    Since the Strings you are working with are to be sorted on an interior substring, you'll need a way to compare those interior substrings.

    Why are there numbers on the Strings? Is that required? If so the instructor must have given you the tools to do the sort.
    Making a Comparator for the sort() method is a way to do it.

    What class is .sort() in?

  19. #39
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Reading A Binary File

    If the list is to be sorted by last name that means the sort order is determined by a substring.

    Are you expected to write your own sort code or can you use a Collections class method?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Reading A Binary File

    i can use the collectiosn class method

    Quote Originally Posted by Norm View Post
    If the list is to be sorted by last name that means the sort order is determined by a substring.

    Are you expected to write your own sort code or can you use a Collections class method?

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Reading / Writing to a binary file
    By JeremyLin in forum New To Java
    Replies: 7
    Last Post: 06-13-2012, 11:12 PM
  2. Reading a binary file
    By sigge in forum New To Java
    Replies: 2
    Last Post: 01-08-2012, 01:44 PM
  3. reading a from a binary file
    By trishtren in forum New To Java
    Replies: 2
    Last Post: 05-11-2011, 05:49 PM
  4. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  5. reading a binary file with a RAF
    By jkurth in forum Advanced Java
    Replies: 2
    Last Post: 12-20-2007, 07: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
  •