2011-11-29 38 views
0

对于我的编程类,我们必须设计一个银行账户,用于读取和写入信息到一个文件中,该文件包含信息,如10位账号,名字和姓氏,中间初始值和开户人的余额。 (所以它会说John A. Smith有一个账号1234567890,余额至少为26.00)无论如何,我在编写它来读写文件时遇到了麻烦。这是关于从文件中读取指令:从文件中读取的银行账户程序

“1.当你的程序开始运行,如果该文件acinfo.dbf存在,它就从中读取 数据,并创建BankAccount类它们 然后存储在对象一个数组列表acinfo.dbf由字段账号 包括数字,名字,中间名和余额组成,然后你的程序 关闭文件,如果文件不存在,你的程序简单地进行 ,因为这意味着没有活动帐户存在,所有这些都是在 主要方法中完成的。“

我已经写了这个到目前为止,但我很少知道我在做什么,这可能是错误的。 :(

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

谁能帮我找出做什么

+0

您有更多的去我们甚至可以帮助你,当你从文件中读入你必须创建一个银行账户对象时,你应该只将它们存储在一个列表中 – JonH

+1

问问你的导师/助教,如果你有广泛的问题,他们会得到什么报酬。 – birryree

+1

你到底需要什么帮助?从文件读取数据? – rana

回答

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