Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList to file

 
Nick Patel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I have this arraylist I want to write to a file. The arrayList is being populated by a file that i am reading in. This arrayList then goes through some loops and specific indexes within the arraylist are changed. When I go to write the arraylist to a file:

FileOutputStream out = new FileOutputStream("1.txt");
PrintStream p = new PrintStream(out);
pw = new PrintWriter(out, true);

The incorrect information is written. The content written is the exact same content as was read in from the file minus the changes. When i stepped through my code using netbeans, I saw that infact the arrayList that I wrote the the file had the necessary changes in it. When I do a system.out.println of the arraylist, the changes so up. Its when i try and write the ArrayList to the file,that it doesnt print the changes. What could be wrong?

here is my code:


tempSettings is the arrayList that is getting populated by the file that i am reading in. myListTest is the list that I am setting up to write to the file(populated by tempSettings). Where am i going wrong? Any help would be appreciated. Thanks

[ July 25, 2005: Message edited by: Nick Patel ]
[ July 25, 2005: Message edited by: Nick Patel ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

There are some... interesting things going on here. First, let's make sure that the changed list contains what you think it does. Right before your "printTempSettings()" call, add

System.out.println(tempSettings);

You'll see all the list elements. Are they what you expect?

I have to ask, also, what the purpose of this is:



You've taken two lines to make a copy of tempSettings, but I don't see why a copy needs to be made. How come?
 
Nick Patel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was told by someone here at work, That i might want to add the arrayList to a List and that that might fix my problem. Basically it doesnt do anything other than add more code to my program. The System.out.println(tempSettings) prints exactley what i want it to.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic