aspose file tools*
The moose likes I/O and Streams and the fly likes Writing recursive generated data to csv or text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Writing recursive generated data to csv or text" Watch "Writing recursive generated data to csv or text" New topic
Author

Writing recursive generated data to csv or text

Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
Hello everyone...

As of right now my project is almost finished. What I am having the program do is to generate probability values repeatedly (Monte Carlo methods I guess). Once it has calculated these values then it will store them in a double array, named markovs. However, what I want it to do as well is when it finished storing those values in the array, it will also store them in an ArrayList, named storeProbs. Once they in the List, a void method will be invoked to write them to a txt or csv file.

As the program computes them it will generate new values (different from the first set), and will replace the old ones in markovs. However, what I need the program to do while it is doing this is save and keep the old file values in storeProbs, while augmenting the ArrayList and adding consecutive new values after the old ones, like so:

first cycle: {.78, .98, .02}
second cycle: {.78, .98, .02, .56, .45, .008}
third cycle: {.78, .98, .02, .56, .45, .008, .13, .25, .67}

Technically, there are nine not three values being calculated and stored in storeProbs, but I just provided that for you as an example of what it needs to do. What I noticed it is doing after every time I run it is that it is only writing the LAST generated set of values and rewriting over the old ones.

What needs to be added or changed in the code to solve the problem? Does anyone have any ideas or suggestions? Any help would be greatly appreciated.

Here is the code that I have. It is rather lengthy I know, but I am posting it in entirety so if you decide to copy and compile it, you won't have any problems running it. To help save time and narrow it down, look at the method saveDataOnDisk() starting on line 236 and ff. The main() starts at line 259.

Thanks again everyone for your help and consideration.

S.T.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

To save even more time, I just read that single method. Which writes your data to a file.

I presume from your comments that it doesn't actually write all of your data to that file? In that case it isn't a good idea to open and close the file in that method. Here's what you should be doing:

(1) Open the file
(2) Write all of the data to it
(3) Close the file

Here's what I conjecture you are doing:

(1) Open the file
(2) Write some of the data to it
(3) Close the file
(4) Open the file
(5) Write some of the data to it
(6) Close the file
(7) ... and so on

Naturally step 5 overwrites what was written earlier by step 2, and so on.
Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
    
    1
I see what you are saying now...

So how should the code look? What should it say? Should it be put in the main method? And where?

S.T.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sorry, I can't tell you where to put the code. That's because you posted too much code so I haven't looked at what's there. Anyway it's pretty simple: open the file before you're going to write anything to it, and close the file after you're finished writing everything to it.
 
Consider Paul's rocket mass heater.
 
subject: Writing recursive generated data to csv or text