Results 1 to 2 of 2
  1. #1
    thestarncy is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default 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:

    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) {
    				}
    			  }
    			}
    Thanks for any help.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by thestarncy View Post
    int countH=count.count();
    String str=count.string();
    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

  1. Strings as array help please..
    By crazygurl in forum New To Java
    Replies: 1
    Last Post: 12-01-2009, 09:23 PM
  2. Need help with counting strings in an array.
    By dalonehunter in forum New To Java
    Replies: 1
    Last Post: 10-02-2009, 09:47 AM
  3. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 08:32 AM
  4. Can we generate an array for class object?
    By gaurav2211 in forum Advanced Java
    Replies: 4
    Last Post: 03-21-2009, 05:20 AM
  5. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:39 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
  •