Results 1 to 7 of 7
- 04-02-2012, 07:04 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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?
ThanksLast edited by Norm; 04-02-2012 at 11:32 PM. Reason: added code tags
- 04-02-2012, 09:47 PM #2
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.
- 04-02-2012, 11:17 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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.
- 04-02-2012, 11:32 PM #4
Re: Creating a simple random file access program
What exceptions are you getting?I kept getting exceptions
The messages you print don't match the number this is printed, except for the first one.Last edited by Norm; 04-02-2012 at 11:38 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-02-2012, 11:55 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
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.
- 04-03-2012, 12:01 AM #6
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:This looks like it prints the 11th number.Java Code:inout.seek(10*4); System.out.println("The third number is " + inout.readInt());
I don't understand what your problem is. Other than confusion about what you are printing.
Position to the 17th number and write 256.how i can modify the 17th number to be 256If you don't understand my response, don't ignore it, ask a question.
- 04-03-2012, 12:13 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Creating a simple random file access program
Went ahead and changed the code and added some stuff
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.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(); } }
Similar Threads
-
Please help me... How to update - (Random access file)?
By etiger in forum Advanced JavaReplies: 2Last Post: 07-17-2011, 08:43 PM -
random file access and GUI
By leiferouis in forum New To JavaReplies: 1Last Post: 02-19-2009, 04:21 AM -
Random File Access
By viper110110 in forum New To JavaReplies: 11Last Post: 11-28-2008, 12:28 AM -
Random access to text file
By lunarbof in forum New To JavaReplies: 1Last Post: 11-05-2008, 08:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks