3 Replies - 676 Views - Last Post: 09 April 2009 - 06:49 PM Rate Topic: -----

#1 arctic_blizzard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 08

Random file errors

Posted 09 April 2009 - 05:42 PM

Hey all,

I have an assignment in my Java class that is as follows:
1. Create a random integer file, from 10 to 110
2. Display the following items:
length of file
2nd number
4th number
11th number
modify 11th number to 111
display 11th number again
Append "123" to end of file
display last record (appended one)

Here is my code - it is finished but I am currently getting a weird error when I run the program (its not a syntax error caught by compiling)

import java.io.*;

public class RandFileGen {
		public static void main(String[]args) throws IOException {
				System.out.println("BCIS 251 Random Results");						  //Prints header
				RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");
				inout.setLength(0);													 //Null out file

				for(int i = 10; i < 111; i++)										   //Will save int 10 to 110
						inout.writeInt(i);											  //Writes integers

				System.out.println("Current file length is " + inout.length());		 //Prints length of file

				inout.seek(1*4);														//Sets pointer to 2nd record
				System.out.println("The second number is:  " + inout.readInt());		//Prints second record

				inout.seek(3*4);														//Sets pointer to 4th record
				System.out.println("The fourth number is:  " + inout.readInt());		//Prints fourth record

				inout.seek(10*4);													   //Sets pointer to 11th record
				System.out.println("The eleventh number is:  " + inout.readInt());	  //Print eleventh record

				inout.writeInt(111);													//Rewrites eleventh record
				System.out.println("The eleventh number is:  " + inout.readInt());	  //Prints new eleventh record

				inout.seek(inout.length());											 //Sets pointer to end of file
				inout.writeInt(123);													//Appends record
				System.out.println("The last number is:  " + inout.readInt());		  //Prints last record

				System.out.println("Current file length is " + inout.length());		 //Prints out file length

		inout.close();
		} //ends exception

} //ends class



Several weird things are happening when I run the program:
Here is the output of running my program


BCIS 251 Random Results
Current file length is 404
The second number is: 11
The fourth number is: 13
The eleventh number is: 20
The eleventh number is: 22
Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readInt(RandomAccessFile.java:776)
at RandFileGen.main(RandFileGen.java:28)


The file length is off, and it never rewrote my 11th record, and then it hits the error and goes no further.

I am really stumped, any help would be greatly appreciated!

Cheers

~AB

Is This A Good Question/Topic? 0
  • +

Replies To: Random file errors

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Random file errors

Posted 09 April 2009 - 05:53 PM

				inout.seek(inout.length());											 //Sets pointer to end of file
				inout.writeInt(123);													//Appends record
				System.out.println("The last number is:  " + inout.readInt());		  //Prints last record



You cannot seek passed the end of the file so
inout.seek(inout.length()); //Sets pointer to end of file
should be
inout.seek(inout.length()-1); //Sets pointer to end of file

After your write of 123 is performed you are at the end of the file so you cannot readInt() as you do here
System.out.println("The last number is: " + inout.readInt()); //Prints last record
without doing a seek() to post you 4 bytes before the end of file
Was This Post Helpful? 1
  • +
  • -

#3 arctic_blizzard  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 08

Re: Random file errors

Posted 09 April 2009 - 06:29 PM

Thanks that fixed my write problems, but my file length still is wrong.

import java.io.*;

public class RandFileGen {
		public static void main(String[]args) throws IOException {
				System.out.println("BCIS 251 Random Results");						  //Prints header
				RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");
				inout.setLength(0);													 //Null out file

				for(int i = 10; i < 111; i++)										   //Will save int 10 to 110
						inout.writeInt(i);											  //Writes integers

				System.out.println("Current file length is " + inout.length());		 //Prints length of file

				inout.seek(1*4);														//Sets pointer to 2nd record
				System.out.println("The second number is:  " + inout.readInt());		//Prints second record

				inout.seek(3*4);														//Sets pointer to 4th record
				System.out.println("The fourth number is:  " + inout.readInt());		//Prints fourth record

				inout.seek(10*4);													   //Sets pointer to 11th record
				System.out.println("The eleventh number is:  " + inout.readInt());	  //Print eleventh record

				inout.seek(10*4);													   //Sets pointer to 11th record
				inout.writeInt(111);													//Rewrites eleventh record
				inout.seek(10*4);													   //Resets pointer to 11th record
				System.out.println("The eleventh number is:  " + inout.readInt());	  //Prints new eleventh record

				inout.seek(inout.length()-4);										   //Sets pointer to end of file
				inout.writeInt(123);													//Appends record
				inout.seek(inout.length()-4);										   //Resets pointer to end of file
				System.out.println("The last number is:  " + inout.readInt());		  //Prints last record

				System.out.println("Current file length is " + inout.length());		 //Prints out file length

		inout.close();
		} //ends exception

} //ends class



here is the output:

BCIS 251 Random Results
Current file length is 404
The second number is: 11
The fourth number is: 13
The eleventh number is: 20
The eleventh number is: 111
The last number is: 123
Current file length is 404

file length should be 100 (101 after append) - not 404 - where did that # come from?

-LOL - i just figured it out it doesn't count records it counts bytes

Thanks for the help

This post has been edited by arctic_blizzard: 09 April 2009 - 06:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Random file errors

Posted 09 April 2009 - 06:49 PM

This write 100 int to the file
for(int i = 10; i < 111; i++)		 //Will save int 10 to 110
				  inout.writeInt(i);   


so file is 400 bytes long

you then seek to end and write another int


inout.seek(inout.length()-4);			   //Sets pointer to end of file
				inout.writeInt(123);



so 404 bytes total
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1