0

对于我的编程课,我们必须设计一个银行账户,它可以读取信息并将信息写入文件,该文件包含诸如 10 位帐号、名字和姓氏、中间名首字母和开户人的余额等信息。(所以它会说 John A. Smith 有一个编号为 1234567890 的帐户,余额至少为 26.00) 无论如何,我在对其进行编程以读取和写入文件时遇到了麻烦。这是有关从文件中读取的说明:

"1. 当您的程序开始运行时,如果文件 acinfo.dbf 存在,它会从中读取数据并创建 BankAccount 类的对象,然后将其存储在数组列表中。acinfo.dbf 由字段帐号、名字组成,中间名首字母和余额。然后您的程序关闭文件。如果文件不存在,您的程序将继续执行,因为这意味着不存在活动帐户。所有这些都在 main 方法中完成。

到目前为止,我已经写了这篇文章,但我几乎不知道我在做什么,这可能是错误的。:(

public class GeauxBank
   {
   public static void main(String[] args)
      {
      ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
      Scanner keyb = new Scanner(System.in);
      String acinfo;
      System.out.print("What is the input file name?");
      acinfo = keyb.next();
      Scanner in = null;

      try
      {
        in = new Scanner(new File(acinfo));
      }
      catch (FileNotFoundException e)
      {
      }

谁能帮我弄清楚该怎么做?

4

1 回答 1

0

从文件中读取:

File file = new File("filename.txt");
FileReader reader;
String line = null;
try {
    reader = new FileReader(file);
    BufferedReader in = new BufferedReader(reader);
    while ((line = in.readLine()) != null)
        System.out.println(line);
    in.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}   

您也可以查看:http ://docs.oracle.com/javase/tutorial/essential/io/file.html

于 2012-03-21T18:34:01.913 回答