Solved

Appending text to an existing text file.

Posted on 2004-10-12
17
191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

733 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