Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

Appending text to an existing text file.

Hi guys I need help again.

I'm writting a large program and it needs to keep a record of its variable states. What I'm doing is, at the end of the day having my program dump all of the variable states to this text file. Sort of like an .ini file. However I need to be able to keep adding to the file.

What I mean is, everyday the program dumps its state to the file but it overwrites yesterdays results. I need to be able to append to the text file.

Is there a way I can do this in Java Thanx ppl
0
kibkid
Asked:
kibkid
  • 5
  • 4
  • 3
  • +3
1 Solution
 
CEHJCommented:
Open in append mode:

FileWriter out = new FileWriter("your-file.txt", true);
0
 
krakatoaCommented:
Use RandomAccessFile.
0
 
sudhakar_koundinyaCommented:
public void Append(String file, String file1) throws Exception {
    FileInputStream first = new FileInputStream(file);
    FileInputStream second = new FileInputStream(file1);
    byte[] array = new byte[first.available()];
    byte[] array1 = new byte[second.available()];
    first.read(array);
    second.read(array1);
    first.close();
    second.close();
    FileOutputStream fout = new FileOutputStream(file);
    fout.write(array);
    fout.write(array1);
    fout.close();

  }
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
sudhakar_koundinyaCommented:
Oh,

I didn't read the question fully
0
 
krakatoaCommented:
I'm curious to know why you need to append data like this - surely the new variables supersede the old ones?
0
 
kmcorbett2Commented:
Is there any advantage to keeping old values in the same file with current values? This file could grow to be unwieldy, and eventually you will hit a file size limitation.

Instead you could rename the current file and write the current values to the same file you use now.

Also you could "rotate" old log files, that is, rename them with the date, time, and/or sequence #. If you do this, you may want  to "trim" the old logs, by deleting all log files older than some specified # of days.

/kmc
0
 
kibkidAuthor Commented:
OK here it is. The program I'm writing is chess game with AI. You would ask "why the hell are you doing that?"  well for the java assignment in computer science I need to write a program which demonstrates AI (second year Univ). Nothing as complicated as the chess game is required but I thought it would be cool as well to write and just due to its complications it should be a guranteed good mark.

I need to keep the state of the old variables for AI. I want the program to learn from every game by looking at prior games.

I'm cheking the code now.
0
 
CEHJCommented:
>>and just due to its complications it should be a guranteed good mark.

Are you sure about that?
0
 
kibkidAuthor Commented:
>>>Are you sure about that?

Why not!!!!????? Damn you are scaring me now
0
 
CEHJCommented:
Personally if i were marking the assignment i'd give higher marks to something simple and executed to a high standard to something surface-scratched, unfinished and confused
0
 
kibkidAuthor Commented:
But why are you suggesting that my program will be "surface-scratched unfinished and confused".

Why can't it be a nice assignment in which the student went the extra mile to make it better and learn more?

;)
0
 
CEHJCommented:
An AI chess-playing Java program - have you any idea how many man hours that would take? Still i suppose it depends on how far you take it ...
0
 
JakobACommented:
Agreeing with CEHJ.
How about mini checkers ?  A 6*6 checkerboard with 3 pieces for each player. On that you could make a fairly simple (and impressive) selflearing AI that beats the pants of your classmates the first 2 or 3 games.
0
 
krakatoaCommented:
Fid Def CEHJ on this.

IBM spent years - decades - programming Deep Thought, and it still only *just* managed to beat Gary Kasparov. That's kinda like the equivalent of you writing a programme to beat me - and it better be above average to do that. ;)
0
 
CEHJCommented:
8-)
0
 
kibkidAuthor Commented:
I'm accepting CEHJ's answer because his was the only one which actually solved my problem. However I thank all of you for your comments and effort.

Just one thing, perhaps I didn't explain correctly the purpose of my program.
My chess game is not supposed to beat every one in the first game. Actually it is supposed to suck at the first game. However it should learn from everygame and grow better, just like a human. That 's the idea.


0
 
JakobACommented:
I know, a selflearning AI.

The problem with standard chess is that the set of possible chessboard states is humungous, it would requre billions of terabyte harddisks to store them so the selflearning must be complemented with loads of rules and methods to identify and prune 'bad' branches, that is the tough part of the job.

regards JakobA

PS for krakatoa: " Fid Def" ???
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now