Results 1 to 2 of 2
- 03-05-2010, 04:08 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Generate and Save Array of Strings to external file
Hi Everyone,
I need to generate array of Strings and then save the strings to a file. Looking around can't get the correct solution. Can someone lead me in getting this done?
My Code looks like this:
Thanks for any help.Java Code:ArrayList <String > set = new ArrayList < String >(); public void search ( String host ) { int countH=count.count(); String str=count.string(); int c; File file =new File(str+".txt"); //BufferedWriter out =null; //String [] urls=new String[countH]; for( c=0;c<countH;c++){ try { FileOutputStream outP = new FileOutputStream (file); DataOutputStream out = new DataOutputStream (outP); URL u = new URL ( host ); set.add ( host ); ArrayList <String > tags = Util . extractTags (u); for (int i = 0; i < tags . size (); ++i) { String nexthost = Util . extractURL ( tags . get(i)); //urls[i]=nexthost; if ( nexthost != null && ! set. contains ( nexthost )) { System .out . println (nexthost); out.writeUTF(nexthost); c++; if(c==countH) System.exit(0); search ( nexthost ); } }out.close(); } catch ( MalformedURLException e) { } catch ( IOException e) { } } }
- 03-05-2010, 04:46 PM #2
where does the object count come from? please post the all the code.
if you want to write your strings from the list into a file than open first the stream and then iterate to the list and extract the strings, format if needed and redirect the output to the file. Here is a fragment of code that i used to write strings from a ArrayList (myList = new ArrayList<Record>();) to a file on a windows system.
Java Code:// First fill the ArrayList with records and then write to file File file = new File( "D:/yourFolder/persons.txt"); try { BufferedWriter out = new BufferedWriter(new FileWriter(file)); it = myList.iterator(); while (it.hasNext()) { Record r = it.next(); out.write(r.name + "\r\n"); } out.close(); } catch (IOException ex) { ex.printStackTrace(); }
here i write only the name in the record which is a String to the file, but you can expand the example and write all members, depending what you need. if you want to append data to an existing file then open it with new FileWriter(file, true). hope, you got the idea.
Similar Threads
-
Strings as array help please..
By crazygurl in forum New To JavaReplies: 1Last Post: 12-01-2009, 08:23 PM -
Need help with counting strings in an array.
By dalonehunter in forum New To JavaReplies: 1Last Post: 10-02-2009, 08:47 AM -
storing strings into an array
By anthonym2121 in forum New To JavaReplies: 2Last Post: 04-04-2009, 07:32 AM -
Can we generate an array for class object?
By gaurav2211 in forum Advanced JavaReplies: 4Last Post: 03-21-2009, 04:20 AM -
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks