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
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.