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
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();
}
}
}