我想追加第一行,但它追加多行数据的最后一行

时间:2016-05-15 19:47:32

标签: java io text-files

public class AddMoney {

    RegisterAndLogin rl = new RegisterAndLogin();
    private String UserName;
    File file;
    int balance;
    String sourceamt;
    Scanner scanner = new Scanner(System.in);
    String path = "D:\\PesonalAccoutant\\account\\";
    String readData;
    FileWriter fw;
    BufferedWriter bw;
    StringBuilder sb;
    BufferedReader br;

    public AddMoney(String UserName) {
        this.UserName = UserName;
    }

    public void acceptmoneyvalus() throws IOException {
        System.out.println("<1> add money to account");
        System.out.println("<2> remove money from account");
        addmoneytoaccount();

    }

    public void addmoneytoaccount() throws IOException {
        try {
            file = new File(path + UserName + ".txt");
            fw = new FileWriter(file, true);
            bw = new BufferedWriter(fw);
            file.createNewFile();
            System.out.println("file create");

            if (file.exists()) {
                br = new BufferedReader(new FileReader(file));
                if (file.length() == 0) {
                    System.out.println("reading null data");
                    bw.write("Balance=" + 0);
                    bw.newLine();
                    System.out.println("Please enter the amount.");
                    balance = scanner.nextInt();
                    System.out.println("Please enter source of the ammount");
                    sourceamt = scanner.next();
                    addBalance(balance);
                    bw.append("Amount=");
                    bw.append("" + balance);
                    bw.newLine();
                    bw.append("Source of the amount=" + sourceamt);
                    bw.newLine();
                    bw.newLine();

                } else {
                    System.out.println("else blco");
                    System.out.println("Please enter the amount.");
                    balance = scanner.nextInt();
                    System.out.println("Please enter source of the ammount");
                    sourceamt = scanner.next();
                    addBalance(balance);
                    bw.append("Amount=");
                    bw.append("" + balance);
                    bw.newLine();
                    bw.append("Source of the amount=" + sourceamt);
                    bw.newLine();
                    bw.newLine();
                }
            } else {
                System.out.println("try to register again...");
            }

        } catch (Exception e) {
            System.out.println("Error" + e);
        } finally {
            bw.close();

        }
    }

    private void addBalance(int balance) throws IOException {
        try {
            int count = 0;
            br = new BufferedReader(new FileReader(file));
            while ((readData = br.readLine()) != null) {
                count++;
                sb = new StringBuilder();
                if (count == 1) {

                    sb.append(readData);
                    sb.delete(0, 8);
                    System.out.println("full datd is"+readData);
                    //  int i=(Integer)sb;
                    int i = Integer.valueOf(sb.toString());
                    int totalbalance = balance + i;
                    System.out.println("balance is " + sb.toString());
                    System.out.println("total balnce" + totalbalance);
                    bw.append("Balance=" + totalbalance);
                    bw.newLine();
                }

            }
        } catch (Exception e) {

            System.out.println("Error " + e);
        } finally {
            br.close();
        }

    }

}

我想在第一行添加余额,但是当我添加更多数据时,它会追加最后两行而不是文件的开头。 我希望每次加钱都必须在输入金额的增加中反映出来

1 个答案:

答案 0 :(得分:0)

while ((readData = br.readLine()) != null) {
            count++;
            sb = new StringBuilder(); 

while loop方法中的addbalance()内,您每次instantiatingStringBuilder

如果您想要append数据StringBuilder sb;你可以声明为 一个实例变量或在whileLoop之外声明变量,如此

sb = new StringBuilder(); 
while ((readData = br.readLine()) != null) {
                ++count;

并确保将count++更改为++count。 既然您只需要阅读第一行就不必使用while loop,那么您只需使用readLine();方法

StringBuilder sb = new StringBuilder();
private void addBalance(int balance) throws IOException{
    readData = br.readLine(); //first line 
    sb.append(readData);
}