【问题标题】:Bank Account program that reads from a file从文件读取的银行帐户程序
【发布时间】:2011-11-29 18:47:58
【问题描述】:

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

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

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

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)
      {
      }

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

【问题讨论】:

  • 在我们提供帮助之前,您还有很多工作要做。当您从文件中读取时,您必须创建一个银行帐户对象,然后才应该将它们存储在一个列表中。
  • 问问你的导师/助教,如果你有广泛的问题,他们就会得到报酬。
  • 您究竟需要什么帮助?从文件中读取数据?

标签: java file account bank


【解决方案1】:

从文件中读取:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-17
    相关资源
    最近更新 更多