Hello,
I tried the below program.
import java.io.*;
import java.net.*;
public class URLGet
{
public static void main(
String[] args) {
BufferedReader in=null;
System.out.println("Starting... ");
if (args.length == 1) {
try {
URL url = new URL(args[0]);
in = new BufferedReader( new InputStreamReader(url.openStream()));
BufferedWriter writer=new BufferedWriter(new FileWriter(new File("myfile.txt")));
String line=null;
while ((line=in.readLine()) != null)
{
System.out.println(line);
writer.write(line,0,line.length());
}
}
catch (MalformedURLException ex) {
System.err.println(ex);
}
catch (FileNotFoundException ex) {
System.err.println("Failed to open stream to URL: "+ex);
}
catch (IOException ex) {
System.err.println("Error reading URL content: "+ex);
}
if (in != null)
try {in.close();} catch (IOException ex) {}
}
else
System.err.println ("Usage: URLGet URL");
}
}
Running: java URLGet
http://google.com But it is giving the error
Error reading URL content: java.net.ConnectException: Connection timed out: connect