Results 1 to 4 of 4
  1. #1
    Join Date
    May 2008
    Location
    Ypsilanti,MI
    Posts
    22
    Rep Power
    0

    Default How to write URL's to a file

    I am having trouble writing only URL's to a file, ie href tags. I have a program that can write a a given http:// address to a file but i only need to write the href tags. I have tried a bunch of things like looking at all the methods that in the java.net package and nothing really seems helpful. Any help would be great below is what i have thus far. Thanks in advance

    Java Code:
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintWriter;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Scanner;
    
    public class LinkFinder 
    {
    	public static void main (String[] args)
    	{
    	
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Please enter a URL to search, please include http:// ");
    		String userInput = keyboard.nextLine();
    		
    		try
    		{
    			
    			
    			String address = userInput;
    			URL u = new URL(address);
    			URLConnection connection = u.openConnection();
    			InputStream stream = connection.getInputStream();
    			Scanner in = new Scanner(stream);
    			PrintWriter links = new PrintWriter(new FileWriter("links.txt"));
    			
    			while(in.hasNextLine())
    			{		
    				links.println(in.nextLine());	
    			}
    				
    			links.close();
    		}
    		catch(FileNotFoundException e)
    		{
    			System.out.println("Cannot find the file.");
    		}
    	
    		catch(MalformedURLException e)
    		{
    			e.getMessage();
    		}
    		
    		catch(IOException e)
    		{
    			e.getMessage();
    		}
    		
    }
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I don't see where you are even attempting to parse the HTML you've gotten. Try this.

  3. #3
    Join Date
    May 2008
    Location
    Ypsilanti,MI
    Posts
    22
    Rep Power
    0

    Default

    wow way to be a fucking asshole...letmegoogle that for you really? wow...i have looked at HTML parsing, but isn't that more advanced then what i am looking to do. Unless you are saying thats the only way is to use some complex html parsing code. Btw the top two searches from your search i have already looked at...wtf is this a honda forum? asshole

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Wow, what an asshole! Post a code that simply copies a stream and expect someone else to do all your work for you. And No an HTML parser is not going overboard. Parse the thing and pull out the href tags. If you want to try and write the half-page long regex that could also be used for this, then go right ahead and try.

Similar Threads

  1. how to write onto a file
    By mirage_87 in forum New To Java
    Replies: 6
    Last Post: 09-08-2009, 03:54 PM
  2. Write to a file in a jar
    By Ms.Ranjan in forum New To Java
    Replies: 4
    Last Post: 06-12-2009, 09:27 PM
  3. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  4. [SOLVED] File Write in File
    By Eku in forum New To Java
    Replies: 4
    Last Post: 10-27-2008, 04:01 AM
  5. Write to file
    By esadeghi in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2008, 01:13 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
  •