Wiring a Java file to save the Links from a HTML page bu inputting a URl into a dialogue box

87 views Asked by At

This is my code snippet

          import java.io.*;
          import java.net.*;
          import java.util.*;
          import java.net.URL;
          import java.util.Scanner;
          import javax.swing.JOptionPane;

public class UrlReader {
public static void main(String[] args) throws IOException {


String name = JOptionPane.showInputDialog("Enter a URL");
String address = "http://";

  URL chula = new URL(address+name);
  URLConnection yc = chula.openConnection();
  //BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
  Scanner in = new Scanner(new InputStreamReader(yc.getInputStream()));

  PrintWriter output = new PrintWriter("MynewFile.txt");
  while (in.hasNext()) {
  String inputLine = in.nextLine();
  output.println(inputLine);


     String line = in.next();
     if (line.contains("href=\"http://")) {
            int from = line.indexOf("\"");
            int to = line.lastIndexOf("\"");
            System.out.println(line.substring(from + 1, to));



       }
       in.close();
       output.close();
          }

          }}

For some reason its not saving to the file Mynewfile.txt Does anyone know the reason behind this? I'm new to Java and still learning so i get a bit confused. Also does anyone know how to just save the links in the URL inserted to the file and not the entirety of the HTML code. All help is greatly appreciated and thank you in advance

2

There are 2 answers

0
Monny On

You can try this :-

inputFile.getPath() will get you the file path. You need inputFile.getParent() which will get you the directory of the file.

String fileDir = inputFile.getParent(); 
String fileName = "MynewFile.txt";
File f = new File (fileDir,fileName);
PrintWriter printWriter = new PrintWriter (f);
2
kavai77 On

One mistake in the code snippet you sent is that you are closing the stream inside the while loop. This code probably throws an IOException, because next time you are reading from a closed InputStream. Here is a correction that should work under Java 7:

public static void main(String[] args) throws IOException
{
    String name = JOptionPane.showInputDialog("Enter a URL");
    String address = "http://";
    URL chula = new URL(address + name);
    URLConnection yc = chula.openConnection();

    try(Scanner in = new Scanner(new InputStreamReader(yc.getInputStream()));
        PrintWriter output = new PrintWriter("MynewFile.txt"))
    {
        while (in.hasNext())
        {
            String inputLine = in.nextLine();

            output.println(inputLine);
            String line = in.next();

            if (line.contains("href=\"http://"))
            {
                int from = line.indexOf("\"");
                int to = line.lastIndexOf("\"");

                System.out.println(line.substring(from + 1, to));
            }
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

You should look for the file MynewFile.txt in your project home.