Java的阅读文本文件和字符串

问题描述:

我曾尝试搜索关于此的任何可能的问题,分配全线飘红,但没有似乎正是我Java的阅读文本文件和字符串

在这里工作的一些代码,我已经阅读在所有文件子目录,该方法需要的文件名来寻找和其目录

public static Account FileRead(String fname, String dir) throws blankException { 
    File file = new File(dir + fname); 
    try { 

     Scanner sc = new Scanner(file); 

     while (sc.hasNextLine()) { 

      String type = sc.nextLine(); 

      if (type.equals("S") || type.equals("C")) { 
       String pin = sc.nextLine(); 
       double balance = sc.nextDouble(); 
       String name = sc.next(); 
       String address = sc.next(); 
       String date = sc.next(); 

       if (type.equals("S")) { 

        return new SavingAccount(pin, balance, name, address, date); 
       } 
       if (type.equals("C")) { 

        return new CheckingAccount(pin, balance, name, address, date); 
       } 
       throw new blankException("no account type given"); 
      } 
     } 
     sc.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return null; 
}//end file reader 

我的问题是,地址,扫描仪只需要弗里斯特“字”,在该文件中的地址是“1234房子驱动器“,地址只有1234.how我可以在哪里地址是”1234房屋驱动器“

还,所有文件都是这种格式

type 
pin 
balance 
name 
address 
date 
+0

只需使用'Scanner#nextLine()'并分别读取每行输入。 –

只要它使用.nextLine()。它将采用您输入的所有内容。

+0

由于某种原因,它不起作用,可能有一个原因,为什么? – nafis

+0

啊没关系,我似乎已经得到它的工作 – nafis

+0

那么,它的完全工作? –