Results 1 to 2 of 2
  1. #1
    Don Q is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Help with file reading.

    Hello guys, I'm kinda new to the forum and to Java

    I've been working on a program that accepts records of employees and stores them into a .dat file then it could later ask again to display add more records or to display the data. I already have the accepting records part but the display part isn't working properly.

    Java Code:
    else if(something==2){
            int EmployeeID;
            String lastName,firstName,position;
            char departmentcode;
            float monthlyrate;
            final int EOF = -1;
            try
            {
                RandomAccessFile inFile = new RandomAccessFile("C:/emp.dat","r");
    
                while ((EmployeeID=inFile.readInt())!=EOF)
                {
                EmployeeID = inFile.readInt();
                lastName = inFile.readUTF();
                firstName = inFile.readUTF();
                position = inFile.readUTF();
                departmentcode = inFile.readChar();
                monthlyrate = inFile.readFloat();
                System.out.println("Employee ID = " + EmployeeID + " ");
                System.out.print("Last Name = " + lastName + " ");
                System.out.print("First Name = " + firstName + " ");
                System.out.print("Position = " + position + " ");
                System.out.print("Department Code = " + departmentcode + " ");
                System.out.print("Monthly Rate = " + monthlyrate);
                System.out.println();
                }
                inFile.close();
    
        }
            catch (FileNotFoundException e)
            {
                System.out.println("Error: " + e.getMessage());
            }
            catch (EOFException e)
            {
                System.out.println("No more records to process");
            }
            catch (IOException e)
            {
                System.out.println("Error: " + e.getMessage());
            }

    the problem here is that when I tell it to display it just goes straight to the end of file and tells me that there are no more records to process. please help.

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

    Default Re: Help with file reading.

    For a full explanation of any error I'd have a call to printStackTrace() in the catch blocks.
    You need it to know Which line in the program is throwing the exception?

    Rearrange the printlns to print out immediately after the value is read. That also will help you find which line is throwing the exception.

    Have you looked at the file with a hex editor to know if it contains any good data?

Similar Threads

  1. Reading file
    By Mariem in forum New To Java
    Replies: 2
    Last Post: 11-30-2011, 09:14 PM
  2. Reading file external to jar file
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 05:46 AM
  3. reading a file and writing to a file....help!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 06:53 PM
  4. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  5. Replies: 3
    Last Post: 05-09-2009, 11:31 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
  •