Results 1 to 6 of 6
Thread: Writing Arraylist to new file
- 09-18-2011, 04:41 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
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(); } } }
-
Re: Writing Arraylist to new file
- 09-18-2011, 05:03 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
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)
-
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.
- 09-18-2011, 05:25 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
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.
- 09-18-2011, 04:29 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 40
- Rep Power
- 0
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
-
Trying to read a file, then writing the info into another file
By bigsonny in forum New To JavaReplies: 14Last Post: 07-15-2011, 05:22 AM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
[SOLVED] Writing ArrayList to Text File on seperate Lines
By shinjitsunohana in forum New To JavaReplies: 9Last Post: 08-27-2008, 05:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks