I am trying to read a URL and then convert it to a string and write the content to a text file, but I get the following exception when I compile the code. Here is my code and my exception:
import java.io.*;import java.net.URL;import java.net.URLConnection;public class Main {public static String url = "google.com";public static String fileName= null;public static String fileConttent ="Something";public static void main(String[] args) throws Exception {getText(new String(url));}public static void getText(String url) throws Exception {URL website = new URL("url\n" +" public static void main(String[] args) throws Exception {\n" +" getText(new String(url));\n" +" }\n" +"\n" +" public static void getText(String url) throws Exception {\n" +" URL website = new URL(\"url");URLConnection connection = website.openConnection();BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));StringBuilder response = new StringBuilder();String inputLine;while ((inputLine = in.readLine()) != null)response.append(inputLine);in.close();String toBeWritten = response.toString();System.out.println(toBeWritten);}public static void createFile(String fileName,String fileContent){Writer writer = null;try {writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\Dell\\Documents\\t"+"fileName"), "utf-8"));writer.write(fileContent);} catch (IOException ex) {// report} finally {try {writer.close();} catch (Exception ex) {}}}}
This is my exception:
Exception in thread "main" java.net.MalformedURLException: no protocol: urlat java.net.URL.<init>(URL.java:583)at java.net.URL.<init>(URL.java:480)at java.net.URL.<init>(URL.java:429)at Main.getText(Main.java:15)at Main.main(Main.java:11)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:601)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 1
What should be done to solve the issue? Thanks for all answers in advance.
Update
Thank you so much for your answer. I did what you said but I still get the same error.
Exception in thread "main" java.net.MalformedURLException: no protocol: urlpublic static void main(String[] args) throws Exception {getText(new String(url));}public static void getText(String url) throws Exception {URL website = new URL("urlat java.net.URL.<init>(URL.java:583)at java.net.URL.<init>(URL.java:480)at java.net.URL.<init>(URL.java:429)at Main.getText(Main.java:15)at Main.main(Main.java:11)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:601)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 1
Best Answer
public static String url = "google.com";
That's a domain, not a URL (or a relative URL for a file named "google.com," but let's not go there). URLs have protocols, e.g. http.
You need to add the protocol to your url:
public static String url = "http://google.com";
And you have to chnage your code (What ever the result should be):
URL website = new URL("url\n" +" public static void main(String[] args) throws Exception {\n" +" getText(new String(url));\n" +" }\n" +"\n" +" public static void getText(String url) throws Exception {\n" +" URL website = new URL(\"url");
to
URL website = new URL(url);
Change your code to:
import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;public class Main {public static String url = "https://www.google.com";public static String fileName = null;public static String fileConttent = "Something";public static void main(String[] args) throws Exception {getText(new String(url));}public static void getText(String url) throws Exception {URL website = new URL(url);URLConnection connection = website.openConnection();BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));StringBuilder response = new StringBuilder();String inputLine;while ((inputLine = in.readLine()) != null)response.append(inputLine);in.close();String toBeWritten = response.toString();System.out.println(toBeWritten);}}