Question

I need help with the following parts which I know are wrong:

  1. The spaces/truncating - I have no idea how to do this
  2. The file.seek - Probably wrong since I am not spacing/truncating to 8 characters.

Here is my code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

public class NationalBank
{
  public static void main(String[] args)  
  {
  InputStreamReader temp = null;
  BufferedReader input = null;
  try
  {
     temp = new InputStreamReader(System.in);
     input = new BufferedReader(temp);
     int acct;
     double amount[] = new double[9999];
     String name[] = new String[9999];
     RandomAccessFile file = new RandomAccessFile("bank.txt", "rw");
     while(true)
     {
        System.out.println("Enter Account Number (0-9999): ");
        acct = Integer.parseInt(input.readLine());
        System.out.println("Enter Last Name: ");
        name[acct] = input.readLine();
        System.out.println("Enter Balance ");
        amount[acct] = Double.parseDouble(input.readLine());
        if(acct >=0 && acct <=9999) {
           file.seek(acct*10);
           file.writeBytes(" "+name[acct]);
           file.writeBytes(" "+amount[acct]);
        }

        System.out.println("Enter More? (y/n)");   
        if (input.readLine().toLowerCase().equals("n"))
           break;
     }
     file.close();
  }
     catch (Exception e)
     {  
     }
   }
}
Was it helpful?

Solution

/**
 * Convert name from string into 8 bytes truncating and padding with spaces
 * id necessary.
 */
public static byte [] truncateName (String name)
{
    byte [] result = new byte [8];
    for (int i = 0; i < 8; i++)
        result [i] = i < name.length () ? (byte)name.charAt (i) : (byte)' ';
    return result;
}

/**
 * Convert double value into 8 bytes.
 */
public static byte [] packAmount (double amount)
{
    byte [] result = new byte [8];
    long bits = Double.doubleToLongBits (amount);

    for (int i = 0; i < 8; i++)
    {
        result [i] = (byte)(bits & 0xFF);
        bits >>>= 8;
    }

    return result;
}

public static void writeAccountinformation (
    RandomAccessFile file, int account, String name, double amount)
    throws IOException
{
    file.seek (account * 16); // 8 bytes for name and another 8 for amount
    file.write (truncateName (name));
    file.write (packAmount (amount));
}

public static void main(String[] args) throws Exception
{
    RandomAccessFile file = new RandomAccessFile ("bank.txt", "rw");
    try
    {
        BufferedReader reader = new BufferedReader (
            new InputStreamReader (System.in));

        while (true)
        {
            System.out.print ("Enter Account Number (0-9999): ");
            int account = Integer.parseInt (reader.readLine ());
            System.out.print ("Enter Last Name: ");
            String name = reader.readLine ();
            System.out.print ("Enter Balance: ");
            double amount = Double.parseDouble (reader.readLine ());

            writeAccountinformation (file, account, name, amount);

            System.out.println ("Enter More? (y/n)");
            if (reader.readLine ().toLowerCase ().equals ("n"))
                break;
        }
    }
    finally
    {
        file.close();
    }
}

Later you can read data back from the file like this:

FileInputStream input = new FileInputStream ("bank.txt");
try
{
    byte [] record = new byte [16];
    while (input.read (record) == 16)
    {
        String name = new String (record, 0, 8);
        long bits = 0L;
        for (int i = 15; i >= 8; i--)
        {
            bits <<= 8;
            bits |= record [i] & 0xFF;
        }
        double amount = Double.longBitsToDouble (bits);

        System.out.println("Name: " + name + ", amount: " + amount);
    }
}
finally
{
    input.close ();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top