0

我创建了一个应用程序,允许用户输入他们的帐号、余额(不超过 99999)和姓氏。该程序将获取此信息并将其插入到与帐号 (acct) 对应的位置的 .txt 文件中。这是代码:

import java.io.*;

public class NationalBank {
  public static void main(String[] args) throws Exception{
     InputStreamReader temp = null;
     BufferedReader input = null;
     try {
        temp = new InputStreamReader(System.in);
        input = new BufferedReader(temp);

        int acct;
        double amount;
        String name;
        RandomAccessFile file = new RandomAccessFile("bank.txt", "rw");
        while(true) {
            // Asks for input
           System.out.println("Enter Account Number (0-9999): ");
           acct = Integer.parseInt(input.readLine());
           System.out.println("Enter Last Name: ");
           name = input.readLine();
           System.out.println("Enter Balance ");
           amount = Double.parseDouble(input.readLine());

            // Making sure account numbers are between 0 and 9999
           if(acct >=0 && acct <= 9999) {
              file.seek(acct*17);
              file.write(truncateName(name));
              file.writeBytes(" " +amount);
           }
           else {
              continue;
           }
            // Asks user if more entries are needed
           System.out.println("Enter More? (y/n)");   
           if (input.readLine().toLowerCase().equals("n"))
              break;
        }
        file.close();
     }
        catch (Exception e) {  
        }
  }

// Truncate/adding spaces to name until 8 characters
  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;
  }

}

现在,我正在尝试制作一个应用程序,它将写回所有包含信息的帐户(包括姓氏和余额)。我需要显示这些帐户的帐号、余额和姓氏。到目前为止,我有:

import java.io.*;

public class DisplayBank {
  public static void main(String[] args) throws IOException {
    FileInputStream input = new FileInputStream ("bank.txt");
     try {
        byte[] record = new byte[17];
        while (input.read(record) == 17) {
           String name = new String(record, 0, 8);
           long bits = 0;
           for (int i = 8; i < 17; i++) {
              bits <<= 8;
              bits |= record[i] & 0xFF;
           }
           double amount = Double.longBitsToDouble(bits);

           System.out.println("Account Number: " + record + " Name: " + name + ", amount: " + amount);
        }
     }

     catch (IOException e) {  
     }
     finally {
            input.close();

     }
  }
}

这目前仅正确显示名称。余额不对,不知道怎么获取账号。为了获得帐号,我需要获得name. 为了得到amount,我需要寻找name,偏移 9 个字节,然后读取接下来的 8 个字节......

4

2 回答 2

1

如果您想解析包含姓氏和金额的文本文件,类似于您提供的内容:

提供的示例

姓氏 93942.12

我会做的是尝试类似以下的

 public void read_file(){
    try{

    // Open the file that is the first 
    // command line parameter
    FileInputStream fstream = new FileInputStream("C:\\Users\\Alos\\Desktop\\test.txt");
    // Use DataInputStream to read binary NOT text.
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    int record = 0;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {

        String[] splits = strLine.split("\t");
        String LastName = splits[0];
        String Amount = splits[1]; 
        System.out.println("Account Number: " + record + " Name: " + LastName + ", amount: " + Amount);
        record++;
    }
    //Close the input stream
    in.close();

      }catch (Exception e){//Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }

}

这可能不是您正在寻找的内容,但如果您想要不同的东西,请查看并更新您的问题。

于 2013-02-22T21:35:50.627 回答
0

我这不是家庭作业,我强烈建议使用一些 RDBMS,如 Derby 或 MySQL。

于 2013-02-22T21:50:34.203 回答