0

I am using the following to go through a dat file and I am hoping to replace money with another double value.

try {  
    RandomAccessFile file = new RandomAccessFile("files/bank.dat","r");
    FileChannel poos;
    for(int pos = 0; pos < 1000; pos++) {
        file.seek(40*pos);
        file.seek(40*pos + 25);
        file.seek(40*pos + 30);
        double money = file.readDouble(); //I WANT TO CHANGE THIS VALUE!
        poos = file.getChannel();
        if(pos == num) {
            long poss=poos.position();
            money += i;
            file.seek(poss);
            System.out.println(money);
            file.writeDouble(money); 
        }
    }
    file.close();
}
catch (IOException ex) { }

However it does not seem to be working properly - no errors but it doesn't seem to be replacing the text.

Edit:

What I am trying to do is go through the text and find a value, in this case money, which I am then going to replace with the modified value. This is what I am struggling with.

takendarkk
  • 3,347
  • 8
  • 25
  • 37
user2853108
  • 1,291
  • 3
  • 12
  • 15
  • What's the format of your file? If it's a *text* file, then this isn't the right way to do it anyway. It would really help if you'd format your code usefully, too - look at the formatting of the `if` block. – Jon Skeet Jan 12 '14 at 16:26
  • Read advances the location in the file. Don't forget to seek back to the point where the value is located before you write. (And the multiple seeks before the read aren't doing you any good.) – keshlam Jan 12 '14 at 16:58
  • John it is a .dat file. – user2853108 Jan 12 '14 at 17:05
  • I am trying to seek back to the point with the file chanel and then I a am seeking to the position of the file channel – user2853108 Jan 12 '14 at 17:06
  • I see `money += i`. Where did that i come from? – takendarkk Jan 22 '14 at 17:32

0 Answers0