views:

64

answers:

2

In my program I need to update the Balance column of the text file each time the user does a withdrawal. I used both the write methods as well as the append methods, but to no avail. Once the user has logged in, that particular line is stored in the array

#PIN AccountNo Balance
1598 01-10-102203-0 95000
4895 01-10-102248-0 45000
9512 01-10-102215-0 125000
6125 01-10-102248 85000

The code:

try{
    BufferedWriter out = new BufferedWriter(new FileWriter(".\\AccountInfo.txt",true));
    if (amount <= 0.0) {
        System.out.println("can only withdraw positive amount!");
        transactionScreen();
    }
    else if (this.balance >= amount) {
        if(amount>15000.0){
            System.out.println("The maximum amount allowed per transaction is only Rs.15000/=");
            transactionScreen();
        }
        else{
            this.balance -= amount;
            object.c=this.balance;
            String newBal=Double.toString(this.balance);
            out.write("");
            //out.append("");
            out.write(newBal);
            //out.append(newBal);
        }
    }
    else {
        System.out.println("not enough money in account!");
        transactionScreen();
    }
}
catch(IOException e){
    e.printStackTrace();
}
+6  A: 

The only simple way to do this is to rewrite the entire contents of the text file every time you make any change. But this is awful for several reasons:

  • It is very slow.
  • It can only be done by one thread at a time, so it's not easily scalable.
  • If your application crashes during the write you could lose all your data.

You can use fixed width columns to allow seeking and writing into your file without having to rewrite the entire file, but there are still problems:

  • If you ever need to store a value that exceeds the width needed by your column then you have a problem.
  • You still need to read the entire file (worst case - half on average) to find the record you want to modify (keeping a cache in memory will help here).

I'd suggest that you use a database instead. There are databases that you can install locally and require no administation (SQLite for example).

Mark Byers
+1 - for suggesting (correctly) that a database would be a better idea.
Stephen C
I can use database, but unfortunately I need to complete this program using the notepad file only. So if you can could you give me the code by which i can re-write the entire file all overagain. Thanks
Yoosuf
@Yoosuf: Why can't you use a database? If you need the output as a textfile you can export it from the database as a textfile. As I said there are lots of reasons why you shouldn't use a textfile for this, so where does the requirement come from? It is your duty as a programmer to point out to your clients that their solution has problems - especially when it is information like account balances. Or is this homework? Then I think you should be asking for hints on what approach to use, not asking us to write the code for you.
Mark Byers
It is homework, see earlier posts on the same problem
Jon Freedman
+3  A: 

Consider a file to be a piece of paper with something written on it. At the end of the writing there is still white paper left, so you can append anything you want. If you want to change something in the middle, you can erase some text and write something else. However, you can only replace text with as many letters as you erased. You can not erase 4 letters and write 6 letters, there is simply no space. Therefore, if the balance increases from 900 to 1100, there is no space left in the file. There is one extra letter, which can not be stored unless the remaining part of the file is rewritten.

Sometimes if you want to change something on a piece of paper, it is easier to get a blank piece of paper and copy the original, including the changes you had in mind. This is also often done when a file is changed. Instead of writing just the changes, the changed file is prepared in memory and written over the original file.

Piece of paper

Sjoerd