[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Appending text to an existing text file.

Posted on 2004-10-12
17
Medium Priority
?
197 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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

649 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