Results 1 to 3 of 3
  1. #1
    threlot is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Question RandomAccessFile Problem

    I need help. Every time I want to read a file (from ReadFile class) some random characters appear with what i have written into memory. I want to display only the strings that i have written to memory.

    Java Code:
    import java.io.RandomAccessFile;
    import java.io.IOException;
    import javax.swing.JOptionPane;
    /**
     * Class WriteClass uses class RandomAccess to create a random acces file with fixed-length String, int and double variables.
     * @author Richard Chuquilin 
     * @version November 11, 2013
     */
    public class WriteClass 
    {
        public static void main(String[] args)
        {
            int account; 
            String firstName, lastName;
            double balance;
    
            try
            {
                RandomAccessFile accountFile = new RandomAccessFile("App_Data/accountFile.dat", "rw");
    
                account = Integer.parseInt(JOptionPane.showInputDialog("Enter account number"));
    
                while(account != 0)
                {
                    accountFile.seek(accountFile.length());
    
                    firstName = JOptionPane.showInputDialog("Enter First Name");
                    lastName = JOptionPane.showInputDialog("Enter Last Name");
                    balance = Double.parseDouble(JOptionPane.showInputDialog("Enter Balance"));
    
                    accountFile.writeInt(account);
                    FixedLengthStringIO.writeFixedLengthString(firstName, 10, accountFile);
                    FixedLengthStringIO.writeFixedLengthString(lastName, 10, accountFile);
                    accountFile.writeDouble(balance);
                }
                
                accountFile.close();
            }
                catch (IOException ex)
                {
                    JOptionPane.showMessageDialog(null, ex);
                }
    
                catch (NumberFormatException ex)
                {
                    JOptionPane.showMessageDialog(null, ex);
                }
    
                catch (RuntimeException ex)
                {
                    JOptionPane.showMessageDialog(null, ex);
                }
    
            }
        }
    Java Code:
    import java.io.RandomAccessFile;
    import java.io.IOException;
    import java.io.EOFException;
    import javax.swing.JOptionPane;
    
    /**
     * Write a description of class ReadClass here.
     * 
     * @author Richard Chuquilin
     * @version November 11, 2013
     */
    public class ReadClass
    {
    
        public static void main(String[] args)
        {
            long accountRecords;
            int account;
            String firstName, lastName;
            double balance;
    
            try
            {
                RandomAccessFile accountFile = new RandomAccessFile("App_Data/accountFile.dat", "r");
    
                accountRecords = Long.parseLong(JOptionPane.showInputDialog("Enter account record (1-" 
                        + (accountFile.length() / 52) + ")") );
    
                while(accountRecords >=1)
                {
                    accountFile.seek(52 * (accountRecords - 1) );
    
                    account = accountFile.readInt();
                    firstName = FixedLengthStringIO.readFixedLengthString(4, accountFile);
                    lastName = FixedLengthStringIO.readFixedLengthString(20, accountFile);
                    balance = accountFile.readDouble();
    
                    JOptionPane.showMessageDialog(null, "Account: " + account + "\nFirst Name: " + firstName + "\nLast Name: " + lastName + "\nBalance: " + balance);
    
                    accountRecords = Long.parseLong(JOptionPane.showInputDialog("Enter account record (1-" 
                            + (accountFile.length() / 52) + ")") );
    
                    accountFile.close();
                }
            }
    
            catch (EOFException ex)
            {
                JOptionPane.showMessageDialog(null, ex);
            }
    
            catch (IOException ex)
            {
                JOptionPane.showMessageDialog(null, ex);
            }
    
            catch (NumberFormatException ex)
            {
                JOptionPane.showMessageDialog(null, ex);
            }
    
            catch (RuntimeException ex)
            {
                JOptionPane.showMessageDialog(null, ex);
            }
    
            System.exit(0);
        }
    }
    Java Code:
    import java.io.IOException;
    import java.io.DataInput;
    import java.io.DataOutput;
    
    /**
     * A class whose methods write and retrieve fixed-length strings.
     * 
     * @author Carl B. Struck
     * @version SCCC West - Copyright (c) 2005
     */
    public class FixedLengthStringIO
    {
        /**
         * Reads and returns a string of fixed-length size from a
         * RandomAccessFile object.
         * 
         * @param size an int which is the number of characters to read
         * @param infile the RandomAccess file object which is assigned 
         *        to the DataInput object
         * 
         * @return The string of characters which was read from the 
         *         RandomAccessFile
         * 
         * @throws IOException
         */
        public static String readFixedLengthString(
                int size, DataInput inFile) throws IOException
        {
            char chars[] = new char[size];
    
            for (int ctr = 0; ctr < size; ctr++)
            {
                chars[ctr] = inFile.readChar();
            }
    
            return new String(chars);
        }
    
         /**
         * Writes a string of fixed-length size to a RandomAccessFile object.
         * 
         * @param outString the string to be written to the RandomAccessFile object
         * @param size an int which is the number of characters to be written
         * @param outfile the RandomAccess file object which is assigned 
         *        to the DataOutput object
         * 
         * @throws IOException
         */
        public static void writeFixedLengthString(String outString,
                int size, DataOutput outFile) throws IOException
        {
            char chars[] = new char[size];
    
            int length = ( outString.length() > size
                              ? size : outString.length() );
    
            outString.getChars(0, length, chars, 0);
    
            // Fill the remaining chars with blank characters
            for (int ctr = length; ctr < size; ctr++)
            {
                chars[ctr] = ' ';
            }
    
            outFile.writeChars( new String(chars) );
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,940
    Rep Power
    4

    Default Re: RandomAccessFile Problem

    Well, I don't know what your data looks like but you are writing two Strings of length 10 and reading two Strings of length 4 and 20 respectively. Could that be the problem? You need to show what you are writing and what you are reading. And provide a little more information on the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,602
    Rep Power
    23

    Default Re: RandomAccessFile Problem

    You forgot to post the code for
    from ReadFile class
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. RandomAccessFile Question
    By Mandingo in forum New To Java
    Replies: 7
    Last Post: 08-05-2013, 08:36 PM
  2. Writing the data into RandomAccessFile
    By vasavi.singh in forum New To Java
    Replies: 5
    Last Post: 01-31-2013, 07:12 AM
  3. Question about RandomAccessFile and MappedByteBuffer
    By Pojahn_M in forum New To Java
    Replies: 17
    Last Post: 06-23-2011, 01:13 PM
  4. read from RandomAccessFile
    By peliukasss in forum New To Java
    Replies: 4
    Last Post: 08-05-2010, 09:34 AM
  5. RandomAccessFile
    By swati.jyoti in forum New To Java
    Replies: 1
    Last Post: 04-22-2009, 04:05 PM

Tags for this Thread

Posting Permissions

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