0 Replies - 2176 Views - Last Post: 12 March 2009 - 06:45 PM Rate Topic: -----

#1 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

EOF Error when reading data from a random access file

Posted 12 March 2009 - 06:45 PM

Hey everyone,

So i'm a little confused on this one as i've never used random access files before... I'm getting an EOF error when i perform the search method in the clientBroker class.

The error is occurring in the ReadUTF(fName) of the else if statement if the string = "type". I'm not sure if i'm being too clear on this one but i'll put some comments where in the code it's giving the error. Cheers

Here is where i call the clientBroker class

		clientBroker broker = new clientBroker();
		List<Client> commercialType = new ArrayList<Client>();
		List<Client> residentialType = new ArrayList<Client>();
		
		commercialType = broker.search("C", "type");
		residentialType = broker.search("R", "type");




Then here is the client class
/**
 * 
 */
package Assignment3;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * @author Mark Hazlett
 *
 */
public class clientBroker implements broker
{
	private RandomAccessFile file;
	private final int RECORD_SIZE = 131;
	private final int ADDRESS_FIELD = 52;
	private final int CLIENT_TYPE_FIELD = 2;
	private final int FNAME_FIELD = 22;
	private final int LNAME_FIELD = 22;
	private final int ID_FIELD = 8;
	private final int PHONE_NUMBER_FIELD = 16;
	private final int POSTAL_CODE_FIELD = 9;
	
	public clientBroker()
	{
		try 
		{	
			File testFile = new File("Client.dat");
			if(testFile.exists())
			{
				file = new RandomAccessFile("Client.dat", "rw");
				System.out.println("File Created");
			}
			else
			{
				// Catch the exception and open the clients.txt file and load it into the random access file
				File textFile = new File("res\\clients.txt");
				Scanner scanner;
				
				System.out.println("File Not Found");
				
				try 
				{
					scanner = new Scanner(textFile);
					
					while(scanner.hasNext())
					{
						String clientText = scanner.nextLine();
						
						StringTokenizer tokenizer = new StringTokenizer(clientText, ";");
						
						file = new RandomAccessFile("Client.dat", "rw");
						try 
						{
							int recordCounter = 1;
							
							file.seek(0);
							String activeStatus = "A";
							System.out.println(activeStatus);
							
							String firstName = tokenizer.nextToken();
							System.out.print(firstName);
							
							String lastName = tokenizer.nextToken();
							System.out.print(lastName);
							String addressString = tokenizer.nextToken();
							System.out.print(addressString);
							String postalCodeString = tokenizer.nextToken();
							System.out.print(postalCodeString);
							String phoneNumberString = tokenizer.nextToken();
							System.out.print(phoneNumberString);
							String clientType = tokenizer.nextToken();
							System.out.print(clientType + "\n");
							
							file.writeUTF(activeStatus);
							file.writeUTF(firstName);
							file.writeUTF(lastName);
							file.writeUTF(addressString);
							file.writeUTF(postalCodeString);
							file.writeUTF(phoneNumberString);
							file.writeChar(clientType.charAt(0));
							
							file.seek(recordCounter * RECORD_SIZE);
							
							recordCounter++;
							
							System.out.println(firstName);
						} 
						catch (IOException e1) 
						{
							e1.printStackTrace();
						}
						
					}
				} 
				catch (FileNotFoundException e1) 
				{
					
					e1.printStackTrace();
				}
			}
			
			System.out.println("File Opened ok");
		}
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		}
	}
	
	@Override
	public void persist(Object obj)
	{	
		try 
		{
			String fName = ((Client) obj).getFName();
			String lName = ((Client) obj).getLName();
			String address = ((Client) obj).getAddress();
			String postalCode = ((Client) obj).getPostalCode();
			String phoneNumber = ((Client) obj).getPhoneNumber();
			String clientType = ((Client) obj).getClientType();
			
			file.seek(getNewPosition());
			file.writeChars("A");
			file.writeChars(fName);
			file.writeChars(lName);
			file.writeChars(address);
			file.writeChars(postalCode);
			file.writeChars(phoneNumber);
			file.writeChars(clientType);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @return
	 * @throws IOException
	 */
	private long getNewPosition() throws IOException
	{
		return (long)(file.length() / RECORD_SIZE);
	}
	
	/**
	 * 
	 * @return
	 * @throws IOException
	 */
	private int getCurrentFilePointer() throws IOException
	{
		int n = (int) file.getFilePointer();
		
		return n;
	}

	@Override
	public void remove(Object obj) 
	{
		long removeId =((Client) obj).getId();
		
		try 
		{
			file.seek(removeId);
			file.writeChars("D");
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}

	@Override
	public List search(String search, String type)
	{
		List<Client> list = new ArrayList<Client>();
		
		if(type.equals("fName"))
		{
			int counter = 0;
			
			try 
			{
				file.seek(counter);
				String activeStatus = file.readLine();
				long idNumber = file.readLong();
				String fName = file.readLine();
				String lName = file.readLine();
				String address = file.readLine();
				String postalCode = file.readLine();
				String phoneNumber = file.readLine();
				String clientType = file.readLine();


				Client client = new Client(activeStatus, idNumber, fName, lName, address, 
						postalCode, phoneNumber, clientType);

				validate(client);

				if(client.getFName().equals(search))
				{
					list.add(client);
				}

				counter += RECORD_SIZE;

			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			} 
			catch (InvalidPhoneNumberException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			catch (InvalidPostalCodeException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(type.equals("lName"))
		{
			int counter = 0;
			
			try 
			{
				while(file.readLine() != null)
				{
					file.seek(counter);
					String activeStatus = file.readLine();
					long idNumber = file.readLong();
					String fName = file.readLine();
					String lName = file.readLine();
					String address = file.readLine();
					String postalCode = file.readLine();
					String phoneNumber = file.readLine();
					String clientType = file.readLine();


					Client client = new Client(activeStatus, idNumber, fName, lName, address, 
							postalCode, phoneNumber, clientType);
					
					validate(client);
					
					if(client.getLName().equals(search))
					{
						list.add(client);
					}
					
					counter += RECORD_SIZE;
				}
			} 
			catch (IOException e) 
			{

				e.printStackTrace();
			} 
			catch (InvalidPhoneNumberException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			catch (InvalidPostalCodeException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		else if(type.equals("type"))
		{
			int counter = 0;
			
			System.out.println("Search Starting");
			
//Error Is HERE!!!
//Under the String fname is where the issue lies
			try 
			{
				System.out.println("Try Succeded");
				file.seek(counter);
				System.out.println(counter);
				String activeStatus = file.readUTF().trim();
				System.out.println(activeStatus);
				String fName = file.readUTF().trim();
				System.out.println(fName);
				String lName = file.readUTF().trim();
				String address = file.readUTF().trim();
				String postalCode = file.readUTF().trim();
				String phoneNumber = file.readUTF().trim();
				String clientType = file.readUTF().trim();

				Client client = new Client(activeStatus, (counter + 1), fName, lName, address, 
						postalCode, phoneNumber, clientType);
				System.out.println(client.toString());

				validate(client);

				if(client.getClientType().equals(search))
				{
					list.add(client);
				}

				counter += RECORD_SIZE;
			} 
			catch (IOException e) 
			{

				e.printStackTrace();
			} 
			catch (InvalidPhoneNumberException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			catch (InvalidPostalCodeException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public void validate(Object obj) throws InvalidPhoneNumberException, InvalidPostalCodeException 
	{
		String phoneNumber = ((Client) obj).getPhoneNumber();
		
		if(phoneNumber.length() > 14)
		{
			throw new InvalidPhoneNumberException();
		}
		
		String postalCode = ((Client) obj).getPostalCode();
		
		if(postalCode.length() > 7)
		{
			throw new InvalidPostalCodeException();
		}
	}
	
	@Override
	public void closeBroker() 
	{
		try 
		{
			file.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
	}
}


PS Sorry for the edits.... the new [code] thing is not working out very well at all for me in safari. It put it all on one line :S

This post has been edited by markhazlett9: 12 March 2009 - 06:54 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1