Results 1 to 4 of 4
  1. #1
    otoro_java is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default how do i write to a text file from an arraylist?

    How can I write the data from my arraylist to a text file?

    Thanks

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    1. Open file using output stream of your choice.
    2. Loop through each elements in your list
    3. For what ever there is in the list get the string or other native types and write to output stream.

    Done.. Hope it helps..
    dont worry newbie, we got you covered.

  3. #3
    otoro_java is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    Here's what I have, but doesn't seem to work :confused:

    Java Code:
        public static ArrayList writeAccts(ArrayList anAL, String file)
        {
        	Writer output = null;
            try {
              output = new BufferedWriter( new FileWriter(file) );
              output.write(anAL);
            }
            finally {
              if (output != null) output.close();
            }
        	
        	return anAL;
        }

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    		try {
    			output = new BufferedWriter(new FileWriter(file));
    			//output.write(anAL);
    			
    			for (Iterator elementsInList = anAL.iterator(); elementsInList.hasNext();) {
    				<WHAT IS IN THE LIST> element = (<WHAT IS IN THE LIST>) elementsInList.next();
    				output.write(<GET/MAKE String thing from your element>);
    			}
    			
    			
    		} finally {
    			if (output != null)
    				output.close();
    		}
    Take care of exceptions in the code above as well ..
    dont worry newbie, we got you covered.

Similar Threads

  1. how to write onto a file
    By mirage_87 in forum New To Java
    Replies: 6
    Last Post: 09-08-2009, 04:54 PM
  2. Replies: 3
    Last Post: 12-12-2008, 01:12 PM
  3. File Write Error
    By vikain in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2008, 05:38 AM
  4. Write unicode into file
    By vata2999 in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 04:04 PM
  5. Help with write file in java
    By mathias in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •