Solved

Appending text to an existing text file.

Posted on 2004-10-12
17
188 Views
Last Modified: 2010-03-31
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
Comment
Question by:kibkid
  • 5
  • 4
  • 3
  • +3
17 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 12289730
Open in append mode:

FileWriter out = new FileWriter("your-file.txt", true);
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 12289736
Use RandomAccessFile.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12289773
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12289796
Oh,

I didn't read the question fully
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 12289974
I'm curious to know why you need to append data like this - surely the new variables supersede the old ones?
0
 
LVL 2

Expert Comment

by:kmcorbett2
ID: 12291062
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
 

Author Comment

by:kibkid
ID: 12292454
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12292514
>>and just due to its complications it should be a guranteed good mark.

Are you sure about that?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:kibkid
ID: 12292684
>>>Are you sure about that?

Why not!!!!????? Damn you are scaring me now
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12292774
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
 

Author Comment

by:kibkid
ID: 12292826
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12292889
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
 
LVL 15

Expert Comment

by:JakobA
ID: 12294745
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
 
LVL 16

Expert Comment

by:krakatoa
ID: 12295203
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12303732
8-)
0
 

Author Comment

by:kibkid
ID: 12303754
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
 
LVL 15

Expert Comment

by:JakobA
ID: 12303839
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now