Solved

Appending text to an existing text file.

Posted on 2004-10-12
17
190 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
factorial example 4 47
javap not working 8 58
configure dependency in POM for new database 3 34
hibernate jars 4 45
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 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