Results 1 to 7 of 7
  1. #1
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Creating a simple random file access program

    These are the specifications i need within the file:

    Header
    The length of the file
    The 11th number
    The 4th number
    The 17th number
    Modify the 17th number to 256
    The 17th number again
    Append one additional field to contain 512 at the end of the file
    Print the appended number


    Basically I have gotten it to do 4 out of the 9 items.

    Here is my code so far, basically writing off a example given in class:

    Java Code:
     import java.io.*;
    
            public class SimpleRandomAccessFile {
                    public static void main(String[]args)throws IOException {
                    RandomAccessFile inout = new RandomAccessFile("inout.dat","rw");
                    inout.setLength(0);
    
                    for(int i = 0; i < 130; i++)
                            inout.writeInt(i);
    
                    System.out.println("Current file length is " + inout.length());
                    inout.seek(0);
                    System.out.println("The first number is " + inout.readInt());
    
                    inout.seek(10*4);
                    System.out.println("The third number is " + inout.readInt());
    
                    inout.seek(3*4);
                    System.out.println("The ninth number is " + inout.readInt());
    
                    inout.seek(16*4);
                    System.out.println("The seventeenth number is " + inout.readInt());
    
                    inout.writeInt(200);
    
                    inout.seek(inout.length());
                    inout.writeInt(1000);
                    System.out.println("The new length is " + inout.length());
    
            inout.close();
            }
      }

    The output is:


    [bdewall@hermes ~]$ java SimpleRandomAccessFile
    Current file length is 520
    The first number is 0
    The third number is 10
    The ninth number is 3
    The seventeenth number is 16
    The new length is 524


    I tried make a field contain 512 at the end but I kept getting exceptions because I have the initial values set to 0 to 130 and the field width is 4 bytes.

    Can someone point me in the right direction?

    Thanks
    Last edited by Norm; 04-03-2012 at 12:32 AM. Reason: added code tags

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

    Default Re: Creating a simple random file access program

    Can you explain what you are trying to do?
    For example: Define what the contents of the file are supposed to be byte by byte. You have not said what bytes each field in the record use.
    What defines the size of a field?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Creating a simple random file access program

    Here is exactly what the assignment says:

    1. Create a random file with the following characteristics: one field per record, field width 4 bytes, and initial values 0 to 130.

    2. Print out the following with appropriate documentation:


    Header "BCIS 251 Crazy Random Results"
    The length of the file
    The 11th number
    The 4th number
    The 17th number
    Modify the 17th number to 256
    The 17th number again
    Append one additional field to contain 512 at the end of the file
    Print the appended number

    3. Include an xxd printout of the random file after running your program.

    Thats all thats basically included with the assignment. I'm using a example for basing my code.

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

    Default Re: Creating a simple random file access program

    I kept getting exceptions
    What exceptions are you getting?

    The messages you print don't match the number this is printed, except for the first one.
    Last edited by Norm; 04-03-2012 at 12:38 AM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Creating a simple random file access program

    Sorry I forgot to post the exception

    I tried changing the 1000 in this code to a number in the 500's and it wouldn't change a the output anyways so i changed it back.

    inout.seek(inout.length());
    inout.writeInt(1000);
    System.out.println("The new length is " + inout.length());

    Im just trying to figure out how i can modify the 17th number to be 256 and to append the additional field at the end of the file.

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

    Default Re: Creating a simple random file access program

    Why don't the printed comments from the code match what the code is doing?
    For example:
    Java Code:
                   inout.seek(10*4);
                   System.out.println("The third number is " + inout.readInt());
    This looks like it prints the 11th number.


    I don't understand what your problem is. Other than confusion about what you are printing.

    how i can modify the 17th number to be 256
    Position to the 17th number and write 256.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Creating a simple random file access program

    Went ahead and changed the code and added some stuff
    Java Code:
     import java.io.*;
    
    
            public class SimpleRandomAccessFile {
                    public static void main(String[]args)throws IOException {
                    RandomAccessFile inout = new RandomAccessFile("inout.dat","rw");
                    inout.setLength(0);
    
    
                    for(int i = 0; i < 130; i++)
                            inout.writeInt(i);
    
    
                    System.out.println("Current file length is " + inout.length());
                    inout.seek(0);
                    System.out.println("The first number is " + inout.readInt());
    
    
                    inout.seek(10*4);
                    System.out.println("The eleventh number is " + inout.readInt());
    
    
                    inout.seek(3*4);
                    System.out.println("The fourth number is " + inout.readInt());
    
    
                    inout.seek(16*4);
                    System.out.println("The seventeenth number is " + inout.readInt());
    
    
                    inout.seek(16*4);
                    System.out.println("The new seventeenth number is 256");
    
    
                    inout.seek(16*4);
                    System.out.println("The seventeenth number is " + inout.readInt());
    
    
    
    
                    inout.writeInt(200);
    
    
                    inout.seek(inout.length());
                    inout.writeInt(1000);
                    System.out.println("The new length is " + inout.length());
    
    
            inout.close();
            }
            }
    Now all i have to figure out is how to append one additional field to contain 512 at the end of the file and print the appended number.

Similar Threads

  1. Replies: 2
    Last Post: 07-17-2011, 09:43 PM
  2. random file access and GUI
    By leiferouis in forum New To Java
    Replies: 1
    Last Post: 02-19-2009, 05:21 AM
  3. Random File Access
    By viper110110 in forum New To Java
    Replies: 11
    Last Post: 11-28-2008, 01:28 AM
  4. Random access to text file
    By lunarbof in forum New To Java
    Replies: 1
    Last Post: 11-05-2008, 09:53 PM

Posting Permissions

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