File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes ArrayList to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "ArrayList to file" Watch "ArrayList to file" New topic

ArrayList to file

Nick Patel

Joined: Jul 25, 2005
Posts: 2
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

Joined: Jul 08, 2003
Posts: 24199


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


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?

[Jess in Action][AskingGoodQuestions]
Nick Patel

Joined: Jul 25, 2005
Posts: 2
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.
I agree. Here's the link:
subject: ArrayList to file
jQuery in Action, 3rd edition