java如何在textfile中使整数值自动递增?

时间:2017-02-07 15:41:20

标签: java text-files

到目前为止,我的代码到此为止。

String name;
int age;
int max = 0;
Scanner y = null;
try 
{
    y = new Scanner(new File("new_member.txt"));
    while(y.hasNext())
    {
        int a = y.nextInt();
        max = a;
        String name=txtname.getText();
        int age=Integer.parseInt(txtage.getText());
    }
    y.close();
}
catch(Exception e)
{
    e.printStackTrace();
}
Formatter x = null;
try 
{
    FileWriter f = new FileWriter("new_member.txt", true);
    x = new Formatter(f);
    x.format("%d %s %d%n",++max,name,age);
    x.close();
}
catch(Exception e)
{
    e.printStackTrace();
}

我想让我的文本文件结果如下。 谁有人可以帮忙?

1 Ahmed  15
2 Hamada 16
3 Johny 12
4 Terry 14
5 Jack 13
6 Sarah 12

之后,下一个数据保存到文本文件中应该是 7等等

0 个答案:

没有答案