读取和写入txt文件

时间:2016-02-10 02:07:27

标签: java file if-statement printwriter

我正在编写一个读取文件Banking.txt的代码,并询问用户ID和&销,然后提示用户提取,存款或检查余额。余额将写入Records.txt。程序在用户输入引脚后终止,&我无法弄清楚如何让它继续下去。请帮忙

@rowcountdivider

2 个答案:

答案 0 :(得分:1)

您正在从输入读取引脚作为字符串,但您从文件中读取它作为整数;所以Integer pin!= String pin。您的引脚永远不会被识别为正确,因此您的程序会跳到最后。

尝试new Scanner(System.in).nextInt()将输入读作整数。

所以我改变了这一行:

String enteredpin = input.nextLine();

到此:

Integer enteredpin = input.nextInt();

答案 1 :(得分:0)

如果我理解正确,你想写一个" person"对象(即BankRecord的一个实例)到文件?由于我不知道该类中有哪些方法,我假设有一个合适的toString()覆盖。我还假设您正在尝试写入您的主目录。

        Path dir = Paths.get(URI.create("file:///"+System.getProperty("user.home")+System.getProperty("file.separator")+"BankRecords.txt"));
        try (BufferedWriter bfr = Files.newBufferedWriter (dir, Charset.forName("UTF-8"))) {
            String contents = person.toString();
            bfr.write(contents, 0, contents.length());
        }
        catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }