Results 1 to 6 of 6
  1. #1
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Writing Arraylist to new file

    This app is supposed to take the strings in a text file organize them in alphabetical order and place the results in a new text file. The issue I'm having is when writing the arraylist to a new file. here is the source.

    Java Code:
    import java.awt.List;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;
    
    
    public class AlphaMain {
    
    	public static void main(String[] args){
    		System.out.println("File: ");
    		Scanner scan = new Scanner(System.in);
    		String local = scan.next();
    		ArrayList<String> info = new ArrayList<String>();
    		
    		try {
    			
    			Scanner read = new Scanner(new FileReader(local));
    			while(read.hasNextLine())
    				info.add(read.nextLine());
    			
    			Collections.sort(info);
    			for (int i = 0; i < info.size(); i++) {
    				System.out.println(info.get(i));
    			}
    			
    			try {
    				FileWriter out = new FileWriter("C:\\Temp\\orderedssid.txt");
    				for (int i = 0; i < info.size(); i++) {
    					//out.write(info.get(i));
    					
    					out.append(info.get(i));
    					out.close();
    				}
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing Arraylist to new file

    Quote Originally Posted by seanfmglobal View Post
    This app is supposed to take the strings in a text file organize them in alphabetical order and place the results in a new text file. The issue I'm having is when writing the arraylist to a new file.
    I suppose that you might want to tell us just what issues you've been having?

  3. #3
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Re: Writing Arraylist to new file

    The arraylist gets filled just as it should but after the arraylist is filled and sorted during the point where placing the strings in the new file I get an IOException

    java.io.IOException: Stream closed
    at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
    at sun.nio.cs.StreamEncoder.write(Unknown Source)
    at sun.nio.cs.StreamEncoder.write(Unknown Source)
    at java.io.OutputStreamWriter.write(Unknown Source)
    at java.io.Writer.write(Unknown Source)
    at java.io.Writer.append(Unknown Source)
    at AlphaMain.main(AlphaMain.java:35)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing Arraylist to new file

    You appear to be closing your FileWriter inside the for loop which means that the loop will write the first String to the file, then close the FileWriter, then when it tries to write the second String to the file it finds the FileWriter closed. Perhaps you should close it elsewhere, after you're done using it.

  5. #5
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Re: Writing Arraylist to new file

    Wow!!! Can't believe I didn't catch that. Sorry guys for starting a thread for such a simple mistake.

  6. #6
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Re: Writing Arraylist to new file

    I do have another question on this subject with formatting the output of the strings to the file new file. I would like to have every word on it's individual line for example

    TestOne
    TestTwo
    TestThree
    etc...

    However using any of these:
    out.write(info.get(i)); out.write(info.get(i)+"\n"); out.write("\n"+info.get(i)); out.append(info.get(i)); out.append(info.get(i)+"\n"); out.append("\n"+info.get(i));
    will not do so. I've googled all last night but I can't come up with anything. Thanks again guys...
    Also I've been trying to see if adding %n in there somewhere but that is also a no go. Figured it only words with the print method.

Similar Threads

  1. Replies: 14
    Last Post: 07-15-2011, 06:22 AM
  2. Replies: 1
    Last Post: 02-27-2010, 02:29 AM
  3. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  4. [SOLVED] Writing ArrayList to Text File on seperate Lines
    By shinjitsunohana in forum New To Java
    Replies: 9
    Last Post: 08-27-2008, 06:53 PM

Tags for this Thread

Posting Permissions

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